1 field.tokens.inc field_token_info_alter(&$info)

Implements hook_token_info_alter().

We use hook_token_info_alter() rather than hook_token_info() as other modules may already have defined some field tokens.

File

core/modules/field/field.tokens.inc, line 13
Builds placeholder replacement tokens for field-related data.

Code

function field_token_info_alter(&$info) {
  $fields = _field_token_info();

  // Attach field tokens to their respective entity tokens.
  foreach ($fields as $field_name => $field) {
    foreach (array_keys($field['bundles']) as $token_type) {
      // If a token already exists for this field, then don't add it.
      if (isset($info['tokens'][$token_type][$field_name])) {
        continue;
      }

      // Ensure both tokens and token types exist.
      if (!isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {
        continue;
      }

      // Comment module provides the comment field as [comment:body].
      if ($token_type == 'comment' && $field_name == 'comment_body') {
        continue;
      }

      $info['tokens'][$token_type][$field_name] = array(
        // Note that label and description have already been sanitized by _field_token_info().
        'name' => $field['label'],
        'description' => $field['description'],
      );
    }
  }
}