Token
Provides a user interface for browsing available tokens and adds missing core tokens to Drupal's Token API.
token
Install
composer require 'drupal/token:8.x-1.16'
Overview
The Token module extends Drupal's core token system in two significant ways: it provides a comprehensive user interface for browsing and inserting tokens, and it adds many useful tokens that are missing from Drupal core.
The module includes an interactive token tree browser that displays all available tokens organized by type in an expandable hierarchy. This browser supports click-to-insert functionality, allowing users to click on any token to automatically insert it into text fields or WYSIWYG editors including CKEditor 5, TinyMCE, and CodeMirror.
Additionally, the module provides tokens for current page information, current date and time, URLs, arrays, random values, content types, menu links, menus, books, taxonomy enhancements, file metadata, user roles, and more. It also adds field-based tokens for all entity fields and integrates with the Devel module to show actual token values for any entity.
Features
- Interactive token tree browser with expandable/collapsible hierarchy for discovering available tokens
- Click-to-insert functionality supporting multiple editors: CKEditor 5, CKEditor, TinyMCE, CodeMirror, and native textareas
- Current page tokens: title, URL, page number, query string parameters, interface language, content language
- Current date tokens with support for all configured date formats
- Current user IP address token
- URL tokens: path, relative URL, absolute URL, brief URL, unaliased URL, URL arguments
- Array tokens for working with multi-value data: first, last, count, reversed, keys, join with separator, specific value access
- Random tokens: random numbers and configurable hash algorithms
- Node enhancements: translation source, revision log, content type, book page, menu link
- Content type tokens: name, machine name, description, node count, edit URL
- Menu link tokens: ID, title, URL, parent, parents array, root, menu, edit URL
- Menu tokens: name, machine name, description, link count, edit URL
- Taxonomy enhancements: translation source, edit URL, parents array, root term, vocabulary machine name
- File tokens: basename, extension, raw size in bytes
- User tokens: cancel URL, one-time login URL, user roles array
- Book tokens: title, author, root page, parent page, parents array
- Image style tokens: MIME type, file size, dimensions, URI, URL
- Language tokens: name, langcode, direction, domain, path prefix
- Entity type to token type mapping service for module developers
- Devel module integration showing actual token values on entity pages
- Drush cache clear command for token cache
- Automatic field tokens for all entity reference, text, and other field types
Use Cases
Email Template Personalization
Use the token browser to discover and insert tokens like [current-user:display-name], [site:name], and [current-date:long] into email templates. The click-to-insert feature makes it easy to add tokens without memorizing their exact syntax.
URL Alias Patterns with Pathauto
When configuring Pathauto URL patterns, use the token browser to find the right tokens for content type-specific paths. For example, use [node:content-type:machine-name]/[node:title] to create organized URL structures.
Automated Content Generation
Leverage tokens like [current-page:title], [current-page:url:absolute], and [current-user:mail] in automated workflows, notifications, or dynamic content blocks.
Developer Token Discovery
Use the Devel module integration to view actual token values on any entity page. Navigate to a node, click the Devel tab, then Tokens to see all available tokens and their resolved values for that specific content.
Multi-value Field Token Processing
Use array tokens to work with multi-value fields: [node:field_tags:first] for the first value, [node:field_tags:join:, ] to join all values with commas, or [node:field_tags:count] to display the number of items.
Breadcrumb and Menu Integration
Access menu hierarchy using menu-link tokens: [node:menu-link:parents:join: > ] creates a breadcrumb-style path, [node:menu-link:root:title] gets the top-level menu item title.
Random Content Generation
Use random tokens for generating unique identifiers or cache-busting: [random:hash:md5] creates a random MD5 hash, [random:number] generates a random integer.
Multilingual Token Handling
Access language information with [current-page:interface-language:name] for the current UI language or [current-page:content-language:langcode] for content language, useful in multilingual site configurations.
Tips
- Use the Devel integration to quickly discover which tokens are available for a specific piece of content - just enable Devel and visit any entity's Tokens tab.
- The token browser's click-to-insert feature works with most WYSIWYG editors - just ensure the text field has focus before clicking.
- Array tokens are powerful for multi-value fields: use :first, :last, :count, :join:{separator}, or :value:{key} to access specific values.
- Token caching improves performance significantly - remember to clear the token cache when developing custom tokens.
- The tree builder supports a maximum depth of 9 levels to prevent infinite recursion with self-referential token types.
- For security-sensitive tokens like one-time login URLs, look for 'restricted' tokens that are hidden by default in the browser.
- Use [current-page:query:{key}] to access URL query parameters dynamically in your token replacements.
Technical Details
Admin Pages 2
/token/tree
AJAX endpoint that displays an interactive tree of all available tokens. This page is typically accessed via AJAX calls from token browser dialogs embedded in forms throughout Drupal. It shows tokens organized by type in an expandable hierarchy with name, token pattern, and description columns. Supports filtering by specific token types via query parameters.
/token/flush-cache
Administrative endpoint to clear the token information cache. Requires the 'flush caches' permission. Useful when custom tokens are added or modified and need to be refreshed.
Hooks 3
hook_token_info
Defines available token types and tokens. The Token module implements this hook to add many tokens missing from Drupal core including current-page, current-date, array, random, url, menu-link, content-type, and more.
hook_tokens
Generates replacement values for tokens. The Token module implements comprehensive token generation for all the token types it defines, handling entity references, dates, URLs, arrays, and more.
hook_token_info_alter
Alters token type and token definitions. Can be used to modify, add, or remove tokens defined by other modules.
Drush Commands 1
drush cache:clear token
Clears the token information cache. Useful after adding custom tokens or when token definitions have changed.
Troubleshooting 5
Ensure the Token module is enabled and the text format being used supports token replacement. Some modules require specific configuration to enable token processing. Clear the token cache with 'drush cc token' if custom tokens were recently added.
Clear the token cache at /token/flush-cache or via 'drush cc token'. Verify that modules providing the expected tokens are enabled. Check if the tokens are marked as 'restricted' which hides them by default.
Ensure the editor has focus before clicking a token. The Token module supports CKEditor 5, CKEditor, TinyMCE, and CodeMirror. If using a different editor, tokens may need to be copied manually.
The Devel module must be installed and enabled. The user must have 'access devel information' permission. The entity type must have a 'token-devel' link template defined.
Clear the token cache after defining new tokens. Ensure hook_token_info() returns properly structured array. Check for PHP errors in the token definition. Verify the module providing tokens is enabled.
Security Notes 4
- Some tokens are marked as 'restricted' and hidden from the token browser by default because they contain sensitive information (like one-time login URLs or account cancellation links).
- The /token/flush-cache endpoint requires the 'flush caches' permission to prevent unauthorized cache clearing.
- The token tree AJAX endpoint requires a valid CSRF token to prevent cross-site request forgery attacks.
- The Devel tokens tab requires 'access devel information' permission and should only be enabled in development environments.