Upload
evelyn-allison
View
213
Download
1
Embed Size (px)
Citation preview
Hook, Drush, and Alters Oh Mai!Hook, Drush, and Alters Oh Mai!
btopro@btopro
http://btopro.com/http://drupal.psu.edu/blog/1
Bryan Ollendyke
TopicsTopics
• 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
Places to look for helpPlaces to look for help
• Stackoverflow• Api.drupal.org• Drupal.org issue queues• Drush.org• http://dropbucket.org/
Must Have projectsMust Have projects
• 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
Nice to Have projectsNice to 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 projectsNice 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
Other good API projectsOther good API projects
• entity• field_collection• replicate• Libraries• context• rules
Hook/alter architectureHook/alter architecture
• 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
A few random HooksA few random Hooks
• init• page_build• page_alter• form_alter• menu• menu_alter• admin_paths• theme_registry_alter• custom_theme• views_query_alter
Useful API functionsUseful API functions
• 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 functionsUseful 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
Build own hooksBuild own hooks
• 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
Common Site ExampleCommon Site Example
• mooc_helper• Supplamental glue code for install profile• Mostly just injects minor changes• Drupal.org/project/mooc
• Then view repository
Simple Library ExampleSimple Library Example
• quojs• Helps include quojs with Drupal• Allows you to invoke js library from other
modules• Requires libraries API module• drupal.org/project/quojs
Complex ExampleComplex 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 ExampleComplex 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 ExampleComplex 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
Simple Drush exampleSimple Drush 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
Writing your own DrushWriting your own Drush
• book_copy• Replicate book outlines in Drupal• Drush support for passing a node id• drupal.org/project/book_copy
Maximize reuseMaximize reuse
• 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