Thursday, July 15, 2010

Lullabot Module Development Deep Dive

Notes from Thursday AM - Angie Byron, Jeff Eaton, Randy Fay

Be sure to read http://drupal.org/writing-secure-code - Read it. Love it.

Alter hooks - how to totally trick out your Drupal site.

hook_X-alter() - what we do when Drupal does it wrong...

  1. build stuff
  2. Alter stuff...
( mental note: don't name themes the same as modules - they'll clobber each other and the world will explode. )

Fun tip: PHP5 passes objects by reference, but scalars and arrays get passed by copy.  Don't alter objects passed as arguments unless you mean to.

Some hooks:

  • hook_form_alter()
  • hook_menu_alter
  • hook_link_alter
  • hook_mail_alter
  • hook_schema_alter
  • hook_theme_registry_alter
  • lots more
Common pattern for module developers - firing your own hooks to let other folks interact with your module:

function myapi_do_something() {
// let modules hook myapi_stuff() to add to the list of stuff.
  $stuff = module_invoke_all('myapi_stuff');
// let modules alter the stuff
  drupal_alter('myapi_stuff', $stuff);
// process the stuff
  $results = myapi_process_stuff($stuff);
// let modules react by doing a post-event hook
  module_invoke_all('myapi_stuffed', $stuff, $results);
  return $results
}
This appears to also be a help toward maintainability - expose your stuff in small bits and write a family of modules to work together?  Maybe?

Important tips:

  • make a module for reuse if you do stuff the same way on multiple sites.
  • NEVER name a module and theme the same thing...ka-BOOM
  • be kind to other developers, use unique function names, namespaces, etc.
I will now misquote @webchick, talking about debugging module_invoke_all() - It's not really hacking core if you do it quick... ( speaking of dsm'ing in core to see what modules are altering your hook... )

Posted via email from dsayswhat's posterous