You are here

function token_token_info_alter in Token 8

Same name and namespace in other branches
  1. 7 token.tokens.inc \token_token_info_alter()

Implements hook_token_info_alter().

File

./token.tokens.inc, line 35
Token callbacks for the token module.

Code

function token_token_info_alter(&$info) {

  // Force 'date' type tokens to require input and add a 'current-date' type.
  // @todo Remove when http://drupal.org/node/943028 is fixed.
  $info['types']['date']['needs-data'] = 'date';
  $info['types']['current-date'] = [
    'name' => t('Current date'),
    'description' => t('Tokens related to the current date and time.'),
    'type' => 'date',
  ];

  // Add a 'dynamic' key to any tokens that have chained but dynamic tokens.
  $info['tokens']['date']['custom']['dynamic'] = TRUE;

  // Remove deprecated tokens from being listed.
  unset($info['tokens']['node']['tnid']);
  unset($info['tokens']['node']['type']);
  unset($info['tokens']['node']['type-name']);

  // Support 'url' type tokens for core tokens.
  if (isset($info['tokens']['comment']['url']) && \Drupal::moduleHandler()
    ->moduleExists('comment')) {
    $info['tokens']['comment']['url']['type'] = 'url';
  }
  if (isset($info['tokens']['node']['url']) && \Drupal::moduleHandler()
    ->moduleExists('node')) {
    $info['tokens']['node']['url']['type'] = 'url';
  }
  if (isset($info['tokens']['term']['url']) && \Drupal::moduleHandler()
    ->moduleExists('taxonomy')) {
    $info['tokens']['term']['url']['type'] = 'url';
  }
  $info['tokens']['user']['url']['type'] = 'url';

  // Add [token:url] tokens for any URI-able entities.
  $entities = \Drupal::entityTypeManager()
    ->getDefinitions();
  foreach ($entities as $entity_info) {

    // Do not generate tokens if the entity doesn't define a token type or is
    // not a content entity.
    if (!$entity_info
      ->get('token_type') || !$entity_info instanceof ContentEntityTypeInterface) {
      continue;
    }
    $token_type = $entity_info
      ->get('token_type');
    if (!isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {

      // Define tokens for entity type's without their own integration.
      $info['types'][$entity_info
        ->id()] = [
        'name' => $entity_info
          ->getLabel(),
        'needs-data' => $entity_info
          ->id(),
        'module' => 'token',
      ];
    }

    // Add [entity:url] tokens if they do not already exist.
    // @todo Support entity:label
    if (!isset($info['tokens'][$token_type]['url'])) {
      $info['tokens'][$token_type]['url'] = [
        'name' => t('URL'),
        'description' => t('The URL of the @entity.', [
          '@entity' => mb_strtolower($entity_info
            ->getLabel()),
        ]),
        'module' => 'token',
        'type' => 'url',
      ];
    }

    // Add [entity:language] tokens if they do not already exist.
    if (!isset($info['tokens'][$token_type]['language'])) {
      $info['tokens'][$token_type]['language'] = [
        'name' => t('Language'),
        'description' => t('The language of the @entity.', [
          '@entity' => mb_strtolower($entity_info
            ->getLabel()),
        ]),
        'module' => 'token',
        'type' => 'language',
      ];
    }

    // Add [entity:original] tokens if they do not already exist.
    if (!isset($info['tokens'][$token_type]['original'])) {
      $info['tokens'][$token_type]['original'] = [
        'name' => t('Original @entity', [
          '@entity' => mb_strtolower($entity_info
            ->getLabel()),
        ]),
        'description' => t('The original @entity data if the @entity is being updated or saved.', [
          '@entity' => mb_strtolower($entity_info
            ->getLabel()),
        ]),
        'module' => 'token',
        'type' => $token_type,
      ];
    }
  }

  // Add support for custom date formats.
  // @todo Remove when http://drupal.org/node/1173706 is fixed.
  $date_format_types = \Drupal::entityTypeManager()
    ->getStorage('date_format')
    ->loadMultiple();
  foreach ($date_format_types as $date_format_type => $date_format_type_info) {

    /* @var \Drupal\system\Entity\DateFormat $date_format_type_info */
    if (!isset($info['tokens']['date'][$date_format_type])) {
      $info['tokens']['date'][$date_format_type] = [
        'name' => Html::escape($date_format_type_info
          ->label()),
        'description' => t("A date in '@type' format. (%date)", [
          '@type' => $date_format_type,
          '%date' => \Drupal::service('date.formatter')
            ->format(\Drupal::time()
            ->getRequestTime(), $date_format_type),
        ]),
        'module' => 'token',
      ];
    }
  }
}