Introduction to Drupal hook update


3 minutes read
Published On
Tuesday, Apr 25, 2023

DrupalCon-lille-2023-blog-header-image

Introduction to Drupal Hook Update

Little Background

Drupal provides and update api to make changes to the stored data, whenever you make changes in your module or theme that is going to make your site’s exsisting data incompatible with the latest change you have made in the module or theme, there comes a need for Drupal update api.

This api provides you with a way to make changes to the exsisting data stored so that you sites data is compatible with the latest version/change you made in you module/theme.

Where to write this hook update

The hook update is written in the module’s or theme’ module_name.install file.

How to write this hook update & its parameters

In the module_name.install file we need to write the hook_update_N() function and all the update code needs to go in this function.

Wait thats not all, lets look at the naming convention and component of writing the hook_update_N()

So the hook_update_N() consist of three parts separated by underscore(_) symbol more on this is as described below:

  • hook : This is part is to be update with the name of the module like example-module_update_N(); this is similar to every other hooks

  • update : This is the keyword used to identify the hook. example-module_update_N()

  • N (Capital N) : This part is specific to hook_update_N(). It is usually a 4 digit number like 8001 or 9001 This number is formed as below

    • 1st digit denotes the drupal core version for eg 8, 9, 10
    • 2nd digit denotes the modules majors version For instance, if you’re developing for Drupal Core and its version 8.0.x, use 0, and if its version 8.1.x, use 1, etc. If you’re in a contributed or custom module, and its version 8.x-1.x, use 1, etc.
    • last two digits for sequential incremental counting start with 01

Parameters available in hook_update_N()

array $sandbox: Stores information for batch updates.

If your hook is going to use a batch update using drupal’s batch api then &$sandbox parameter is available and passed to the hook. More on sandbox variable.

So for example below is the exact code showing the implementation of the hook update for a sample module named example_module

/**
 * Write a line or two here about what the updates are for.
 * This is shown to users on the update.php page.
 */
function mymodule_update_8001(&$sandbox) {

    *update code goes here 
*
}

Please give us your feedback on our articles and if you find them helpful, click the thumbs up icon to motivate us to write more.

How We Can Help You With Your Next Project?

Subscribe to Our Newsletter

Because staying informed is the first step