I’m a big fan of Drupal, possibly the biggest in our office. Over the past seven years, I’ve been thoroughly indoctrinated into all things best practice, patched inherited hacks, learned how to replace core components in ways that ensure the site can still auto update, and generally, just stretched the limit of what Drupal can do. So I thought it’d be a good time to step back and review just what it is I use Drupal for most often in custom development. I’ll start by giving some statistics on what hooks I use most, why. Then, just for fun, I’ll cover the the more rare cases.
Hook Usage Statistics
Hook | Percentage of projects |
---|---|
hook_menu | 93.75 |
hook_permission | 87.5 |
hook_form_{form_id}_alter | 81.25 |
hook_node_view | 75 |
hook_block_view | 75 |
hook_block_info | 75 |
hook_wysiwyg_editor_settings_alter | 62.5 |
hook_views_pre_render | 62.5 |
hook_views_api | 62.5 |
hook_theme | 62.5 |
hook_node_insert | 62.5 |
hook_node_update | 56.25 |
hook_node_presave | 56.25 |
hook_flush_caches | 50 |
hook_node_load | 43.75 |
hook_menu_alter | 43.75 |
hook_query_TAG_alter | 37.5 |
hook_init | 37.5 |
hook_html_head_alter | 31.25 |
hook_field_extra_fields | 31.25 |
hook_field_attach_form | 31.25 |
hook_entity_info_alter | 31.25 |
hook_user_insert | 25 |
hook_mail | 25 |
hook_views_default_views | 18.75 |
hook_user_update | 18.75 |
hook_node_delete | 18.75 |
hook_drush_command | 18.75 |
hook_cron | 18.75 |
hook_block_save | 18.75 |
hook_block_configure | 18.75 |
hook_user_login | 12.5 |
hook_services_resources_alter | 12.5 |
hook_services_resources | 12.5 |
hook_rules_action_info | 12.5 |
hook_preprocess_page | 12.5 |
hook_node_validate | 12.5 |
hook_node_access | 12.5 |
hook_mail_alter | 12.5 |
hook_form_alter | 12.5 |
hook_filter_info | 12.5 |
hook_exit | 12.5 |
hook_boot | 12.5 |
hook_votingapi_results | 6.25 |
hook_views_query_alter | 6.25 |
hook_views_handlers | 6.25 |
hook_username_alter | 6.25 |
hook_user_view | 6.25 |
hook_uninstall | 6.25 |
hook_services_request_preprocess_alter | 6.25 |
hook_schema | 6.25 |
hook_registration_entity_settings | 6.25 |
hook_registration_access | 6.25 |
hook_openlayers_styles | 6.25 |
hook_openlayers_behaviors | 6.25 |
hook_node_page_view | 6.25 |
hook_node_alter | 6.25 |
hook_install | 6.25 |
hook_fivestar_widgets | 6.25 |
hook_field_formatter_info | 6.25 |
hook_entity_update | 6.25 |
hook_entity_insert | 6.25 |
hook_entity_delete | 6.25 |
hook_default_rules_configuration | 6.25 |
hook_ctools_plugin_api | 6.25 |
hook_cron_queue_info | 6.25 |
hook_commerce_checkout_pane_info | 6.25 |
Hook Purposes
1. hook_menu
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7
This is the base for nearly any site with custom functionality. We also tend to include a few pages standard on any site we touch such as a Style Guide. Make sure you understand all of the menu ‘type’ constants available before you do much here. If you use this to introduce any new admin sections, the following snippet will make your /admin/{section} easy.
[php]
// Admin
$items[‘admin/{section}] = array(
‘title’ => {Name},
‘description’ => ‘{Description}.’,
‘position’ => ‘left’,
‘weight’ => -8,
// This is the magic that auto builds for you
‘page callback’ => ‘system_admin_menu_block_page’,
‘access callback’ => ‘_custom_access_{section}_administration’,
// And this is required to reach that function
‘file path’ => ‘modules/system/’,
‘file’ => ‘system.admin.inc’,
);
[/php]
2. hook_permission
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_permission/7
Nearly* every site with a custom section is going to require unique permissions. When you can, stick to the system permissions already in existence to keep things simple to understand (you get node specific permissions automatically!), but this will let you do more crafty controlling.
3. hook_form_{form_id}_alter
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_FORM_ID_alter/7
I love this one, so very much. I have to count it as a single hook for the purposes of percent calculation, but I’m more likely to have 5+ of these in any given project. For those not familiar, this is the upgraded version of hook_form_alter. hook_form_later has to be run on every single form on the site, and that can cost you performance. With only a minor change change you can limit your system’s overhead. It’s absolutely worth the minor amount of effort. Need to find the form_id? Right click the form on the front end, inspect, and check the hidden fields near the top of the