Chris@0: t("Taxonomy terms"), Chris@0: 'description' => t("Tokens related to taxonomy terms."), Chris@0: 'needs-data' => 'term', Chris@0: ]; Chris@0: $types['vocabulary'] = [ Chris@0: 'name' => t("Vocabularies"), Chris@0: 'description' => t("Tokens related to taxonomy vocabularies."), Chris@0: 'needs-data' => 'vocabulary', Chris@0: ]; Chris@0: Chris@0: // Taxonomy term related variables. Chris@0: $term['tid'] = [ Chris@0: 'name' => t("Term ID"), Chris@0: 'description' => t("The unique ID of the taxonomy term."), Chris@0: ]; Chris@0: $term['name'] = [ Chris@0: 'name' => t("Name"), Chris@0: 'description' => t("The name of the taxonomy term."), Chris@0: ]; Chris@0: $term['description'] = [ Chris@0: 'name' => t("Description"), Chris@0: 'description' => t("The optional description of the taxonomy term."), Chris@0: ]; Chris@0: $term['node-count'] = [ Chris@0: 'name' => t("Node count"), Chris@0: 'description' => t("The number of nodes tagged with the taxonomy term."), Chris@0: ]; Chris@0: $term['url'] = [ Chris@0: 'name' => t("URL"), Chris@0: 'description' => t("The URL of the taxonomy term."), Chris@0: ]; Chris@0: Chris@0: // Taxonomy vocabulary related variables. Chris@0: $vocabulary['vid'] = [ Chris@0: 'name' => t("Vocabulary ID"), Chris@0: 'description' => t("The unique ID of the taxonomy vocabulary."), Chris@0: ]; Chris@0: $vocabulary['name'] = [ Chris@0: 'name' => t("Name"), Chris@0: 'description' => t("The name of the taxonomy vocabulary."), Chris@0: ]; Chris@0: $vocabulary['description'] = [ Chris@0: 'name' => t("Description"), Chris@0: 'description' => t("The optional description of the taxonomy vocabulary."), Chris@0: ]; Chris@0: $vocabulary['node-count'] = [ Chris@0: 'name' => t("Node count"), Chris@0: 'description' => t("The number of nodes tagged with terms belonging to the taxonomy vocabulary."), Chris@0: ]; Chris@0: $vocabulary['term-count'] = [ Chris@0: 'name' => t("Term count"), Chris@0: 'description' => t("The number of terms belonging to the taxonomy vocabulary."), Chris@0: ]; Chris@0: Chris@0: // Chained tokens for taxonomies Chris@0: $term['vocabulary'] = [ Chris@0: 'name' => t("Vocabulary"), Chris@0: 'description' => t("The vocabulary the taxonomy term belongs to."), Chris@0: 'type' => 'vocabulary', Chris@0: ]; Chris@0: $term['parent'] = [ Chris@0: 'name' => t("Parent term"), Chris@0: 'description' => t("The parent term of the taxonomy term, if one exists."), Chris@0: 'type' => 'term', Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'types' => $types, Chris@0: 'tokens' => [ Chris@0: 'term' => $term, Chris@0: 'vocabulary' => $vocabulary, Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_tokens(). Chris@0: */ Chris@0: function taxonomy_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { Chris@0: $token_service = \Drupal::token(); Chris@0: Chris@0: $replacements = []; Chris@0: $taxonomy_storage = \Drupal::entityManager()->getStorage('taxonomy_term'); Chris@0: if ($type == 'term' && !empty($data['term'])) { Chris@0: $term = $data['term']; Chris@0: Chris@0: foreach ($tokens as $name => $original) { Chris@0: switch ($name) { Chris@0: case 'tid': Chris@0: $replacements[$original] = $term->id(); Chris@0: break; Chris@0: Chris@0: case 'name': Chris@0: $replacements[$original] = $term->getName(); Chris@0: break; Chris@0: Chris@0: case 'description': Chris@0: // "processed" returns a \Drupal\Component\Render\MarkupInterface via Chris@0: // check_markup(). Chris@0: $replacements[$original] = $term->description->processed; Chris@0: break; Chris@0: Chris@0: case 'url': Chris@18: $replacements[$original] = $term->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@0: break; Chris@0: Chris@0: case 'node-count': Chris@18: $query = \Drupal::database()->select('taxonomy_index'); Chris@0: $query->condition('tid', $term->id()); Chris@0: $query->addTag('term_node_count'); Chris@0: $count = $query->countQuery()->execute()->fetchField(); Chris@0: $replacements[$original] = $count; Chris@0: break; Chris@0: Chris@0: case 'vocabulary': Chris@0: $vocabulary = Vocabulary::load($term->bundle()); Chris@0: $bubbleable_metadata->addCacheableDependency($vocabulary); Chris@0: $replacements[$original] = $vocabulary->label(); Chris@0: break; Chris@0: Chris@0: case 'parent': Chris@0: if ($parents = $taxonomy_storage->loadParents($term->id())) { Chris@0: $parent = array_pop($parents); Chris@0: $bubbleable_metadata->addCacheableDependency($parent); Chris@0: $replacements[$original] = $parent->getName(); Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'vocabulary')) { Chris@0: $vocabulary = Vocabulary::load($term->bundle()); Chris@0: $replacements += $token_service->generate('vocabulary', $vocabulary_tokens, ['vocabulary' => $vocabulary], $options, $bubbleable_metadata); Chris@0: } Chris@0: Chris@0: if (($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'parent')) && $parents = $taxonomy_storage->loadParents($term->id())) { Chris@0: $parent = array_pop($parents); Chris@0: $replacements += $token_service->generate('term', $vocabulary_tokens, ['term' => $parent], $options, $bubbleable_metadata); Chris@0: } Chris@0: } Chris@0: Chris@0: elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) { Chris@0: $vocabulary = $data['vocabulary']; Chris@0: Chris@0: foreach ($tokens as $name => $original) { Chris@0: switch ($name) { Chris@0: case 'vid': Chris@0: $replacements[$original] = $vocabulary->id(); Chris@0: break; Chris@0: Chris@0: case 'name': Chris@0: $replacements[$original] = $vocabulary->label(); Chris@0: break; Chris@0: Chris@0: case 'description': Chris@0: $build = ['#markup' => $vocabulary->getDescription()]; Chris@0: // @todo Fix in https://www.drupal.org/node/2577827 Chris@0: $replacements[$original] = \Drupal::service('renderer')->renderPlain($build); Chris@0: break; Chris@0: Chris@0: case 'term-count': Chris@0: $replacements[$original] = \Drupal::entityQuery('taxonomy_term') Chris@0: ->condition('vid', $vocabulary->id()) Chris@0: ->addTag('vocabulary_term_count') Chris@0: ->count() Chris@0: ->execute(); Chris@0: break; Chris@0: Chris@0: case 'node-count': Chris@0: $replacements[$original] = $taxonomy_storage->nodeCount($vocabulary->id()); Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $replacements; Chris@0: }