You are here

function _token_field_info in Token 7

Fetch an array of field data used for tokens.

1 call to _token_field_info()
field_token_info_alter in ./token.tokens.inc
Implements hook_token_info_alter() on behalf of field.module.
1 string reference to '_token_field_info'
token_clear_cache in ./token.module
Clear token caches and static variables.

File

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

Code

function _token_field_info($field_name = NULL) {
  $info =& drupal_static(__FUNCTION__);
  if (!isset($fields)) {
    if ($cached = cache_get('field:info', 'cache_token')) {
      $info = $cached->data;
    }
    else {
      $info = array();
      $fields = field_info_fields();
      $instances = field_info_instances();
      $type_info = field_info_field_types();
      $entity_info = entity_get_info();
      foreach ($fields as $field) {
        $key = $field['field_name'];
        if (!empty($field['bundles'])) {
          foreach (array_keys($field['bundles']) as $entity) {

            // Make sure a token type exists for this entity.
            $token_type = token_get_entity_mapping('entity', $entity);
            if (empty($token_type)) {
              continue;
            }
            $info[$key]['token types'][] = $token_type;
            $info[$key] += array(
              'labels' => array(),
              'bundles' => array(),
            );

            // Find which label is most commonly used.
            foreach ($field['bundles'][$entity] as $bundle) {

              // Field information will included fields attached to disabled
              // bundles, so check that the bundle exists before provided a
              // token for it.
              // @see http://drupal.org/node/1252566
              if (!isset($entity_info[$entity]['bundles'][$bundle])) {
                continue;
              }
              $info[$key]['labels'][] = $instances[$entity][$bundle][$key]['label'];
              $info[$key]['bundles'][$token_type][$bundle] = $entity_info[$entity]['bundles'][$bundle]['label'];
            }
          }
        }
        if (isset($info[$key])) {
          $labels = array_count_values($info[$key]['labels']);
          arsort($labels);
          $info[$key]['label'] = check_plain(key($labels));

          // Generate a description for the token.
          $info[$key]['description'] = t('@type field.', array(
            '@type' => $type_info[$field['type']]['label'],
          ));
          if ($also_known_as = array_unique(array_diff($info[$key]['labels'], array(
            $info[$key]['label'],
          )))) {
            $info[$key]['description'] .= ' ' . t('Also known as %labels.', array(
              '%labels' => implode(', ', $also_known_as),
            ));
          }
        }
      }
      drupal_alter('token_field_info', $info);
      cache_set('field:info', $info, 'cache_token');
    }
  }
  if (isset($field_name)) {
    return isset($info[$field_name]) ? $info[$field_name] : FALSE;
  }
  return $info;
}