You are here

function entity_token_token_info_alter in Entity API 7

Implements hook_token_info_alter().

File

./entity_token.tokens.inc, line 82
Provides tokens for entity properties which have no token yet.

Code

function entity_token_token_info_alter(&$info) {
  $entity_info = entity_get_info();
  $token_types = entity_token_types_chained();

  // Loop over all chain-able token types, as those may contain further tokens,
  // e.g. entity types or 'site'.
  foreach ($token_types as $token_type => $type) {

    // Just add all properties regardless whether it's in a bundle, but only if
    // there is no token of the property yet.
    foreach (entity_get_all_property_info($type) as $name => $property) {
      $name = str_replace('_', '-', $name);
      $property += array(
        'type' => 'text',
        'description' => $property['label'],
      );
      $property_token_type = _entity_token_map_to_token_type($property);
      if (!isset($info['tokens'][$token_type][$name]) && $property_token_type) {
        $info['tokens'][$token_type][$name] = array(
          'name' => $property['label'],
          'description' => $property['description'],
          'type' => $property_token_type,
          // Mark the token so we know we have to provide the value afterwards.
          'entity-token' => TRUE,
        );
      }
      if ($property_token_type == 'struct' && !empty($property['property info'])) {
        $info['tokens'][$token_type][$name]['dynamic'] = TRUE;
        $help = array();
        foreach ($property['property info'] as $key => $property_info) {
          $help[] = $key . ' (' . $property_info['label'] . ')';
        }
        $info['tokens'][$token_type][$name]['description'] .= ' ' . t('The following properties may be appended to the token: @keys', array(
          '@keys' => implode(', ', $help),
        ));
      }
    }
  }

  // Make sure all chain-able token types we support are registered.
  foreach ($token_types as $token_type => $type) {
    if (!empty($info['tokens'][$token_type]) && !isset($info['types'][$token_type])) {
      if (isset($entity_info[$type])) {
        $info['types'][$token_type] = array(
          'name' => $entity_info[$type]['label'],
          'description' => t('Tokens related to the "@name" entities.', array(
            '@name' => $entity_info[$type]['label'],
          )),
          'needs-data' => $token_type,
        );
      }
      else {
        $info['types'][$token_type] = array(
          'name' => drupal_strtoupper($token_type),
          'description' => t('@name tokens.', array(
            '@name' => drupal_strtoupper($token_type),
          )),
          'needs-data' => $token_type,
        );
      }
    }
    if (!empty($info['tokens'][$token_type]) && !isset($info['types']["list<{$token_type}>"]) && $token_type != 'site') {
      if (isset($entity_info[$type])) {
        $info['types']["list<{$token_type}>"] = array(
          'name' => t('List of @entities', array(
            '@entities' => isset($entity_info[$type]['plural label']) ? $entity_info[$type]['plural label'] : $entity_info[$type]['label'] . 's',
          )),
          'description' => t('Tokens related to the "@name" entities.', array(
            '@name' => $entity_info[$type]['label'],
          )),
          'needs-data' => "list<{$token_type}>",
        );
      }
      else {
        $info['types']["list<{$token_type}>"] = array(
          'name' => t('List of @type values', array(
            '@type' => $token_type,
          )),
          'description' => t('Tokens for lists of @type values.', array(
            '@type' => $token_type,
          )),
          'needs-data' => "list<{$token_type}>",
        );
      }

      // Also add some basic token replacements for lists...
      for ($i = 0; $i < 4; $i++) {
        $info['tokens']["list<{$token_type}>"][$i] = array(
          'name' => t('@type with delta @delta', array(
            '@delta' => $i,
            '@type' => $info['types'][$token_type]['name'],
          )),
          'description' => t('The list item with delta @delta. Delta values start from 0 and are incremented by one per list item.', array(
            '@delta' => $i,
          )),
          'type' => $token_type,
        );
      }
    }
  }
}