You are here

function name_token_types_chained in Name Field 7

Defines a list of token types that can be chained with the name field.

Return value

If an entity (token) type is given, returns the chained sub-list.

2 calls to name_token_types_chained()
name_tokens in ./name.tokens.inc
Implements hook_tokens().
name_token_info in ./name.tokens.inc
Implements hook_token_info().

File

./name.tokens.inc, line 246
Builds placeholder replacement tokens for country-related data.

Code

function name_token_types_chained($type = NULL, $reset = FALSE) {

  // This functions gets called rather often when replacing tokens.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['types'] =& drupal_static(__FUNCTION__);
  }
  $types =& $drupal_static_fast['types'];
  if (!isset($types) || $reset) {

    // Note that this hook contains translated strings, so each language is
    // cached separately.
    global $language;
    $langcode = $language->language;
    if (!$reset && ($cache = cache_get("name_token_types_chained:{$langcode}", 'cache'))) {
      $types = $cache->data;
    }
    if (!$types) {
      $types = array();
      foreach (field_info_field_map() as $field_name => $info) {
        if ($info['type'] == 'name') {
          foreach ($info['bundles'] as $entity_type => $bundles) {

            // Name field. Also known as Client.
            $labels = array();
            foreach ($bundles as $bundle) {
              $instance = field_info_instance($entity_type, $field_name, $bundle);
              $labels[$instance['label']] = $instance['label'];
            }
            $label = array_shift($labels);
            $clean = str_replace('_', '-', $field_name);
            if (empty($labels)) {
              $description = t('Name field in the default format. To specify a delta value, use "@token:0". Append the other chained options after the delta value like this, "@token:0:component-given". Replace the delta value with all to obtain all items in the field like this "@token:all".', array(
                '@token' => $clean,
              ));
            }
            else {
              $description = t('Name field in the default format. Also known as %labels', array(
                '%labels' => implode(', ', $labels),
              ));
            }

            // Make sure we get the correct token type.
            $entity_info = entity_get_info($entity_type);
            $token_type = isset($entity_info['token type']) ? $entity_info['token type'] : $entity_type;
            $types[$token_type]['name-' . $clean] = array(
              'name' => check_plain($label),
              'description' => $description,
              'type' => 'name-field',
            );
          }
        }
      }
      cache_set("name_token_types_chained:{$langcode}", $types);
    }
  }
  if (isset($type)) {
    return isset($types[$type]) ? $types[$type] : NULL;
  }
  return $types;
}