230 likes | 338 Views
Hook, Drush, and Alters Oh Mai!. btopro @btopro http://btopro.com/ http://drupal.psu.edu/blog/1 Bryan Ollendyke. This is a code deep dive “ Hello World ” dev @ 10am in 206 Modules and Dev must haves Cover basic hooks Cover more obscure but useful hooks
E N D
Hook, Drush, and Alters Oh Mai! btopro @btopro http://btopro.com/ http://drupal.psu.edu/blog/1 Bryan Ollendyke
This is a code deep dive • “Hello World” dev @ 10am in 206 • Modules and Dev must haves • Cover basic hooks • Cover more obscure but useful hooks • Examples provided throughout via contrib Topics
Stackoverflow • Api.drupal.org • Drupal.org issue queues • Drush.org • http://dropbucket.org/ Places to look for help
devel (drush support) • Easy to debug all aspects of drupal objects • drush - Drupal via Command line • Ex: drush @sites cc all • Clear cache on all sites in this multi-site • coder (drush support) • Feedback on code quality based on Drupal community conventions • Provides security audits and suggestions • libraries • API for reusable, non-drupal lib integration Must Have projects
devel_themer • Theme debugging / template sniffing • query_coder • Convert SQL to Drupal query language • features (drush support) • Bundle exportable configuration • Maintain consistency / version control IA • profiler_builder (drush support) • Help converting a site into a distribution • Consistency across build routines (ulmus) Nice to Have projects
registry_rebuild (drush) • Drush extension, life saver at times • backup_migrate (drush support) • UI / Drush based migrations • module_builder (drush support) • Generate module skeletons quickly • examples • Community approved example implementations of APIs Nice to Have projects
entity • field_collection • replicate • Libraries • context • rules Other good API projects
Commonly referred to as “the drupal way” • Easy to override some else’s code • Goal is to never modify others code unless absolutely necessary • Common to write a “sitename_helper” style module loaded with alters Hook/alter architecture
init • page_build • page_alter • form_alter • menu • menu_alter • admin_paths • theme_registry_alter • custom_theme • views_query_alter A few random Hooks
page_build • page_alter Hook Example
page_build • page_alter Alter Example
arg($id) – part of the url (node/67/edit) • arg(0) == ‘node’ • arg(1) == 67 • arg(2) == ‘edit’ • arg(3) NULL • menu_get_item – pull object from URL • $node = menu_get_item(); • the same as node_load(arg(1)); but more flexible (works with other menu objects) Useful API functions
module_load_include(‘module’, ‘yourproject’) • Load an include file w.o. knowing path • module_invokes(‘cron’); • List all modules invoking a hook • drupal_static($value, ‘default’); • Centralized static caching • drupal_form_submit(‘name’, $values) • Programmatically trigger form to submit • dvm($node) or dpm($node) • Required devel, clean var_dump of object Useful API functions
module_invoke_all (‘things_stuff’) • Assemble an array of all returns • {MODULENAME}_things_stuff • drupal_alter(‘things_stuff’, $data) • $data passed by reference to all modules • {MODULE}_things_stuff_alter(&$data); • Invoking existing hook (like page_build) • Call your module_invoke_all(‘myhook’); • Follow it by drupal_alter(‘myhook’, $data); • This allows other devs to safely mod Build own hooks
mooc_helper • Supplamental glue code for install profile • Mostly just injects minor changes • Drupal.org/project/mooc • Then view repository Common Site Example
quojs • Helps include quojs with Drupal • Allows you to invoke js library from other modules • Requires libraries API module • drupal.org/project/quojs Simple Library Example
regions • Stand alone “block” areas across-theme • Creates its own API that is invoked • Add “widgets” that function across themes • Reuse components across sites easier • Export via features cleaner w.o. theme issues • drupal.org/project/regions Complex Example
cis_connector • Web service endpoint for my edtech distros • Caches http calls through a wrapper • Uses mix of drupal_static and cache_set • drupal.org/project/cis_connector • https://drupal.psu.edu/node/779 Complex Example
entity_iframe • Stand alone functionality for any site • Themeable via tpl.php files • Views integration • Dynamic url rewriting per request • Allows secure cross-frame communication • drupal.org/project/entity_iframe Complex Example
drush dl outline_designer views regions cis_connector • Download outline_designer, views, regions, and cis_connector to sites/all/modules • --y can be used to answer yes to everything • drush @sites cc all • Clear the cache on all sites Simple Drush example
book_copy • Replicate book outlines in Drupal • Drush support for passing a node id • drupal.org/project/book_copy Writing your own Drush
See if functionality could be useful elsewhere • Solve for Drupal, not for PSU • hooks/alters to solution, PSU being just 1 • Code “The drupal way” • Try to structure code as follows: • Contrib module – given away • PSU – specific to university • College – specific to our usage • Implementation – specific to 1 deployment Maximize reuse