You are here

function name_tokens in Name Field 7

Implements hook_tokens().

File

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

Code

function name_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $sanitize = !empty($options['sanitize']);
  $replacements = array();

  // This handles the field tokens.
  if (isset($data[$type]) && ($known_tokens = name_token_types_chained($type))) {
    foreach ($tokens as $name => $original) {

      // The RealName module provides the 'name-raw' token.
      if ($name != 'name-raw' && strpos($name, 'name-') === 0) {

        /*
         * We handle a number of different combinations here.
         * token
         * token:[delta|all]
         * token:formatter-FORMAT_NAME
         * token:[delta|all]:formatter-FORMAT_NAME
         * token:component-TYPE
         * token:[delta|all]:component-TYPE
         */
        $parts = explode(':', $name);
        $field_name = array_shift($parts);
        $field_name = str_replace('-', '_', substr($field_name, 5));

        // Ensure that this is actually a real field token before replacing.
        // This will mimimise the chances of false matches like 'name-raw'.
        if (field_info_field($field_name)) {
          $entity_type = str_replace('-', '_', $type);

          // Fix for only known token type to entity type mismatched keys.
          if (in_array($entity_type, array(
            'term',
            'vocabulary',
          ))) {
            $entity_type = 'taxonomy_' . $entity_type;
          }
          $items = field_get_items($entity_type, $data[$type], $field_name);
          if (empty($items)) {
            $replacements[$original] = '';
            continue;
          }

          // Find the delta value.
          $delta = NULL;
          $next = array_shift($parts);
          if (isset($next)) {
            if (is_numeric($next) && (string) intval($next) === (string) $next) {
              $delta = $next;
            }
            elseif ($next == 'all') {
              $delta = 'all';
            }
            else {

              // Return the value to the array for the next step.
              $delta = 0;
              array_unshift($parts, $next);
            }
          }
          else {
            $delta = 0;
          }
          if ($delta != 'all' && !isset($items[$delta])) {
            $replacements[$original] = '';
            continue;
          }

          // Find the token action and format / component.
          $action = NULL;
          $action_key = NULL;
          if ($next = array_shift($parts)) {
            if (strpos($next, 'formatter-') === 0) {
              $action = 'formatter';
              $action_key = substr($next, 10);
            }
            elseif (strpos($next, 'component-') === 0) {
              $action = 'component';
              $action_key = substr($next, 10);
            }
          }
          else {
            $action_key = 'default';
            $action = 'formatter';
          }
          $names = array();
          if ($action == 'formatter') {
            $format = name_get_format_by_machine_name($action_key);
            if (empty($format)) {
              $format = name_get_format_by_machine_name('default');
            }
            if ($delta != 'all') {
              $items = array(
                $items[$delta],
              );
            }
            foreach ($items as $item) {
              $names[] = name_format($item, $format);
            }
          }
          else {
            if ($delta != 'all') {
              $items = array(
                $items[$delta],
              );
            }
            foreach ($items as $item) {
              if (isset($item[$action_key])) {
                $names[] = $item[$action_key];
              }
            }
          }
          $names = implode(', ', array_filter($names));
          $replacements[$original] = $sanitize ? check_plain($names) : $names;
        }
      }
    }
  }
  if ($type == 'user' && !empty($data['user'])) {
    $account = $data['user'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'realname':
          $replacements[$original] = '';
          if (!empty($account->realname)) {
            $replacements[$original] = $sanitize ? check_plain($account->realname) : $account->realname;
          }
          break;
      }
    }
  }
  if ($type == 'name' && !empty($data['name'])) {
    $name = $data['name'];
    $name_components = array();
    foreach (_name_translations() as $key => $title) {
      if (!empty($name[$key])) {
        $name_components[$key] = $name[$key];
      }
      else {
        $name_components[$key] = '';
      }
    }
    foreach ($tokens as $key => $original) {
      if ($key == 'default') {

        // Full default formatted name.
        $default = name_format($name_components, name_settings('default_format'));
        $replacements[$original] = $sanitize ? check_plain($default) : $default;
      }
      elseif (strpos($key, 'formatter-') === 0) {
        list(, $machine_name) = explode('-', $key, 2);
        $format = name_get_format_by_machine_name($machine_name);
        $formated_name = name_format($name_components, $format);
        $replacements[$original] = $sanitize ? check_plain($formated_name) : $formated_name;
      }
      elseif (strpos($key, 'component-') === 0) {
        list(, $component) = explode('-', $key, 2);
        $replacements[$original] = $sanitize ? check_plain($name_components[$component]) : $name_components[$component];
      }
    }
  }
  return $replacements;
}