8.5.x token.api.php hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.0.x token.api.php hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.1.x token.api.php hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.2.x token.api.php hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.3.x token.api.php hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.4.x token.api.php hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.6.x token.api.php hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
7.x system.api.php hook_tokens($type, $tokens, array $data = array(), array $options = array())

Provide replacement values for placeholder tokens.

This hook is invoked when someone calls token_replace(). That function first scans the text for [type:token] patterns, and splits the needed tokens into groups by type. Then hook_tokens() is invoked on each token-type group, allowing your module to respond by providing replacement text for any of the tokens in the group that your module knows how to process.

A module implementing this hook should also implement hook_token_info() in order to list its available tokens on editing screens.

Parameters

$type: The machine-readable name of the type (group) of token being replaced, such as 'node', 'user', or another type defined by a hook_token_info() implementation.

$tokens: An array of tokens to be replaced. The keys are the machine-readable token names, and the values are the raw [type:token] strings that appeared in the original text.

$data: (optional) An associative array of data objects to be used when generating replacement values, as supplied in the $data parameter to token_replace().

$options: (optional) An associative array of options for token replacement; see token_replace() for possible values.

Return value

An associative array of replacement values, keyed by the raw [type:token] strings from the original text.

See also

hook_token_info()

hook_tokens_alter()

Related topics

8 functions implement hook_tokens()

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

comment_tokens in modules/comment/comment.tokens.inc
Implements hook_tokens().
node_tokens in modules/node/node.tokens.inc
Implements hook_tokens().
poll_tokens in modules/poll/poll.tokens.inc
Implements hook_tokens().
statistics_tokens in modules/statistics/statistics.tokens.inc
Implements hook_tokens().
system_tokens in modules/system/system.tokens.inc
Implements hook_tokens().

... See full list

1 invocation of hook_tokens()
token_generate in includes/token.inc
Generates replacement values for a list of tokens.

File

modules/system/system.api.php, line 4425
Hooks provided by Drupal core and the System module.

Code

function hook_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      switch ($name) {

        // Simple key values on the node.
        case 'nid':
          $replacements[$original] = $node->nid;
          break;
        case 'title':
          $replacements[$original] = $sanitize ? check_plain($node->title) : $node->title;
          break;
        case 'edit-url':
          $replacements[$original] = url('node/' . $node->nid . '/edit', $url_options);
          break;

          // Default values for the chained tokens handled below.

        // Default values for the chained tokens handled below.
        case 'author':
          $name = $node->uid == 0 ? variable_get('anonymous', t('Anonymous')) : $node->name;
          $replacements[$original] = $sanitize ? filter_xss($name) : $name;
          break;
        case 'created':
          $replacements[$original] = format_date($node->created, 'medium', '', NULL, $language_code);
          break;
      }
    }
    if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
      $author = user_load($node->uid);
      $replacements += token_generate('user', $author_tokens, array(
        'user' => $author,
      ), $options);
    }
    if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
      $replacements += token_generate('date', $created_tokens, array(
        'date' => $node->created,
      ), $options);
    }
  }
  return $replacements;
}

Comments

drugget’s picture

Easy example, how to create a new node token
(found here: http://web-tricks.org/content/drupal-7-how-create-custom-node-token):

function MYMODULE_token_info() {
  $info['tokens']['node']['title_first_letter'] = array(
    'name' => t('Title first letter'),
    'description' => t('Generates first letter of node title')
  );
  return $info;
}

function slideaway_currency_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'title_first_letter':
          $replacements[$original] = substr($node->title, 0, 1); // get first letter of the title here 
          break;
      }
    }
  }
  return $replacements;
}