0% found this document useful (0 votes)
331 views26 pages

Think Generic - Add API's To Your Custom Modules v2 by Jens Beltofte

- The document discusses creating APIs and hooks in Drupal modules to make them more flexible and customizable. - It provides examples of how to define a hook, create API functions, and leverage Drupal core functions like hook_hook_info() and module_invoke() to build APIs. - The speaker advocates designing modules with APIs and hooks from the beginning to promote extensibility and integration with other modules.

Uploaded by

segments
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
331 views26 pages

Think Generic - Add API's To Your Custom Modules v2 by Jens Beltofte

- The document discusses creating APIs and hooks in Drupal modules to make them more flexible and customizable. - It provides examples of how to define a hook, create API functions, and leverage Drupal core functions like hook_hook_info() and module_invoke() to build APIs. - The speaker advocates designing modules with APIs and hooks from the beginning to promote extensibility and integration with other modules.

Uploaded by

segments
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Think generic

- add API's to your custom modules


By Jens Beltofte
Technical Manager and Senior
Drupal Developer at Propeople
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
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
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
Module architecture
Steps in module architecture in Propeople
1. Think & research
2. Specify
3. Code
4. Review by team lead
5. QA
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
Module architecture

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

Review by team lead


• Review with coder & coder_tough_love
• Manually review
• Testing
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;
}

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.
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.

This means that we can overwrite the hook if it’s code


is placed outsite example.module.
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.
module_implements()
Find all modules implementing the hook.
$r = module_implements(’foo');

The function returns an array with all modules


implementing the hook foo.
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);
}
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);
}
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.
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
Questions?

?
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


Need a new job?

We’re hiring 
• Team Lead / Senior PHP developer
• PHP / Drupal developers
• Senior HTML developer

Interested? Talk with Welin or Rumen.

You might also like