Think Generic - Add API's To Your Custom Modules v2 by Jens Beltofte
Think Generic - Add API's To Your Custom Modules v2 by Jens Beltofte
Specify
• Business purpose
• Every aspect of the module
• Administration interface
• Frontend
• API’s
• Code examples
Module architecture
Code
• Code the module
• Test your code manually or with simpletest
• Review with coder & coder_tough_love
• Commit to version control
Module architecture
Quality assurance
• Test module compared to specification
• Test the user experience
• Report bugs / record videos
API’s what and why?
• Application programming interface
• Allow systems to interact
• Flexibility
• Custimization
• Integration
• Plugins
• Drupal API’s vs. hooks.
How to create a hook / API
A hook:
• Can be defined in D7 – not required.
• Don’t exists as a function in Drupal before a
module implements it.
• Is a skeleton for a function that other modules
can implement.
• Provide an example file or module.
• Know hooks: hook_menu, hook_perm etc.
How to create a hook / API
Example hook D6 style:
function hook_foo($op = 'info', $delta = NULL, &$a3 = NULL) {
switch ($op) {
case 'info':
$info[0] = array('info' => t('Some info about delta 1'));
$info[1] = array('info' => t('Some info about delta 2'));
return $info;
case 'configuration':
$form['example_field'] = array(
'#type' => 'textfield',
'#title' => t('Example field'),
);
return $form;
case 'view':
// Returning the frontend part.
break;
}
}
How to create a hook / API
Example hook D7 style:
function hook_foo_info($delta = NULL, &$a3 = NULL) {
$info[0] = array('info' => t('Some info about delta 1'));
$info[1] = array('info' => t('Some info about delta 2'));
return $info;
}
Drupal core hooks no longer use the $op variable, but instead a function for
each $op.
Drupal core helper functions
Functions that help you implement API’s
• hook_hook_info()
• hook_hook_info_alter()
• module_hook()
• module_implements()
• module_invoke()
• module_invoke_all()
• drupal_alter()
hook_hook_info()
Define hooks exposed by a module in D7
function example_hook_info() {
$hooks[’foo_info'] = array(
'group' => ’hooks',
);
$hooks[’foo_view'] = array(
'group' => ’hooks',
);
return $hooks;
}
Autoloading of example.hooks.inc if it exists.
hook_hook_info_alter()
Alter a hook defined with hook_hook_info().
function example_hook_info_alter(&$hooks) {
// Makes it possible to overwrite existing hooks.
$hooks[’foo_info']['group'] = 'hooks_new';
$hooks[’foo_view']['group'] = 'hooks_new';
}
It will now try to load example.hooks_new.inc instead
of example.hooks.inc.
$function = 'example_foo';
if (module_hook('example,’foo')) {
call_user_func_array($function, $args);
}
module_invoke_all()
Invokes a hook in all modules implementing it.
$r = module_invoke_all('foo', ’info’, $args);
$hook = 'foo';
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
call_user_func_array($function, $args);
}
drupal_alter()
Invokes hook_TYPE_alter() in modules implementing it.
drupal_alter('example', $args);
?
Want to learn more?
Drupal Thursdays
For you that want to learn advanced Drupal from the
developers and themers in Propeople.
We’re hiring
• Team Lead / Senior PHP developer
• PHP / Drupal developers
• Senior HTML developer