think generic - add api's to your custom modules v2 by jens beltofte

Upload: segments

Post on 09-Apr-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    1/26

    Think generic

    - add API's to your custommodules

    By Jens Beltofte

    Technical Manager and SeniorDrupal Developer at Propeople

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    2/26

    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

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    3/26

    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, Mrsk, Egmont, SBS

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    4/26

    This session

    Module architecture

    APIs what and why?

    How to create a hook / API Drupal core helper functions

    Custom APIs

    Real life examples

    Questions

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    5/26

    Module architecture

    Steps in module architecture in Propeople

    1. Think & research

    2. Specify3. Code

    4. Review by team lead

    5. QA

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    6/26

    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

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    7/26

    Module architecture

    Specify Business purpose

    Every aspect of the module

    Administration interface

    Frontend

    APIs

    Code examples

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    8/26

    Module architecture

    Code Code the module Test your code manually or with

    simpletest

    Review with coder & coder_tough_love Commit to version control

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    9/26

    Module architecture

    Review by team lead Review with coder & coder_tough_love Manually review

    Testing

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    10/26

    Module architecture

    Quality assurance Test module compared to specification Test the user experience

    Report bugs / record videos

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    11/26

    APIs what and why?

    Application programming interface

    Allow systems to interact

    Flexibility Custimization

    Integration

    Plugins Drupal APIs vs. hooks.

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    12/26

    How to create a hook / API

    A hook:

    Can be defined in D7 not required.

    Dont exists as a function in Drupal beforea 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.

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    13/26

    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;

    }}

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    14/26

    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 afunction for each $op.

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    15/26

    Drupal core helper functions

    Functions that help you implement APIs

    hook_hook_info()

    hook_hook_info_alter() module_hook()

    module_implements()

    module_invoke()

    module_invoke_all()

    drupal_alter()

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    16/26

    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.

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    17/26

    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 its code

    is placed outsite example.module.

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    18/26

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    19/26

    module_implements()

    Find all modules implementing the hook.

    $r = module_implements(foo');

    The function returns an array with all modulesimplementing the hook foo.

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    20/26

    module_invoke()

    Invokes a hook in a specific module.

    $r = module_invoke('example', foo', info, $args);

    The function returns the return value fromexample_foo().

    If you need $args passed as reference is module_invokenot the solution. Instead use:

    $function = 'example_foo';

    if (module_hook('example,foo')) {

    call_user_func_array($function, $args);

    }

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    21/26

    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_invokenot the solution. Instead use:

    $hook = 'foo';

    foreach (module_implements($hook) as $module) {$function = $module . '_' . $hook;

    call_user_func_array($function, $args);

    }

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    22/26

    drupal_alter()

    Invokes hook_TYPE_alter() in modules implementing it.

    drupal_alter('example', $args);

    It will invoke module module_example_alter() in allmodules.

    $args is passed as reference to module_example_alter().

    drupal_alter() is used in the core for hooks likehook_form_alter(), hook_menu_alter(), hook_link_alter()etc.

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    23/26

    Custom APIs

    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 athttp://drupal.org/project/dibs

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    24/26

    Questions?

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    25/26

    Want to learn more?

    Drupal ThursdaysFor 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

  • 8/7/2019 Think Generic - Add API's to Your Custom Modules v2 by Jens Beltofte

    26/26

    Need a new job?

    Were hiring Team Lead / Senior PHP developer PHP / Drupal developers

    Senior HTML developer

    Interested? Talk with Welin or Rumen.