It’s amazing – I’ve worked on dozens of Drupal projects (as far back as 4.7) as a PM, and never actually built a module myself. Wow. Now I’m going to have to learn!
Contents |
Guides
- Learn how to create a module from scratch!:Â http://drupal.org/node/1074362
- Configure your fave text editor to do syntax highlighting with Drupal’s non-standard filenames:Â http://drupal.org/node/29325
Howto: block function
- name it: give it a lowercase_underscore type name for module file name and function prefixes (make sure it’s not an existing theme name)
- create folder and module file sites/all/modules/module_name/module_name.module
- create module_name.info file
- add module @file comment description
- add a help hook: module_name_help function (returns text and breaks)
- if it’s a block hook, add module_name_block_info (describe it to Drupal) : info, cache etc.
- add content functions (get things from db. etc)
- if it’s a block module: do a block view
- use a switch($delta) – you might want more blocks later in the same module
- do access checks – on content retrieval functions
- do the db grab function to get stuff in a result
- load up the $items array with a foreach loop
- if it’s empty – return an apology
- otherwise: pass data (as $items array) through ot the template function – choosing a format (lists etc… or whatever)
- return the block
Configuration form
- Impement a hook_menu function (module_name_menu) – to create a configuration form for the module
- do not t(translate) it (automatically translated by drupal) – pass it an array of title, description, page callback (drupal_get_form), page arguments (current_posts_form – see below), access arguments and type.
- Then return items.
- now do the current_posts_form
- use #variables and check the forms api reference for element attributes you can set.
- variable_get(thing, def.val) to get/set default persistent variable (indexed on form name)
- system_settings_form calls Drupal’s form api to do the work. (you don’t have to create a submit button… although we could if we wanted)
- now do any necessary data validation
- use &$form_state to capture and keep $_POST variables during form process
- use ifs to validate values from the $form_state array
Permissions for custom pages
- create module_name_permission hook
- create private page callback hook
- add $display variable to module_name_contents() function
- this will enable block or page view
create page (private) view
- use private function call for module_name_page hook
- load items into array with module_name_contents hook (+display variable ‘page’)
- add a ‘if no data’ option & return the page array
- else, return page array *with double underscore after theme tag*
- eg: item_list__module_name (this enables specific theming options by telling Drupal it’s a theme hook suggestion)
Notes on function names
- private: _module_name_functionname
- public: module_name_functionname
- always check your function name isn’t the same as a hook name
- if you’re implementing a hook: module_name_hookname
- NB: page callbacks (very specific to module) are a good candidate for private
Creating a module action
- first define the action using hook_actions_info
- then create action functions module_name_description_of_action_action
- optional form definition