Token upgrade guides
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.
hook_token_infohook_tokens
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
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.