Download - Domas monkus drupal module development
Overview Drupal’s components Modules APIs
Drupal module developmentBasics and examples
Domas [email protected] :: http://domas.monkus.lt
25th September 2010Drupal camp Riga
Overview Drupal’s components Modules APIs
Domas Monkus
Overview Drupal’s components Modules APIs
What is drupal?
CMSNode systemTaxonomyCCK & Views
FRAMEWORKModule ArchitectureThemingAPIs
Overview Drupal’s components Modules APIs
What is drupal?
CMSNode systemTaxonomyCCK & Views
FRAMEWORKModule ArchitectureThemingAPIs
Overview Drupal’s components Modules APIs
Drupal development workflowWhere did my time go?
Overview Drupal’s components Modules APIs
Should You develop a custom module?
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.
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.
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.
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.
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.
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.
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)
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)
Overview Drupal’s components Modules APIs
Overview
Overview Drupal’s components Modules APIs
Drupal’s architecture
procedural (and it works!)event drivenhooks
Overview Drupal’s components Modules APIs
HooksDrupal’s glue
Observer patternDrupal calls available hooks at certain pointsModules implement hooksHooks add additional processing of requests
Overview Drupal’s components Modules APIs
HooksThere’s how many?!
(D7 has 178 hooks)
Overview Drupal’s components Modules APIs
HooksWhat they do.
Overview Drupal’s components Modules APIs
Drupal’s APIsOutput
ThemingMenu APIOutput filteringImage manipulationBlocksTranslationsSearch
Overview Drupal’s components Modules APIs
Drupal’s APIsInput
Forms APIXMLRPC
Overview Drupal’s components Modules APIs
Drupal’s APIsUsers
User managementSession handlingAccess control
Overview Drupal’s components Modules APIs
Drupal’s APIsStorage
Database abstraction layerFile storageCaching
Overview Drupal’s components Modules APIs
Drupal’s APIsInfrastructure
Batch APIEmail handlingUpdatesModule system
Overview Drupal’s components Modules APIs
Reference
http://api.drupal.org
Overview Drupal’s components Modules APIs
How does this work?
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.
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.
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.
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.
Overview Drupal’s components Modules APIs
So where do we put this?
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
Overview Drupal’s components Modules APIs
The .info file
name = Develdescription = Various blocks, pages..package = Developmentdependencies[] = menucore = 6.x
Overview Drupal’s components Modules APIs
The .module file
A simple php fileHooks and internal functions
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’,
);
...
Overview Drupal’s components Modules APIs
3 most common APIs/hooks
Overview Drupal’s components Modules APIs
Menu API
Tasksroutingmenusbreadcrumbs
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;}
Overview Drupal’s components Modules APIs
Menu API
Overview Drupal’s components Modules APIs
Form API
TasksForm creation and renderingForm alteringProcessingForm validation and security
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);
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
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
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,
);...
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
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
Overview Drupal’s components Modules APIs
Node APIOperations
loadinsertupdatedeletevalidatepresaveetc.
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’:...
Overview Drupal’s components Modules APIs
Tips and tools
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
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
Overview Drupal’s components Modules APIs
.. and never
everhackcore
Overview Drupal’s components Modules APIs
.. and neverever
hackcore
Overview Drupal’s components Modules APIs
.. and nevereverhack
core
Overview Drupal’s components Modules APIs
.. and nevereverhackcore
Overview Drupal’s components Modules APIs
Don’t hack core!
Overview Drupal’s components Modules APIs
That’s it(we’ve barely scratched the surface though)
Overview Drupal’s components Modules APIs
Overview Drupal’s components Modules APIs
Questions?