You are here

function address_token_info in Address 8

Implements hook_token_info().

File

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

Code

function address_token_info() {
  if (!\Drupal::hasService('token.entity_mapper')) {
    return;
  }
  $types = [];
  $tokens = [];
  foreach (\Drupal::entityTypeManager()
    ->getDefinitions() as $entity_type_id => $entity_type) {
    if (!$entity_type
      ->entityClassImplements(ContentEntityInterface::class)) {
      continue;
    }
    $token_type = \Drupal::service('token.entity_mapper')
      ->getTokenTypeForEntityType($entity_type_id);
    if (empty($token_type)) {
      continue;
    }

    // Build country name tokens for all address fields.
    $fields = \Drupal::service('entity_field.manager')
      ->getFieldStorageDefinitions($entity_type_id);
    foreach ($fields as $field_name => $field) {
      if ($field
        ->getType() != 'address') {
        continue;
      }
      $tokens[$token_type . '-' . $field_name]['country_name'] = [
        'name' => t('The country name'),
        'description' => NULL,
        'module' => 'address',
      ];
    }
  }
  return [
    'types' => $types,
    'tokens' => $tokens,
  ];
}