Token upgrade guides

Last updated on
20 April 2025

This documentation needs review. See "Help improve this page" in the sidebar.

Object-oriented hook system (Drupal 11.1+)

Since Drupal 11.1, hooks can be implemented as classes that use the attribute Drupal\Core\Hook\Attribute\Hook to denote specific methods as belonging to specific hooks.

Instead of implementing hook_token_info or hook_tokens, you would create a new PHP class, add functions that implement hook_token_info or hook_tokens  within the class, and set the #[Hook('token_info')] or #[Hook('tokens')] attribute before your function declaration.

Some examples of this usage have been provided in the guides for implementing hook_token_info and hook_tokens.

Drupal 7 to 8+

Updating hook_token_info and hook_tokens for Drupal 8+ mainly involves allowing type declarations for arguments and return values, and adding $bubbleable_metadata for hook_tokens.

Type declarations for arguments and return values is well-supported in newer PHP versions and is highly recommended to ensure functions are being called safely.

Bubbleable metadata is required in some cases to pass cache metadata to the top-level render method, which is critical if rendering a token that may involve something that might differ due to certain caching rules, such as URLs.

For more information, consult api.drupal.org's documentation on these two hooks and compare their example implementations from version to version.

Drupal 6 to 7 upgrade guide

A comprehensive guide for updating Token API from Drupal 6 to Drupal 7 can be found at https://www.drupal.org/documentation/modules/token/update/6/7

Help improve this page

Page status: Needs review

You can: