You are here

function address_tokens in Address 8

Implements hook_tokens().

File

./address.tokens.inc, line 54
Provides Token integration for Address.

Code

function address_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if (!empty($data['field_property'])) {
    foreach ($tokens as $token => $original) {
      $delta = 0;
      $parts = explode(':', $token);
      if (is_numeric($parts[0])) {
        if (count($parts) > 1) {
          $delta = $parts[0];
          $property_name = $parts[1];
        }
        else {
          continue;
        }
      }
      else {
        $property_name = $parts[0];
      }
      if ($property_name != 'country_name') {
        continue;
      }
      if (!isset($data[$data['field_name']][$delta])) {
        continue;
      }
      $field_item = $data[$data['field_name']][$delta];
      $country_name = '';
      if ($country_code = $field_item->country_code) {
        $country_repository = \Drupal::service('address.country_repository');
        $locale = isset($options['langcode']) ? $options['langcode'] : NULL;
        if ($country = $country_repository
          ->get($country_code, $locale)) {
          $country_name = $country
            ->getName();
        }
      }
      $replacements[$original] = $country_name;
    }
  }
  return $replacements;
}