taxonomy.tokens.inc 5.82 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Builds placeholder replacement tokens for taxonomy terms and vocabularies.
 */

8
use Drupal\Core\Render\BubbleableMetadata;
9
use Drupal\taxonomy\Entity\Vocabulary;
10

11
/**
12
 * Implements hook_token_info().
13 14
 */
function taxonomy_token_info() {
15
  $types['term'] = [
16 17 18
    'name' => t("Taxonomy terms"),
    'description' => t("Tokens related to taxonomy terms."),
    'needs-data' => 'term',
19 20
  ];
  $types['vocabulary'] = [
21 22 23
    'name' => t("Vocabularies"),
    'description' => t("Tokens related to taxonomy vocabularies."),
    'needs-data' => 'vocabulary',
24
  ];
25 26

  // Taxonomy term related variables.
27
  $term['tid'] = [
28 29
    'name' => t("Term ID"),
    'description' => t("The unique ID of the taxonomy term."),
30 31
  ];
  $term['name'] = [
32 33
    'name' => t("Name"),
    'description' => t("The name of the taxonomy term."),
34 35
  ];
  $term['description'] = [
36 37
    'name' => t("Description"),
    'description' => t("The optional description of the taxonomy term."),
38 39
  ];
  $term['node-count'] = [
40 41
    'name' => t("Node count"),
    'description' => t("The number of nodes tagged with the taxonomy term."),
42 43
  ];
  $term['url'] = [
44 45
    'name' => t("URL"),
    'description' => t("The URL of the taxonomy term."),
46
  ];
47 48

  // Taxonomy vocabulary related variables.
49
  $vocabulary['vid'] = [
50 51
    'name' => t("Vocabulary ID"),
    'description' => t("The unique ID of the taxonomy vocabulary."),
52 53
  ];
  $vocabulary['name'] = [
54 55
    'name' => t("Name"),
    'description' => t("The name of the taxonomy vocabulary."),
56 57
  ];
  $vocabulary['description'] = [
58 59
    'name' => t("Description"),
    'description' => t("The optional description of the taxonomy vocabulary."),
60 61
  ];
  $vocabulary['node-count'] = [
62 63
    'name' => t("Node count"),
    'description' => t("The number of nodes tagged with terms belonging to the taxonomy vocabulary."),
64 65
  ];
  $vocabulary['term-count'] = [
66
    'name' => t("Term count"),
67
    'description' => t("The number of terms belonging to the taxonomy vocabulary."),
68
  ];
69 70

  // Chained tokens for taxonomies
71
  $term['vocabulary'] = [
72 73 74
    'name' => t("Vocabulary"),
    'description' => t("The vocabulary the taxonomy term belongs to."),
    'type' => 'vocabulary',
75 76
  ];
  $term['parent'] = [
77 78 79
    'name' => t("Parent term"),
    'description' => t("The parent term of the taxonomy term, if one exists."),
    'type' => 'term',
80
  ];
81

82
  return [
83
    'types' => $types,
84
    'tokens' => [
85 86
      'term' => $term,
      'vocabulary' => $vocabulary,
87 88
    ],
  ];
89 90 91
}

/**
92
 * Implements hook_tokens().
93
 */
94
function taxonomy_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
95
  $token_service = \Drupal::token();
96

97
  $replacements = [];
98
  $taxonomy_storage = \Drupal::entityManager()->getStorage('taxonomy_term');
99 100 101 102 103 104
  if ($type == 'term' && !empty($data['term'])) {
    $term = $data['term'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'tid':
105
          $replacements[$original] = $term->id();
106 107 108
          break;

        case 'name':
109
          $replacements[$original] = $term->getName();
110 111 112
          break;

        case 'description':
113 114
          // "processed" returns a \Drupal\Component\Render\MarkupInterface via
          // check_markup().
115
          $replacements[$original] = $term->description->processed;
116 117 118
          break;

        case 'url':
119
          $replacements[$original] = $term->url('canonical', ['absolute' => TRUE]);
120 121 122
          break;

        case 'node-count':
123
          $query = db_select('taxonomy_index');
124
          $query->condition('tid', $term->id());
125 126
          $query->addTag('term_node_count');
          $count = $query->countQuery()->execute()->fetchField();
127 128 129 130
          $replacements[$original] = $count;
          break;

        case 'vocabulary':
131
          $vocabulary = Vocabulary::load($term->bundle());
132
          $bubbleable_metadata->addCacheableDependency($vocabulary);
133
          $replacements[$original] = $vocabulary->label();
134 135 136
          break;

        case 'parent':
137
          if ($parents = $taxonomy_storage->loadParents($term->id())) {
138
            $parent = array_pop($parents);
139
            $bubbleable_metadata->addCacheableDependency($parent);
140
            $replacements[$original] = $parent->getName();
141
          }
142 143 144 145
          break;
      }
    }

146
    if ($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'vocabulary')) {
147
      $vocabulary = Vocabulary::load($term->bundle());
148
      $replacements += $token_service->generate('vocabulary', $vocabulary_tokens, ['vocabulary' => $vocabulary], $options, $bubbleable_metadata);
149 150
    }

151
    if (($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'parent')) && $parents = $taxonomy_storage->loadParents($term->id())) {
152
      $parent = array_pop($parents);
153
      $replacements += $token_service->generate('term', $vocabulary_tokens, ['term' => $parent], $options, $bubbleable_metadata);
154 155 156 157 158 159 160 161 162
    }
  }

  elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
    $vocabulary = $data['vocabulary'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'vid':
163
          $replacements[$original] = $vocabulary->id();
164 165 166
          break;

        case 'name':
167
          $replacements[$original] = $vocabulary->label();
168 169 170
          break;

        case 'description':
171 172 173
          $build = ['#markup' => $vocabulary->getDescription()];
          // @todo Fix in https://www.drupal.org/node/2577827
          $replacements[$original] = \Drupal::service('renderer')->renderPlain($build);
174 175 176
          break;

        case 'term-count':
177 178 179 180 181
          $replacements[$original] = \Drupal::entityQuery('taxonomy_term')
            ->condition('vid', $vocabulary->id())
            ->addTag('vocabulary_term_count')
            ->count()
            ->execute();
182 183 184
          break;

        case 'node-count':
185
          $replacements[$original] = $taxonomy_storage->nodeCount($vocabulary->id());
186 187 188 189 190 191 192
          break;
      }
    }
  }

  return $replacements;
}