On this page
Token
This documentation needs review. See "Help improve this page" in the sidebar.
There are many different modules that work with Drupal's Token API, but the most important one is the Token module. It provides some enhanced functionality for tokens, such as reusable user interfaces for tokens, some missing tokens for specific modules, and crucially, entity field tokens.
For use of tokens beyond what Drupal core offers, the Token module is highly recommended.
Additional documentation for Token module can be found at the Token module handbook.
Token Tree
The Token Tree widget is a user interface element that provides a tree display of all tokens available on the site. In some cases, it even provides information about the tokens available within a specific context, adding additional token types that are relevant to a given input.

An example screenshot of the Token Tree element. Source: https://www.drupal.org/project/token
The Token Tree allows developers and editors to navigate through the tokens available on the site as a hierarchy.
The Name column indicates the name of the token or token type, and the arrow at the left of the row allows token types or chainable tokens to be expanded.
Clicking a token in the Token column will insert the corresponding token into whatever field the Token Tree element was linked to.
The Token Tree element is displayed on demand via Ajax because it can be very large in size, depending on the amount of tokens available on the site and the level of recursion configured in the element.
Tokens added by Token module
Token module adds an array of new tokens that add new token types and add new functionality to existing token types.
Every content entity and entities with token types
Every content entity gains a token type defined by its machine name. This also applies to entities which have a token type defined.
| Token | Token name | Description |
|---|---|---|
[<entity-type>:url] |
URL | The URL of the (entity type). |
[<entity-type>:language] |
Language | The language of the (entity type). |
[<entity-type>:original] |
Original | The original entity, if the entity is being updated or saved. |
Date
These tokens can be chained from any value that returns a date.
A new current-date token type is added that relates to the current date and time. This is subject to change due to drupal.org issue #943028.
| Token | Token name | Description |
|---|---|---|
[date:<format-type>] |
(Date format type) | A date in the format specified by the date format ID. |
Node
These tokens can be chained from any value that returns a node.
[node:type] is removed due to deprecation.
| Token | Token name | Description |
|---|---|---|
[node:source] |
Translation source node | The source node for this current node's translation set. |
[node:log] |
Revision log message | The explanation of the most recent changes made to the node. |
[node:content-type] |
Content type | The content type for this node. Chainable. |
[node:content-type:name] |
Content type name | The label for the content type for this node. |
[node:content-type:machine-name] |
Content type machine name | The machine name for the content type for this node. |
[node:content-type:description] |
Content type description | The description for the content type for this node. |
[node:content-type:node-count] |
Content type node count | The number of nodes belonging to the content type for this node. |
[node:content-type:edit-url] |
Content type edit URL | The URL of the content type's edit page. |
Taxonomy term
These tokens can be chained from any value that returns a taxonomy term.
| Token | Token name | Description |
|---|---|---|
[term:source] |
Translation source term | The source term for this current term's translation set. |
[term:edit-url] |
Edit URL | The URL of the taxonomy term's edit page. |
[term:parents] |
Parents | An array of all the term's parents, starting with the root. |
[term:root] |
Root term | The root term of the taxonomy term. |
Vocabulary
These tokens can be chained from any value that returns a vocabulary.
| Token | Token name | Description |
|---|---|---|
[vocabulary:machine-name] |
Machine-readable name | The unique machine-readable name of the vocabulary. |
[vocabulary:edit-url] |
Edit URL | The URL of the vocabulary's edit page. |
File
These tokens can be chained from any value that returns a file.
| Token | Token name | Description |
|---|---|---|
[file:basename] |
Base name | The base name of the file. |
[file:extension] |
Extension | The extension of the file. |
[file:size-raw] |
File byte size | The size of the file, in bytes. |
User
These tokens can be chained from any value that returns a user.
| Token | Token name | Description |
|---|---|---|
[user:cancel-url] |
Account cancellation URL | The URL of the confirm delete page for the user account. |
[user:one-time-login-url] |
One-time login URL | The URL of the one-time login page for the user account. |
[user:roles] |
Roles | The user roles associated with the user account. |
Current user
| Token | Token name | Description |
|---|---|---|
[current-user:ip-address] |
IP address | The IP address of the current user. |
Menu link
These tokens can be chained from any value that returns a menu link.
| Token | Token name | Description |
|---|---|---|
[menu-link:mlid] |
Link ID | The unique ID of the menu link. |
[menu-link:title] |
Title | The title of the menu link. |
[menu-link:url] |
URL | The URL of the menu link. |
[menu-link:parent] |
Parent | The menu link's parent. |
[menu-link:parents] |
Parents | An array of all the menu link's parents, starting with the root. |
[menu-link:root] |
Root | The menu link's root. |
Language
These tokens can be chained from any value that returns a language.
| Token | Token name | Description |
|---|---|---|
[language:name] |
Language name | The language name. |
[language:langcode] |
Language code | The language code. |
[language:direction] |
Direction | Whether the language is written left-to-right (ltr) or right-to-left (rtl). |
[language:domain] |
Domain | The domain name to use for the language. |
[language:prefix] |
Path prefix | Path prefix for URLs in the language. |
Current page
| Token | Token name | Description |
|---|---|---|
[current-page:title] |
Current page | Tokens related to the current page request. |
[current-page:url] |
Title | The title of the current page. |
[current-page:page-number] |
URL | The URL of the current page. |
[current-page:query] |
Query string value | The page number of the current page when viewing paged lists. |
[current-page:interface-language] |
Interface language | The active user interface language. |
[current-page:content-language] |
Content language | The active content language. |
URL
These tokens can be chained from any value that returns a URL.
| Token | Token name | Description |
|---|---|---|
[url:path] |
Path | The path component of the URL. |
[url:relative] |
Relative URL | The relative URL. |
[url:absolute] |
Absolute URL | The absolute URL. |
[url:brief] |
Brief URL | The URL without the protocol and trailing backslash. |
[url:unaliased] |
Unaliased URL | The unaliased URL. |
[url:args] |
Arguments | The specific argument of the current page. e.g. arg:1 on the page node/1 returns 1. |
Array
These tokens can be chained from any value that returns an array, such as [term:parents] or [menu-link:parents].
| Token | Token name | Description |
|---|---|---|
[array:first] |
First | The first element of the array. |
[array:last] |
Last | The last element of the array. |
[array:count] |
Count | The number of elements in the array. |
[array:reversed] |
Reversed | The array reversed. |
[array:keys] |
Keys | The array of keys of the array. |
[array:join] |
Imploded | The values of the array joined together with a custom string in-between each value. eg. [term:parents:join:,] returns a taxonomy term's parent terms, with their titles separated with a single comma. |
[array:value] |
Value | The specific value of the array. eg. [term:parents:value:1] returns the 2nd value of a taxonomy term's parent terms array, since the array is zero-indexed. |
Random
| Token | Token name | Description |
|---|---|---|
[random:number] |
Number | A random number from 0 to the return value of mt_getrandmax. |
[random:hash:?] |
Hash | A random hash using an algorithm specified in the return value of hash_algos.eg. [random:hash:sha256] returns a random SHA-256 hash. |
Image
Requires Image module to be enabled.
These tokens can be chained from any value that returns an image.
| Token | Token name | Description |
|---|---|---|
[image_with_image_style:mimetype] |
MIME Type | The MIME type (image/png, image/bmp, etc.) of the image. |
[image_with_image_style:filesize] |
File size | The file size of the image. |
[image_with_image_style:height] |
Height | The height the image, in pixels. |
[image_with_image_style:width] |
Width | The width of the image, in pixels. |
[image_with_image_style:uri] |
URI | The URI to the image. |
[image_with_image_style:url] |
URL | The URL to the image. |
How entity field tokens work
Entity field tokens depend on Field module as well as Token module.
Depending on the token value you are trying to fetch, you might have to chain several values together to get to the value you are trying to reach. For example, you might be dealing with nodes and want to print the URL of an image linked through a Media field. You might start with text formatted like so:
The node's Image URL is: [node:field_image:0:entity:field_media_image:entity:url]This traverses through the following steps in Drupal's content hierarchy:
- Starts with the Node itself
- Fetches items in field_image
- Fetches the first field item
- Since field lists are zero-based indexing
- Fetches the Media entity linked from this field item
- Within the Media entity, fetches items in field_media_image
- Fetches the File entity linked from this field item
- Note how we don't specify the index here. We're implicitly selecting the first, but it's important to know that you can explicitly specify the index.
- Generates the URL for the File entity
So in the end, the text returned matches the URL of the image linked through entity references two levels deep.
The output would look something like:
The node's Image URL is: https://my.website.example.com/sites/default/files/2025-04/example-picture.jpgThis is made possible by Token module including its own implementation of hook_tokens that detects deeply-chained tokens and allows recursive calls to Token::generate to be called.
Validating form fields with tokens
A helpful tool provided by the token module is the token_element_validate() callback that works with #element_validate in Form API. This function can be added to any form field that allows tokens and ensures that only valid tokens are provided.
The validator callback performs a number of checks against text inputs:
- If
#min_tokensis specified, expects that the number of tokens present in the text input is equal to or greater than this number. - If
#max_tokensis specified, expects that the number of tokens present in the text input is equal to or less than this number. - If
#token_typesis specified, expects that there aren't any tokens present in the text that can't fulfill the specified types. For example, a[user:]token in a context that does not support them should be considered invalid.
If any of these expectations fail, the form element has an error set against it.
Since token_element_validate() uses \Drupal::token()->scan() to find tokens in the text input, all tokens must be formatted correctly.
It's the responsibility of the developer to know which token types are valid in the context of the form elements they are adding this validator to.
Example Form API element with token_element_validate():
$form['databasics_message'] = [
'#type' => 'textarea',
'#title' => t('Message'),
'#default_value' => \Drupal::config('databasics.settings')->get('message') ?? '',
'#element_validate' => ['token_element_validate'],
'#token_types' => ['user', 'node'],
];Creating a Custom Token
Out of the box, many tokens exist to solve a variety of common use cases for them. Occasionally you may encounter a requirement where no such token exists to facilitate it. Fortunately, implementing custom tokens in Drupal is pretty straightforward.
From Creating a Custom Token in Drupal 8, see also Drupal 8 how to have custom tokens with html in them properly replaced for tips on how to allow HTML.
Here's an example module based on the excellent article by Kevin Quillen.
In this example, the challenge is that "Author" and "Author location" fields can both be empty or not, in which case the labels should not be printed (See it in use at the bottom):
<?php
declare(strict_types = 1);
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function building_custom_token_info() : array {
$info = [];
$info['types']['building_custom'] = [
'name' => t('Building Custom Tokens'),
'description' => t('Custom tokens to solve use-case problems for our website.'),
];
$info['tokens']['building_custom']['image_caption'] = [
'name' => 'Building Author Caption',
'description' => t('This token will return the author and author location. To be used as caption with PhotoSwipe in Views')
];
return $info;
}
/**
* Implements hook_tokens().
*/
function building_custom_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) : array {
$replacements = [];
if ($type == 'building_custom') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'image_caption':
$node = $data['node'];
$text = '';
if ($node->hasField('field_image_author') && !$node->get('field_image_author')->isEmpty()) {
$text .= "<strong>Author</strong>:<br>" . $node->field_image_author->value . "<br>";
}
if ($node->hasField('field_author_location') && !$node->get('field_author_location')->isEmpty()) {
$text .= "<strong>Location</strong>:<br>" . $node->field_author_location->value . "<br>";
}
$replacements[$original] = \Drupal\Core\Render\Markup::create($text);
break;
default:
break;
}
}
}
return $replacements;
}Can be used for example in Views, to format a PhotoSwipe image caption as below. The first three tokens are always available and safe to use, whereas "Author" and "Author location" are not, which is what [building_custom:image_caption] handles:
<h2>[node:field_building]</h2>
<hr>
<h4>[node:field_image_category]</h4>
[node:field_image_tags]
<hr>
[building_custom:image_caption]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