Same name and namespace in other branches
  1. 7.x modules/system/system.api.php \hook_token_info()
  2. 8.9.x core/lib/Drupal/Core/Utility/token.api.php \hook_token_info()
  3. 9 core/lib/Drupal/Core/Utility/token.api.php \hook_token_info()

Provide information about available placeholder tokens and token types.

Tokens are placeholders that can be put into text by using the syntax [type:token], where type is the machine-readable name of a token type, and token is the machine-readable name of a token within this group. This hook provides a list of types and tokens to be displayed on text editing screens, so that people editing text can see what their token options are.

The actual token replacement is done by \Drupal\Core\Utility\Token::replace(), which invokes hook_tokens(). Your module will need to implement that hook in order to generate token replacements from the tokens defined here.

Return value

array An associative array of available tokens and token types. The outer array has two components:

  • types: An associative array of token types (groups). Each token type is an associative array with the following components:

    • name: The translated human-readable short name of the token type.
    • description (optional): A translated longer description of the token type.
    • needs-data: The type of data that must be provided to \Drupal\Core\Utility\Token::replace() in the $data argument (i.e., the key name in $data) in order for tokens of this type to be used in the $text being processed. For instance, if the token needs a node object, 'needs-data' should be 'node', and to use this token in \Drupal\Core\Utility\Token::replace(), the caller needs to supply a node object as $data['node']. Some token data can also be supplied indirectly; for instance, a node object in $data supplies a user object (the author of the node), allowing user tokens to be used when only a node data object is supplied.
  • tokens: An associative array of tokens. The outer array is keyed by the group name (the same key as in the types array). Within each group of tokens, each token item is keyed by the machine name of the token, and each token item has the following components:

    • name: The translated human-readable short name of the token.
    • description (optional): A translated longer description of the token.
    • type (optional): A 'needs-data' data type supplied by this token, which should match a 'needs-data' value from another token type. For example, the node author token provides a user object, which can then be used for token replacement data in \Drupal\Core\Utility\Token::replace() without having to supply a separate user object.

See also

hook_token_info_alter()

hook_tokens()

Related topics

9 functions implement hook_token_info()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

comment_token_info in core/modules/comment/comment.tokens.inc
Implements hook_token_info().
file_token_info in core/modules/file/file.module
Implements hook_token_info().
locale_test_token_info in core/modules/locale/tests/modules/locale_test/locale_test.module
Implements hook_token_info().
node_token_info in core/modules/node/node.tokens.inc
Implements hook_token_info().
statistics_token_info in core/modules/statistics/statistics.tokens.inc
Implements hook_token_info().

... See full list

1 invocation of hook_token_info()
Token::getInfo in core/lib/Drupal/Core/Utility/Token.php
Returns metadata describing supported tokens.

File

core/lib/Drupal/Core/Utility/token.api.php, line 211
Hooks related to the Token system.

Code

function hook_token_info() {
  $type = [
    'name' => t('Nodes'),
    'description' => t('Tokens related to individual nodes.'),
    'needs-data' => 'node',
  ];

  // Core tokens for nodes.
  $node['nid'] = [
    'name' => t("Node ID"),
    'description' => t("The unique ID of the node."),
  ];
  $node['title'] = [
    'name' => t("Title"),
  ];
  $node['edit-url'] = [
    'name' => t("Edit URL"),
    'description' => t("The URL of the node's edit page."),
  ];

  // Chained tokens for nodes.
  $node['created'] = [
    'name' => t("Date created"),
    'type' => 'date',
  ];
  $node['author'] = [
    'name' => t("Author"),
    'type' => 'user',
  ];
  return [
    'types' => [
      'node' => $type,
    ],
    'tokens' => [
      'node' => $node,
    ],
  ];
}