This document discusses adding APIs to custom Drupal modules to make them more flexible and reusable. It covers module architecture best practices, how to create hooks and APIs in Drupal, helper functions like hook_hook_info() and module_invoke(), and examples of custom APIs created for newsletter and payment modules. The presenter offers to answer any questions and invites attendees to Drupal Thursdays for advanced learning and mentions job openings at their company.
1. Think generic - add API's to your custom modules By Jens Beltofte Technical Manager and Senior Drupal Developer at Propeople
2. Facts about me Jens Beltofte Technical Manager at Propeople Working with Drupal since 2007 Founder of Drupal Danmark Member of the Drupal Danmark board DrupalCon CPH core team Camps, days and stammtisch in CPH
3. Facts about Propeople Danish full service web agency 70+ brains CPH, Sofia, Chisinau, Sweden, SF Drupal, Magento, SiteFinity, EPiServer, MOSS Acquia Enterprise Select & Microsoft Gold Berlingske, Amnesty, UNICEF, FDM, Saxo Bank, Arla Foods, Mærsk, Egmont, SBS
4. This session Module architecture API’s what and why? How to create a hook / API Drupal core helper functions Custom API’s Real life examples Questions
5. Module architecture Steps in module architecture in Propeople Think & research Specify Code Review by team lead QA
6. Module architecture Think & research Let the ideas flow around in your brain Think on every aspect of the module Discuss with colleagues if needed Research Prototyping
7. Module architecture Specify Business purpose Every aspect of the module Administration interface Frontend API’s Code examples
8. Module architecture Code Code the module Test your code manually or with simpletest Review with coder & coder_tough_love Commit to version control
10. Module architecture Quality assurance Test module compared to specification Test the user experience Report bugs / record videos
11. API’s what and why? Application programming interface Allow systems to interact Flexibility Custimization Integration Plugins Drupal API’s vs. hooks.
12. 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.
13. 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; } }
14. 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; } function hook_foo_configuration($delta = NULL, &$a3 = NULL) { $form['example_field'] = array( '#type' => 'textfield', '#title' => t('Example field'), ); return $form; } Drupal core hooks no longer use the $op variable, but instead a function for each $op.
15. 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()
16. 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.
17. 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. This means that we can overwrite the hook if it’s code is placed outsite example.module.
18. module_hook() Check if a module implements a given hook. $check = module_hook('example', ’foo'); If example implements the hook example_foo is $check TRUE else FALSE.
19. module_implements() Find all modules implementing the hook. $r = module_implements(’foo'); The function returns an array with all modules implementing the hook foo.
20. module_invoke() Invokes a hook in a specific module. $r = module_invoke('example', ’foo', ’info’, $args); The function returns the return value from example_foo(). If you need $args passed as reference is module_invoke not the solution. Instead use: $function = 'example_foo'; if (module_hook('example,’foo')) { call_user_func_array($function, $args); }
21. module_invoke_all() Invokes a hook in all modules implementing it. $r = module_invoke_all('foo', ’info’, $args); The function returns an array of all the return values. If you need $args passed as reference is module_invoke not the solution. Instead use: $hook = 'foo'; foreach (module_implements($hook) as $module) { $function = $module . '_' . $hook; call_user_func_array($function, $args); }
22. drupal_alter() Invokes hook_TYPE_alter() in modules implementing it. drupal_alter('example', $args); It will invoke module module_example_alter() in all modules. $args is passed as reference to module_example_alter(). drupal_alter() is used in the core for hooks like hook_form_alter(), hook_menu_alter(), hook_link_alter() etc.
23. Custom API’s Newsletter subscription module Flexible module for handling subscription and unsubscription to multiple lists and with custom fields. Used on UNICEF.dk, Amnesty.dk etc. DIBS API (Danish payment gateway) Flexible module for handling payments via DIBS. Support settings per module delta. Used on FDM.dk, UNICEF.dk, DDC.dk etc. Available at http://drupal.org/project/dibs
25. Want to learn more? Drupal Thursdays For you that want to learn advanced Drupal from the developers and themers in Propeople. Location: Sofia, Pirin 40A street. Date: Every Thursday from ~19.30. More info http://groups.drupal.org/bulgaria
26. Need a new job? We’re hiring Team Lead / Senior PHP developer PHP / Drupal developers Senior HTML developer Interested? Talk with Welin or Rumen.