You are here

function token_token_info_alter in Token 7

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

Implements hook_token_info_alter().

File

./token.tokens.inc, line 11
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'] = array(
    '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;

  // The [file:size] may not always return in kilobytes.
  // @todo Remove when http://drupal.org/node/1193044 is fixed.
  $info['tokens']['file']['size']['description'] = t('The size of the file.');

  // 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']) && module_exists('comment')) {
    $info['tokens']['comment']['url']['type'] = 'url';
  }
  $info['tokens']['node']['url']['type'] = 'url';
  if (isset($info['tokens']['term']['url']) && module_exists('taxonomy')) {
    $info['tokens']['term']['url']['type'] = 'url';
  }
  $info['tokens']['user']['url']['type'] = 'url';

  // Add [token:url] tokens for any URI-able entities.
  $entities = entity_get_info();
  foreach ($entities as $entity => $entity_info) {
    if (!isset($entity_info['token type'])) {
      continue;
    }
    $token_type = $entity_info['token type'];
    if (!isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {
      continue;
    }

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

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

  // Add support for custom date formats.
  // @todo Remove when http://drupal.org/node/1173706 is fixed.
  $date_format_types = system_get_date_types();
  foreach ($date_format_types as $date_format_type => $date_format_type_info) {
    if (!isset($info['tokens']['date'][$date_format_type])) {
      $info['tokens']['date'][$date_format_type] = array(
        'name' => check_plain($date_format_type_info['title']),
        'description' => t("A date in '@type' format. (%date)", array(
          '@type' => $date_format_type,
          '%date' => format_date(REQUEST_TIME, $date_format_type),
        )),
        'module' => 'token',
      );
    }
  }
}