What are Hooks?

Sep 20, 2016 - Posted by: Lauren Lewis
Hooks
As a junior developer ramping up to learning Drupal, I spent a lot of time clicking through the UI. After getting familiar with it, I wanted to take a look behind the scenes at Drupal’s codebase. Writing code for a Drupal site can be an overwhelming experience because, even though it’s written in PHP, there’s a dense API behind it. One of the biggest parts of that API is the hook system. The first exposure I had to writing PHP with Drupal was through update hooks. So I wanted to review how hooks work, and how cool they are to use!

What is a Hook?

Drupal has a lot of excellent Community Documentation, and their page on hooks is thorough. It says:

“Hooks are how modules can interact with the core code of Drupal. They make it possible for a module to define new urls and pages within the site (hook_menu), to add content to pages (hook_block, hook_footer, etc.), to set up custom database tables (hook_schema), and more.

Hooks occur at various points in the thread of execution, where Drupal seeks contributions from all the enabled modules. For example, when a user visits a help page on a Drupal site, as Drupal builds the help page it will give each module a chance to contribute documentation about itself. It does this by scanning all the module code for functions that have the name mymodule_help($path, $arg), where "mymodule" is the module's name, e.g., the block module's help hook is called block_help and the node module's help hook is called node_help. The hook may provide parameters; hook_help's parameters $path and $arg allow the developer to determine what page or pages the help messages will appear on.
A hook can be thought of as an event listener in the sense that an event triggers an action.”

It’s amazing that Drupal is built on this hook system. There are hooks for anything you can think of! You can even alter a form or a whole page. As Drupal builds each page, it’s scanning the module code for the hooks in core and then runs the ones that you wrote. Alan Storm put it succinctly in one of his blog posts:

“When a hook is invoked, Drupal will

  1. Get a list of all installed and enabled modules
  2. Ask each module “Have you implemented the do_something hook”?
  3. If so, then Drupal calls the function in those modules that implement the hook

This way, as a core developer, you can achieve what you want while still letting other programmers “hook into” what you’re doing.”

Update Hooks

The first hook I used was an update hook. The purpose of an update hook is to run code when a database update is triggered. (See the Drupal documentation for function hook_update_N for more information). I’m assuming my reader has worked with Features development (if not, go here).

My goal was to enable a certain contributed module programmatically when I deployed my code to the site (the Olark module). To do this, I had to find an existing feature in my project that was already enabled. A good rule of thumb is to add your code to an existing feature that is related to the code you’re writing. Since Olark is a chat module, I was looking for a feature related to customers or the homepage. I couldn’t find any feature that fit exactly with the module, so I added my code to feature_general. Within the directory of that feature, I found the feature_general.install file, and added my hook:
/** * Enables 'olark' module. */ function feature_general_update_7006() { module_enable(array('olark')); }
After I deploy my code, I trigger a database update. Think back to the three steps that Alan Storm mentioned. Since my feature is already enabled, when the update is triggered, Drupal asks if any update hooks have been invoked. When it sees that there is one in my feature, it knows to read the code in the install file and turn on the Olark module.

It’s also worth noting the naming convention. I have to put the name of my feature and then _update. The number of the hook also matters (see documentation for specifics on how to number your hooks). The hooks only run once, so each hook has to be one greater than the one before.

Writing hooks is a great way for a junior developer to be introduced to PHP in Drupal and learn about how Drupal is built. Once you can conceptualize the inner workings of Drupal, it’s much easier to tackle things like module development in the future.
Lauren Lewis

Lauren joined Zivtech in February 2016 after completing web development training at the Philadelphia chapter of the New York Code + Design Academy. Her six years of professional experience in... Read more

Contact

Send us a message

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.