Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hook, Drush, and Alters Oh Mai! Bryan Ollendyke.

Similar presentations


Presentation on theme: "Hook, Drush, and Alters Oh Mai! Bryan Ollendyke."— Presentation transcript:

1 Hook, Drush, and Alters Oh Mai! btopro @btopro http://btopro.com/ http://drupal.psu.edu/blog/1 Bryan Ollendyke

2 Topics 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

3 Places to look for help Stackoverflow Api.drupal.org Drupal.org issue queues Drush.org http://dropbucket.org/

4 Must 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

5 Nice 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)

6 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

7 Other good API projects entity field_collection replicate Libraries context rules

8 Hook/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

9 A few random Hooks init page_build page_alter form_alter menu menu_alter admin_paths theme_registry_alter custom_theme views_query_alter

10 Hook Example page_build page_alter

11 Alter Example page_build page_alter

12 Useful 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)

13 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

14 Build 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

15 Common Site Example mooc_helper Supplamental glue code for install profile Mostly just injects minor changes Drupal.org/project/mooc Then view repository

16 Simple 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

17 Complex 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

18 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

19 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

20 Simple 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

21 Writing your own Drush book_copy Replicate book outlines in Drupal Drush support for passing a node id drupal.org/project/book_copy

22 Maximize 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

23 Questions? #dcpsu @psu_drupal


Download ppt "Hook, Drush, and Alters Oh Mai! Bryan Ollendyke."

Similar presentations


Ads by Google