You are here

function country_tokens in Country 8

Implements hook_tokens().

File

./country.tokens.inc, line 50
Provides Token integration for country.

Code

function country_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_original_name') {
        continue;
      }
      if (!isset($data[$data['field_name']][$delta])) {
        continue;
      }
      $field_item = $data[$data['field_name']][$delta];
      $country_name = t('');
      if ($country_code = $field_item->value) {
        $field_definition = $field_item
          ->getFieldDefinition();
        $countries = \Drupal::service('country.field.manager')
          ->getSelectableCountries($field_definition);
        $country_name = $countries[$country_code];
      }
      $replacements[$original] = $country_name
        ->render();
    }
  }
  return $replacements;
}