You are here

function phone_tokens in Phone 7.2

Implements hook_tokens().

File

./phone.tokens.inc, line 94
Provides token integration for the phone module.

Code

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

  // This handles the field tokens.
  if (isset($data[$type]) && phone_token_types_chained($type)) {
    foreach ($tokens as $name => $original) {
      if (strpos($name, 'phone-') === 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, 6));

        // Ensure that this is actually a real field token before replacing.
        if ($field = field_info_field($field_name)) {
          $items = field_get_items($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;
          $no_extension = FALSE;
          $alpha = FALSE;
          if ($next = array_shift($parts)) {
            if (strpos($next, 'formatter-alpha-no-ext-') === 0) {
              $action = 'formatter';
              $action_key = substr($next, 24);
              $no_extension = TRUE;
              $alpha = TRUE;
            }
            if (strpos($next, 'formatter-alpha-') === 0) {
              $action = 'formatter';
              $action_key = substr($next, 16);
              $alpha = TRUE;
            }
            if (strpos($next, 'formatter-no-ext-') === 0) {
              $action = 'formatter';
              $action_key = substr($next, 17);
              $no_extension = TRUE;
            }
            elseif (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 = 'phone_international';
            $action = 'formatter';
          }
          $formatted = array();
          if ($action == 'formatter') {
            if ($delta != 'all') {
              $items = array(
                $items[$delta],
              );
            }
            foreach ($items as $item) {

              // Load libphonenumber.
              phone_libphonenumber();
              $number = $item['number'];
              $countrycode = $item['countrycode'];
              $extension = $no_extension ? '' : $item['extension'];
              $format = str_replace('-', '_', $action_key);
              $formatted[] = phone_libphonenumber_format($number, $countrycode, $extension, $format, $alpha);
            }
          }
          else {
            if ($delta != 'all') {
              $items = array(
                $items[$delta],
              );
            }
            foreach ($items as $item) {
              switch ($action_key) {
                case 'countryname':
                case 'callingcode':
                  if (isset($item['countrycode'])) {
                    $formatted[] = phone_countries($item['countrycode'], $action_key == 'countryname' ? 'country' : 'calling_code');
                  }
                  break;
                case 'numbertypelabel':
                  if (isset($item['numbertype'])) {
                    $entity_info = entity_get_info($type);
                    $key = $entity_info['bundle keys']['bundle'];
                    $instance = field_info_instance($type, $field['field_name'], $data[$type]->{$key});
                    $allowed_values = phone_numbertype_allowed_values($field, $instance);
                    if (isset($allowed_values[$item['numbertype']])) {
                      $formatted[] = $allowed_values[$item['numbertype']];
                    }
                  }
                  break;
                default:
                  if (isset($item[$action_key])) {
                    $formatted[] = $item[$action_key];
                  }
                  break;
              }
            }
          }
          $formatted = implode(', ', array_filter($formatted));
          $replacements[$original] = $sanitize ? check_plain($formatted) : $formatted;
        }
      }
    }
  }
  return $replacements;
}