You are here

function taxonomy_entity_index_token_info_alter in Taxonomy Entity Index 8

Same name and namespace in other branches
  1. 7 taxonomy_entity_index.tokens.inc \taxonomy_entity_index_token_info_alter()

Implements hook_token_info_alter().

File

includes/taxonomy_entity_index.tokens.inc, line 11
Token integration for the taxonomy_entity_index module.

Code

function taxonomy_entity_index_token_info_alter(&$info) {
  if (!\Drupal::moduleHandler()
    ->moduleExists('token')) {

    // We depend on the token_get_entity_mapping() function in token.module.
    return $info;
  }

  // Add [entity:terms] tokens for all entities.
  $entities = \Drupal::entityTypeManager()
    ->getDefinitions();
  foreach ($entities as $entity_type => $entity_info) {
    if (!isset($entity_info['token type'])) {
      continue;
    }

    // Only add to valid token types.
    $token_type = $entity_info['token type'];
    if (!isset($info['tokens'][$token_type]) || !isset($info['types'][$token_type])) {
      continue;
    }

    // Do not add token if the entity type has no term fields.
    $fields = taxonomy_entity_index_get_taxonomy_field_names($entity_type);
    if (empty($fields)) {
      continue;
    }
    if (!isset($info['tokens'][$token_type]['terms'])) {
      $info['tokens'][$token_type]['terms'] = [
        'name' => t('Terms'),
        'description' => t('The terms associated with the @entity.', [
          '@entity' => mb_strtolower($entity_info['label']),
        ]),
        'type' => 'array',
        'module' => 'taxonomy_entity_index',
      ];
    }
  }
}