An introduction to Drupal Hooks

Drupal Version:
7

How to use Drupal hooks

What is a Drupal Hook?

In short, a Drupal Hook is a method that allows you to interact with Drupal data (by hooking it). Drupal Hooks are extremely powerful and are one of the main attractions for many people to Drupal. These hooks allow you to view, change and work with various data at specific points of time in Drupal’s processes. For example, you can change data of a form structure before it’s displayed to the user, or you could modify a Node’s data values before it’s saved to the database.

How to create a Drupal Hook

A Drupal hook name is displayed as a function name that starts with the word “hook”. For example, “hook_form_alter” is a very popular hook that is called before a Drupal form is rendered for output.

The word “hook” will be replaced with either the name of your custom module or the name of your theme where your hook will be located.

NOTE: Hooks placed in themes will only function for processes that occur for that theme. For this reason it is recommended to place your hooks into a custom module. This will allow your hook to be called no matter which theme is currently being used.

MORE INFO: For a brief introduction to creating your own custom module see “Creating a Basic Drupal 7 Module”

If we look at the documentation on Drupal.org for hook_form_alter we’ll see it has three arguments:

hook_form_alter(%$form, $%form_state, $form_id)

If you have a custom module called "custom_hooks" then your hook would look like this:

<?php
function custom_hooks_form_alter(%$form, $%form_state, $form_id){
  
// logic goes here
}
?>

Notice that all we did was replace the word “hook” with “custom_hooks” (the name of your module). Now, once your module is enabled, every time a form is displayed on your website you will be able to inspect and alter the form’s data before it is output.

NOTE: If your hook does not appear to be functioning try clearing the site’s caches.

A basic and contrived example of this hook’s usage could like this:

<?php
function custom_hooks_form_alter(&$form, &$form_state, $form_id) {
 
// Check the Form's ID, if it's a Page Node creation form then let's modify it
 
if ($form_id == 'page_node_form'){
   
// Change the Form's title
   
$form['title']['#title'] = 'Enter Page Title for this page';
   
// Give the Page a default value
   
$form['title']['#default_value'] = 'Drupal hooks are awesome.';
  }
}
?>

This little bit of code in your module would modify all Page Node creation forms to give them a new title and default value for the title field. Of course, this could be achieved through the page’s content type configuration but this should give you an idea of how to harness the power of the form_alter hook. This can be used to alter titles, descriptions, default values and even add new fields to any form that is rendered using Drupal’s form API system.

Drupal 7 Custom Hook example hook_form_alter

How to find Drupal Hooks

Now that you we understand what a Drupal Hook is and how to implement it, we just need to know what hooks are available. A good starting place is the listing of Drupal's core hooks which can be found on Drupal.org. To help find a the hooks available to a specific module you can try looking in that module's directory. Most, but now all, modules contain a file called MODULE_NAME.api.php. This file has no functionality and just contains a list of hooks for that module, along with examples of each hooks' usage. All core and most contributed modules should contain this file.

The screenshot below shows the blocks.api.php file contained in the core Blocks Module directory. The hooks shown in this file will allow you to create your own Drupal Blocks or modify existing ones. Like most module.api.php files, this one has well documented examples of the hook's usage.

Drupal 7 blocks api hooks example

Using a module's module.api.php file should list all hooks available to that module and should be a great place to get started if you need to act upon, modify or interact with a module's data. Hooks are extremely powerful, lending to Drupal's flexibility and are well worth understanding their usage.

Example Hook Usages

Follow us for more Drupal tips and articles