domas monkus drupal module development
DESCRIPTION
Presentation for the talk I gave at Drupal Camp Baltics (2010 september 25th in Riga). The main focus is on the basic concepts behind drupal module development. A short overview of available APIs and examples of the most common hooks are given.TRANSCRIPT
![Page 1: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/1.jpg)
Overview Drupal’s components Modules APIs
Drupal module developmentBasics and examples
Domas [email protected] :: http://domas.monkus.lt
25th September 2010Drupal camp Riga
![Page 2: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/2.jpg)
Overview Drupal’s components Modules APIs
Domas Monkus
![Page 3: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/3.jpg)
Overview Drupal’s components Modules APIs
What is drupal?
CMSNode systemTaxonomyCCK & Views
FRAMEWORKModule ArchitectureThemingAPIs
![Page 4: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/4.jpg)
Overview Drupal’s components Modules APIs
What is drupal?
CMSNode systemTaxonomyCCK & Views
FRAMEWORKModule ArchitectureThemingAPIs
![Page 5: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/5.jpg)
Overview Drupal’s components Modules APIs
Drupal development workflowWhere did my time go?
![Page 6: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/6.jpg)
Overview Drupal’s components Modules APIs
Should You develop a custom module?
![Page 7: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/7.jpg)
Overview Drupal’s components Modules APIs
ConsiderationsNot-invented-here?
Maintenance - you will need to maintain and update even ifyou do not release it.
Investment - making custom one-off modules is not overlycost-effective. Will you need it again? In the foreseeablefuture?Lonely path - it takes time to flush out all the bugs. Only inthis case you will almost always be the only one finding them.
![Page 8: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/8.jpg)
Overview Drupal’s components Modules APIs
ConsiderationsNot-invented-here?
Maintenance - you will need to maintain and update even ifyou do not release it.Investment - making custom one-off modules is not overlycost-effective. Will you need it again? In the foreseeablefuture?
Lonely path - it takes time to flush out all the bugs. Only inthis case you will almost always be the only one finding them.
![Page 9: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/9.jpg)
Overview Drupal’s components Modules APIs
ConsiderationsNot-invented-here?
Maintenance - you will need to maintain and update even ifyou do not release it.Investment - making custom one-off modules is not overlycost-effective. Will you need it again? In the foreseeablefuture?Lonely path - it takes time to flush out all the bugs. Only inthis case you will almost always be the only one finding them.
![Page 10: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/10.jpg)
Overview Drupal’s components Modules APIs
AlternativesTo custom modules
Contrib - over 4500 modules, are you sure your problemhasn’t been tackled?
Extending similar modules - base your work on a testedcodebase.One-off solutions - if it’s a rare problem, sometimes hacksare not the worst idea.
![Page 11: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/11.jpg)
Overview Drupal’s components Modules APIs
AlternativesTo custom modules
Contrib - over 4500 modules, are you sure your problemhasn’t been tackled?Extending similar modules - base your work on a testedcodebase.
One-off solutions - if it’s a rare problem, sometimes hacksare not the worst idea.
![Page 12: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/12.jpg)
Overview Drupal’s components Modules APIs
AlternativesTo custom modules
Contrib - over 4500 modules, are you sure your problemhasn’t been tackled?Extending similar modules - base your work on a testedcodebase.One-off solutions - if it’s a rare problem, sometimes hacksare not the worst idea.
![Page 13: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/13.jpg)
Overview Drupal’s components Modules APIs
Enough talkLet’s get to the programming
Things you need to know:
Drupal’s architectureDrupal module structureDrupal core subsystems and APIsCoding conventions
Things you REALLY need to know:PHPMySQLHTML, CSSjavascript (probably)
![Page 14: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/14.jpg)
Overview Drupal’s components Modules APIs
Enough talkLet’s get to the programming
Things you need to know:
Drupal’s architectureDrupal module structureDrupal core subsystems and APIsCoding conventions
Things you REALLY need to know:PHPMySQLHTML, CSSjavascript (probably)
![Page 15: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/15.jpg)
Overview Drupal’s components Modules APIs
Overview
![Page 16: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/16.jpg)
Overview Drupal’s components Modules APIs
Drupal’s architecture
procedural (and it works!)event drivenhooks
![Page 17: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/17.jpg)
Overview Drupal’s components Modules APIs
HooksDrupal’s glue
Observer patternDrupal calls available hooks at certain pointsModules implement hooksHooks add additional processing of requests
![Page 18: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/18.jpg)
Overview Drupal’s components Modules APIs
HooksThere’s how many?!
(D7 has 178 hooks)
![Page 19: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/19.jpg)
Overview Drupal’s components Modules APIs
HooksWhat they do.
![Page 20: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/20.jpg)
Overview Drupal’s components Modules APIs
Drupal’s APIsOutput
ThemingMenu APIOutput filteringImage manipulationBlocksTranslationsSearch
![Page 21: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/21.jpg)
Overview Drupal’s components Modules APIs
Drupal’s APIsInput
Forms APIXMLRPC
![Page 22: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/22.jpg)
Overview Drupal’s components Modules APIs
Drupal’s APIsUsers
User managementSession handlingAccess control
![Page 23: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/23.jpg)
Overview Drupal’s components Modules APIs
Drupal’s APIsStorage
Database abstraction layerFile storageCaching
![Page 24: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/24.jpg)
Overview Drupal’s components Modules APIs
Drupal’s APIsInfrastructure
Batch APIEmail handlingUpdatesModule system
![Page 25: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/25.jpg)
Overview Drupal’s components Modules APIs
Reference
http://api.drupal.org
![Page 26: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/26.jpg)
Overview Drupal’s components Modules APIs
How does this work?
![Page 27: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/27.jpg)
Overview Drupal’s components Modules APIs
Hooking things togetherDelayed publishing
ProblemWe need to publish nodes of unverified users not immediately, butafter a certain wait period.
Hooks usedhook_permsTo define a permission ’Publish nodes immediately’.hook_nodeapiTo forcefully unpublish new nodes.hook_cronTo publish nodes whose wait period has already passed.
![Page 28: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/28.jpg)
Overview Drupal’s components Modules APIs
Hooking things togetherDelayed publishing
ProblemWe need to publish nodes of unverified users not immediately, butafter a certain wait period.
Hooks usedhook_permsTo define a permission ’Publish nodes immediately’.hook_nodeapiTo forcefully unpublish new nodes.hook_cronTo publish nodes whose wait period has already passed.
![Page 29: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/29.jpg)
Overview Drupal’s components Modules APIs
Hooking things togetherTerm-node synchronization
TaskCreate a node for every term in the dictionary and vice-versa,constantly synchronizing the contents of a node’s body and theterm’s description.
Hooks usedhook_form_alterTo add options to taxonomy vocabulary edit form.hook_taxonomyTo react to term creation, updates and deletes.hook_node_infoTo define a custom node type.hook_insert, hook_update, hook_deleteTo react to node creation, updates and deletes.
![Page 30: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/30.jpg)
Overview Drupal’s components Modules APIs
Hooking things togetherTerm-node synchronization
TaskCreate a node for every term in the dictionary and vice-versa,constantly synchronizing the contents of a node’s body and theterm’s description.
Hooks usedhook_form_alterTo add options to taxonomy vocabulary edit form.hook_taxonomyTo react to term creation, updates and deletes.hook_node_infoTo define a custom node type.hook_insert, hook_update, hook_deleteTo react to node creation, updates and deletes.
![Page 31: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/31.jpg)
Overview Drupal’s components Modules APIs
So where do we put this?
![Page 32: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/32.jpg)
Overview Drupal’s components Modules APIs
The structure of a drupal module
module_name/* module_name.info - module descriptionmodule_name.install - installation/uninstallation* module_name.module - module code
![Page 33: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/33.jpg)
Overview Drupal’s components Modules APIs
The .info file
name = Develdescription = Various blocks, pages..package = Developmentdependencies[] = menucore = 6.x
![Page 34: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/34.jpg)
Overview Drupal’s components Modules APIs
The .module file
A simple php fileHooks and internal functions
![Page 35: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/35.jpg)
Overview Drupal’s components Modules APIs
The .module file
<?php...define(’DEVEL_ERROR_HANDLER_BACKTRACE’, 2);.../*** Implementation of hook_menu().*/
function devel_menu() {$items = array();$items[’devel/cache/clear’] = array(
’title’ => ’Empty cache’,’page callback’ => ’devel_cache_clear’,’description’ => ’Clear the CSS cache.’,’access arguments’ => array(’access devel information’),’menu_name’ => ’devel’,
);
...
![Page 36: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/36.jpg)
Overview Drupal’s components Modules APIs
3 most common APIs/hooks
![Page 37: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/37.jpg)
Overview Drupal’s components Modules APIs
Menu API
Tasksroutingmenusbreadcrumbs
![Page 38: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/38.jpg)
Overview Drupal’s components Modules APIs
Menu APIAn example
function devel_menu() {$items = array();
$items[’devel/queries’] = array(’title’ => ’Database queries’,’page callback’ => ’devel_queries’,’access callback’ => ’devel_menu_access_store_queries’,’access arguments’ => array(),’menu_name’ => ’devel’,
);
...
return $items;}
![Page 39: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/39.jpg)
Overview Drupal’s components Modules APIs
Menu API
![Page 40: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/40.jpg)
Overview Drupal’s components Modules APIs
Form API
TasksForm creation and renderingForm alteringProcessingForm validation and security
![Page 41: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/41.jpg)
Overview Drupal’s components Modules APIs
Form APIWhat’s in a form?
Forms are defined as nested arrays
$form = array();$form[’mail’] = array(
’#type’ => ’textfield’,’#title’ => t(’Email’),’#size’ => 20,’#maxlength’ => 128,’#required’ => TRUE,
);return drupal_get_form($form);
![Page 42: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/42.jpg)
Overview Drupal’s components Modules APIs
Form APIAltering forms
Any Form API form can be altered by other modulesFields can be added, removed or changedAdditional validation and submit processing can be added
![Page 43: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/43.jpg)
Overview Drupal’s components Modules APIs
Form APIhook_form_alter
function hook_form_alter(&$form, $form_state, $form_id)
$formThe form array - passed by reference, can be altered$form_stateThe form’s state (if the form has already been rendered)$form_idThe id of the form being processed
![Page 44: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/44.jpg)
Overview Drupal’s components Modules APIs
Form APIAn example
function path_form_alter(&$form, $form_state, $form_id) {if (isset($form[’type’]) && isset($form[’#node’])\
&& $form[’type’][’#value’] .’_node_form’ == $form_id) {$path = isset($form[’#node’]->path) ? $form[’#node’]->path : NULL;$form[’path’] = array(
’#type’ => ’fieldset’,’#title’ => t(’URL path settings’),’#collapsible’ => TRUE,’#collapsed’ => empty($path),’#access’ => user_access(’create url aliases’),’#weight’ => 30,
);...
![Page 45: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/45.jpg)
Overview Drupal’s components Modules APIs
hook_node_api
TasksReact to all operations on nodesAdditional processing or validation of node fieldsAdd additional content to nodes on load
![Page 46: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/46.jpg)
Overview Drupal’s components Modules APIs
function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
$nodeThe node object$opThe operation being performed$a3, $a4Additional parameters, dependant on operation
![Page 47: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/47.jpg)
Overview Drupal’s components Modules APIs
Node APIOperations
loadinsertupdatedeletevalidatepresaveetc.
![Page 48: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/48.jpg)
Overview Drupal’s components Modules APIs
Node APIAn example
function nodeasblock_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {switch ($op) {
case ’load’:$node->nodeasblock = db_result(\
db_query(’SELECT nid FROM {nodeasblock}\WHERE nid = %d’, $node->nid));
break;
case ’insert’:...
![Page 49: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/49.jpg)
Overview Drupal’s components Modules APIs
Tips and tools
![Page 50: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/50.jpg)
Overview Drupal’s components Modules APIs
Tools
devel modulecache operations, dummy content generation, debugging aidscoder modulecoding standards validation, basic module security tipsdrupal api modulehave a local instance of api.drupal.orgsimpletest moduleautomated unit testing of your module (highly encouraged)version controlcvs, svn, git, mercurial - anything is better than nothing
![Page 51: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/51.jpg)
Overview Drupal’s components Modules APIs
Tips
Coding practicesCoding standardskeep your code clean and documented - if you ever decide torelease itCachingbecause performance mattersSplitting large module filesparts can be loaded on demand - less load on the system
Looking for help#drupalgo to the irc.freenode.net irc channel for helpapi.drupal.orgcomplete reference of drupal core APIs
![Page 52: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/52.jpg)
Overview Drupal’s components Modules APIs
.. and never
everhackcore
![Page 53: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/53.jpg)
Overview Drupal’s components Modules APIs
.. and neverever
hackcore
![Page 54: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/54.jpg)
Overview Drupal’s components Modules APIs
.. and nevereverhack
core
![Page 55: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/55.jpg)
Overview Drupal’s components Modules APIs
.. and nevereverhackcore
![Page 56: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/56.jpg)
Overview Drupal’s components Modules APIs
Don’t hack core!
![Page 57: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/57.jpg)
Overview Drupal’s components Modules APIs
That’s it(we’ve barely scratched the surface though)
![Page 58: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/58.jpg)
Overview Drupal’s components Modules APIs
![Page 59: Domas monkus drupal module development](https://reader033.vdocuments.site/reader033/viewer/2022052505/555de731d8b42a1e2c8b528e/html5/thumbnails/59.jpg)
Overview Drupal’s components Modules APIs
Questions?