function taxonomy_tokens
Same name in other branches
- 7.x modules/taxonomy/taxonomy.tokens.inc \taxonomy_tokens()
- 8.9.x core/modules/taxonomy/taxonomy.tokens.inc \taxonomy_tokens()
- 10 core/modules/taxonomy/taxonomy.tokens.inc \taxonomy_tokens()
- 11.x core/modules/taxonomy/taxonomy.tokens.inc \taxonomy_tokens()
Implements hook_tokens().
File
-
core/
modules/ taxonomy/ taxonomy.tokens.inc, line 94
Code
function taxonomy_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$token_service = \Drupal::token();
$replacements = [];
if ($type == 'term' && !empty($data['term'])) {
$term = $data['term'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'tid':
$replacements[$original] = $term->id();
break;
case 'name':
$replacements[$original] = $term->getName();
break;
case 'description':
// "processed" returns a \Drupal\Component\Render\MarkupInterface via
// check_markup().
$replacements[$original] = $term->description->processed;
break;
case 'url':
$replacements[$original] = $term->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
break;
case 'node-count':
$query = \Drupal::database()->select('taxonomy_index');
$query->condition('tid', $term->id());
$query->addTag('term_node_count');
$count = $query->countQuery()
->execute()
->fetchField();
$replacements[$original] = $count;
break;
case 'vocabulary':
$vocabulary = Vocabulary::load($term->bundle());
$bubbleable_metadata->addCacheableDependency($vocabulary);
$replacements[$original] = $vocabulary->label();
break;
case 'parent':
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
if ($parents = $taxonomy_storage->loadParents($term->id())) {
$parent = array_pop($parents);
$bubbleable_metadata->addCacheableDependency($parent);
$replacements[$original] = $parent->getName();
}
break;
}
}
if ($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'vocabulary')) {
$vocabulary = Vocabulary::load($term->bundle());
$replacements += $token_service->generate('vocabulary', $vocabulary_tokens, [
'vocabulary' => $vocabulary,
], $options, $bubbleable_metadata);
}
if ($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'parent')) {
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
if ($parents = $taxonomy_storage->loadParents($term->id())) {
$parent = array_pop($parents);
$replacements += $token_service->generate('term', $vocabulary_tokens, [
'term' => $parent,
], $options, $bubbleable_metadata);
}
}
}
elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
$vocabulary = $data['vocabulary'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'vid':
$replacements[$original] = $vocabulary->id();
break;
case 'name':
$replacements[$original] = $vocabulary->label();
break;
case 'description':
$build = [
'#markup' => $vocabulary->getDescription(),
];
// @todo Fix in https://www.drupal.org/node/2577827
$replacements[$original] = \Drupal::service('renderer')->renderPlain($build);
break;
case 'term-count':
$replacements[$original] = \Drupal::entityQuery('taxonomy_term')->accessCheck(TRUE)
->condition('vid', $vocabulary->id())
->addTag('vocabulary_term_count')
->count()
->execute();
break;
case 'node-count':
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$replacements[$original] = $taxonomy_storage->nodeCount($vocabulary->id());
break;
}
}
}
return $replacements;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.