You are here

function taxonomy_entity_index_tokens in Taxonomy Entity Index 8

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

Implements hook_tokens().

File

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

Code

function taxonomy_entity_index_tokens($type, $tokens, array $data = [], array $options = []) {
  if (!\Drupal::moduleHandler()
    ->moduleExists('token') || _token_module($type, 'terms') != 'taxonomy_entity_index') {

    // We depend on the token_get_entity_mapping() function in token.module.
    return [];
  }
  $replacements = [];

  // Entity tokens.
  if (!empty($data[$type]) && ($entity_type = token_get_entity_mapping('token', $type))) {
    $entity = $data[$type];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'terms':
          $terms = _taxonomy_entity_index_get_terms_array($entity_type, $entity);
          $replacements[$original] = token_render_array($terms, $options);
          break;
      }
    }

    // Chained token relationships.
    if ($terms_tokens = \Drupal::token()
      ->findWithPrefix($tokens, 'terms')) {
      if ($terms = _taxonomy_entity_index_get_terms_array($entity_type, $entity)) {
        $replacements += \Drupal::token()
          ->generate('array', $terms_tokens, [
          'array' => $terms,
        ], $options);
      }
    }
  }
  return $replacements;
}