taxonomy.tokens.inc 5.92 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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 */
function taxonomy_token_info() {
  $types['term'] = array(
    'name' => t("Taxonomy terms"),
    'description' => t("Tokens related to taxonomy terms."),
    'needs-data' => 'term',
  );
  $types['vocabulary'] = array(
    'name' => t("Vocabularies"),
    'description' => t("Tokens related to taxonomy vocabularies."),
    'needs-data' => 'vocabulary',
  );

  // Taxonomy term related variables.
  $term['tid'] = array(
    'name' => t("Term ID"),
    'description' => t("The unique ID of the taxonomy term."),
  );
  $term['name'] = array(
    'name' => t("Name"),
    'description' => t("The name of the taxonomy term."),
  );
  $term['description'] = array(
    'name' => t("Description"),
    'description' => t("The optional description of the taxonomy term."),
  );
  $term['node-count'] = array(
    'name' => t("Node count"),
    'description' => t("The number of nodes tagged with the taxonomy term."),
  );
  $term['url'] = array(
    'name' => t("URL"),
    'description' => t("The URL of the taxonomy term."),
  );

  // Taxonomy vocabulary related variables.
  $vocabulary['vid'] = array(
    'name' => t("Vocabulary ID"),
    'description' => t("The unique ID of the taxonomy vocabulary."),
  );
  $vocabulary['name'] = array(
    'name' => t("Name"),
    'description' => t("The name of the taxonomy vocabulary."),
  );
  $vocabulary['description'] = array(
    'name' => t("Description"),
    'description' => t("The optional description of the taxonomy vocabulary."),
  );
  $vocabulary['node-count'] = array(
    'name' => t("Node count"),
    'description' => t("The number of nodes tagged with terms belonging to the taxonomy vocabulary."),
  );
  $vocabulary['term-count'] = array(
66
    'name' => t("Term count"),
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    'description' => t("The number of terms belonging to the taxonomy vocabulary."),
  );

  // Chained tokens for taxonomies
  $term['vocabulary'] = array(
    'name' => t("Vocabulary"),
    'description' => t("The vocabulary the taxonomy term belongs to."),
    'type' => 'vocabulary',
  );
  $term['parent'] = array(
    'name' => t("Parent term"),
    'description' => t("The parent term of the taxonomy term, if one exists."),
    'type' => 'term',
  );

  return array(
    'types' => $types,
    'tokens' => array(
      'term' => $term,
      'vocabulary' => $vocabulary,
    ),
  );
}

/**
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 = array();
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', array('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, array('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, array('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;
}