annotate core/modules/taxonomy/src/TermStorage.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\taxonomy;
Chris@0 4
Chris@17 5 use Drupal\Core\Entity\EntityInterface;
Chris@0 6 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Defines a Controller class for taxonomy terms.
Chris@0 10 */
Chris@0 11 class TermStorage extends SqlContentEntityStorage implements TermStorageInterface {
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Array of term parents keyed by vocabulary ID and child term ID.
Chris@0 15 *
Chris@0 16 * @var array
Chris@0 17 */
Chris@0 18 protected $treeParents = [];
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Array of term ancestors keyed by vocabulary ID and parent term ID.
Chris@0 22 *
Chris@0 23 * @var array
Chris@0 24 */
Chris@0 25 protected $treeChildren = [];
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Array of terms in a tree keyed by vocabulary ID and term ID.
Chris@0 29 *
Chris@0 30 * @var array
Chris@0 31 */
Chris@0 32 protected $treeTerms = [];
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Array of loaded trees keyed by a cache id matching tree arguments.
Chris@0 36 *
Chris@0 37 * @var array
Chris@0 38 */
Chris@0 39 protected $trees = [];
Chris@0 40
Chris@0 41 /**
Chris@17 42 * Array of all loaded term ancestry keyed by ancestor term ID, keyed by term
Chris@17 43 * ID.
Chris@17 44 *
Chris@17 45 * @var \Drupal\taxonomy\TermInterface[][]
Chris@17 46 */
Chris@17 47 protected $ancestors;
Chris@17 48
Chris@17 49 /**
Chris@0 50 * {@inheritdoc}
Chris@0 51 *
Chris@0 52 * @param array $values
Chris@0 53 * An array of values to set, keyed by property name. A value for the
Chris@0 54 * vocabulary ID ('vid') is required.
Chris@0 55 */
Chris@0 56 public function create(array $values = []) {
Chris@0 57 // Save new terms with no parents by default.
Chris@0 58 if (empty($values['parent'])) {
Chris@0 59 $values['parent'] = [0];
Chris@0 60 }
Chris@0 61 $entity = parent::create($values);
Chris@0 62 return $entity;
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * {@inheritdoc}
Chris@0 67 */
Chris@0 68 public function resetCache(array $ids = NULL) {
Chris@0 69 drupal_static_reset('taxonomy_term_count_nodes');
Chris@17 70 $this->ancestors = [];
Chris@0 71 $this->treeChildren = [];
Chris@0 72 $this->treeParents = [];
Chris@0 73 $this->treeTerms = [];
Chris@0 74 $this->trees = [];
Chris@0 75 parent::resetCache($ids);
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * {@inheritdoc}
Chris@0 80 */
Chris@17 81 public function deleteTermHierarchy($tids) {}
Chris@0 82
Chris@0 83 /**
Chris@0 84 * {@inheritdoc}
Chris@0 85 */
Chris@17 86 public function updateTermHierarchy(EntityInterface $term) {}
Chris@0 87
Chris@0 88 /**
Chris@0 89 * {@inheritdoc}
Chris@0 90 */
Chris@0 91 public function loadParents($tid) {
Chris@17 92 $terms = [];
Chris@17 93 /** @var \Drupal\taxonomy\TermInterface $term */
Chris@17 94 if ($tid && $term = $this->load($tid)) {
Chris@17 95 foreach ($this->getParents($term) as $id => $parent) {
Chris@17 96 // This method currently doesn't return the <root> parent.
Chris@17 97 // @see https://www.drupal.org/node/2019905
Chris@17 98 if (!empty($id)) {
Chris@17 99 $terms[$id] = $parent;
Chris@17 100 }
Chris@0 101 }
Chris@0 102 }
Chris@17 103
Chris@17 104 return $terms;
Chris@17 105 }
Chris@17 106
Chris@17 107 /**
Chris@17 108 * Returns a list of parents of this term.
Chris@17 109 *
Chris@17 110 * @return \Drupal\taxonomy\TermInterface[]
Chris@17 111 * The parent taxonomy term entities keyed by term ID. If this term has a
Chris@17 112 * <root> parent, that item is keyed with 0 and will have NULL as value.
Chris@17 113 *
Chris@17 114 * @internal
Chris@17 115 * @todo Refactor away when TreeInterface is introduced.
Chris@17 116 */
Chris@17 117 protected function getParents(TermInterface $term) {
Chris@17 118 $parents = $ids = [];
Chris@17 119 // Cannot use $this->get('parent')->referencedEntities() here because that
Chris@17 120 // strips out the '0' reference.
Chris@17 121 foreach ($term->get('parent') as $item) {
Chris@17 122 if ($item->target_id == 0) {
Chris@17 123 // The <root> parent.
Chris@17 124 $parents[0] = NULL;
Chris@17 125 continue;
Chris@17 126 }
Chris@17 127 $ids[] = $item->target_id;
Chris@17 128 }
Chris@17 129
Chris@17 130 // @todo Better way to do this? AND handle the NULL/0 parent?
Chris@17 131 // Querying the terms again so that the same access checks are run when
Chris@17 132 // getParents() is called as in Drupal version prior to 8.3.
Chris@17 133 $loaded_parents = [];
Chris@17 134
Chris@17 135 if ($ids) {
Chris@17 136 $query = \Drupal::entityQuery('taxonomy_term')
Chris@17 137 ->condition('tid', $ids, 'IN');
Chris@17 138
Chris@17 139 $loaded_parents = static::loadMultiple($query->execute());
Chris@17 140 }
Chris@17 141
Chris@17 142 return $parents + $loaded_parents;
Chris@0 143 }
Chris@0 144
Chris@0 145 /**
Chris@0 146 * {@inheritdoc}
Chris@0 147 */
Chris@0 148 public function loadAllParents($tid) {
Chris@17 149 /** @var \Drupal\taxonomy\TermInterface $term */
Chris@17 150 return (!empty($tid) && $term = $this->load($tid)) ? $this->getAncestors($term) : [];
Chris@17 151 }
Chris@0 152
Chris@17 153 /**
Chris@17 154 * Returns all ancestors of this term.
Chris@17 155 *
Chris@17 156 * @return \Drupal\taxonomy\TermInterface[]
Chris@17 157 * A list of ancestor taxonomy term entities keyed by term ID.
Chris@17 158 *
Chris@17 159 * @internal
Chris@17 160 * @todo Refactor away when TreeInterface is introduced.
Chris@17 161 */
Chris@17 162 protected function getAncestors(TermInterface $term) {
Chris@17 163 if (!isset($this->ancestors[$term->id()])) {
Chris@17 164 $this->ancestors[$term->id()] = [$term->id() => $term];
Chris@17 165 $search[] = $term->id();
Chris@17 166
Chris@17 167 while ($tid = array_shift($search)) {
Chris@17 168 foreach ($this->getParents(static::load($tid)) as $id => $parent) {
Chris@17 169 if ($parent && !isset($this->ancestors[$term->id()][$id])) {
Chris@17 170 $this->ancestors[$term->id()][$id] = $parent;
Chris@17 171 $search[] = $id;
Chris@0 172 }
Chris@0 173 }
Chris@0 174 }
Chris@0 175 }
Chris@17 176 return $this->ancestors[$term->id()];
Chris@0 177 }
Chris@0 178
Chris@0 179 /**
Chris@0 180 * {@inheritdoc}
Chris@0 181 */
Chris@0 182 public function loadChildren($tid, $vid = NULL) {
Chris@17 183 /** @var \Drupal\taxonomy\TermInterface $term */
Chris@17 184 return (!empty($tid) && $term = $this->load($tid)) ? $this->getChildren($term) : [];
Chris@17 185 }
Chris@17 186
Chris@17 187 /**
Chris@17 188 * Returns all children terms of this term.
Chris@17 189 *
Chris@17 190 * @return \Drupal\taxonomy\TermInterface[]
Chris@17 191 * A list of children taxonomy term entities keyed by term ID.
Chris@17 192 *
Chris@17 193 * @internal
Chris@17 194 * @todo Refactor away when TreeInterface is introduced.
Chris@17 195 */
Chris@17 196 public function getChildren(TermInterface $term) {
Chris@17 197 $query = \Drupal::entityQuery('taxonomy_term')
Chris@17 198 ->condition('parent', $term->id());
Chris@17 199 return static::loadMultiple($query->execute());
Chris@0 200 }
Chris@0 201
Chris@0 202 /**
Chris@0 203 * {@inheritdoc}
Chris@0 204 */
Chris@0 205 public function loadTree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) {
Chris@0 206 $cache_key = implode(':', func_get_args());
Chris@0 207 if (!isset($this->trees[$cache_key])) {
Chris@0 208 // We cache trees, so it's not CPU-intensive to call on a term and its
Chris@0 209 // children, too.
Chris@0 210 if (!isset($this->treeChildren[$vid])) {
Chris@0 211 $this->treeChildren[$vid] = [];
Chris@0 212 $this->treeParents[$vid] = [];
Chris@0 213 $this->treeTerms[$vid] = [];
Chris@17 214 $query = $this->database->select($this->getDataTable(), 't');
Chris@17 215 $query->join('taxonomy_term__parent', 'p', 't.tid = p.entity_id');
Chris@17 216 $query->addExpression('parent_target_id', 'parent');
Chris@0 217 $result = $query
Chris@0 218 ->addTag('taxonomy_term_access')
Chris@0 219 ->fields('t')
Chris@0 220 ->condition('t.vid', $vid)
Chris@0 221 ->condition('t.default_langcode', 1)
Chris@0 222 ->orderBy('t.weight')
Chris@0 223 ->orderBy('t.name')
Chris@0 224 ->execute();
Chris@0 225 foreach ($result as $term) {
Chris@0 226 $this->treeChildren[$vid][$term->parent][] = $term->tid;
Chris@0 227 $this->treeParents[$vid][$term->tid][] = $term->parent;
Chris@0 228 $this->treeTerms[$vid][$term->tid] = $term;
Chris@0 229 }
Chris@0 230 }
Chris@0 231
Chris@0 232 // Load full entities, if necessary. The entity controller statically
Chris@0 233 // caches the results.
Chris@0 234 $term_entities = [];
Chris@0 235 if ($load_entities) {
Chris@0 236 $term_entities = $this->loadMultiple(array_keys($this->treeTerms[$vid]));
Chris@0 237 }
Chris@0 238
Chris@0 239 $max_depth = (!isset($max_depth)) ? count($this->treeChildren[$vid]) : $max_depth;
Chris@0 240 $tree = [];
Chris@0 241
Chris@0 242 // Keeps track of the parents we have to process, the last entry is used
Chris@0 243 // for the next processing step.
Chris@0 244 $process_parents = [];
Chris@0 245 $process_parents[] = $parent;
Chris@0 246
Chris@0 247 // Loops over the parent terms and adds its children to the tree array.
Chris@0 248 // Uses a loop instead of a recursion, because it's more efficient.
Chris@0 249 while (count($process_parents)) {
Chris@0 250 $parent = array_pop($process_parents);
Chris@0 251 // The number of parents determines the current depth.
Chris@0 252 $depth = count($process_parents);
Chris@0 253 if ($max_depth > $depth && !empty($this->treeChildren[$vid][$parent])) {
Chris@0 254 $has_children = FALSE;
Chris@0 255 $child = current($this->treeChildren[$vid][$parent]);
Chris@0 256 do {
Chris@0 257 if (empty($child)) {
Chris@0 258 break;
Chris@0 259 }
Chris@0 260 $term = $load_entities ? $term_entities[$child] : $this->treeTerms[$vid][$child];
Chris@0 261 if (isset($this->treeParents[$vid][$load_entities ? $term->id() : $term->tid])) {
Chris@0 262 // Clone the term so that the depth attribute remains correct
Chris@0 263 // in the event of multiple parents.
Chris@0 264 $term = clone $term;
Chris@0 265 }
Chris@0 266 $term->depth = $depth;
Chris@17 267 if (!$load_entities) {
Chris@17 268 unset($term->parent);
Chris@17 269 }
Chris@0 270 $tid = $load_entities ? $term->id() : $term->tid;
Chris@0 271 $term->parents = $this->treeParents[$vid][$tid];
Chris@0 272 $tree[] = $term;
Chris@0 273 if (!empty($this->treeChildren[$vid][$tid])) {
Chris@0 274 $has_children = TRUE;
Chris@0 275
Chris@0 276 // We have to continue with this parent later.
Chris@0 277 $process_parents[] = $parent;
Chris@0 278 // Use the current term as parent for the next iteration.
Chris@0 279 $process_parents[] = $tid;
Chris@0 280
Chris@0 281 // Reset pointers for child lists because we step in there more
Chris@0 282 // often with multi parents.
Chris@0 283 reset($this->treeChildren[$vid][$tid]);
Chris@0 284 // Move pointer so that we get the correct term the next time.
Chris@0 285 next($this->treeChildren[$vid][$parent]);
Chris@0 286 break;
Chris@0 287 }
Chris@0 288 } while ($child = next($this->treeChildren[$vid][$parent]));
Chris@0 289
Chris@0 290 if (!$has_children) {
Chris@0 291 // We processed all terms in this hierarchy-level, reset pointer
Chris@0 292 // so that this function works the next time it gets called.
Chris@0 293 reset($this->treeChildren[$vid][$parent]);
Chris@0 294 }
Chris@0 295 }
Chris@0 296 }
Chris@0 297 $this->trees[$cache_key] = $tree;
Chris@0 298 }
Chris@0 299 return $this->trees[$cache_key];
Chris@0 300 }
Chris@0 301
Chris@0 302 /**
Chris@0 303 * {@inheritdoc}
Chris@0 304 */
Chris@0 305 public function nodeCount($vid) {
Chris@0 306 $query = $this->database->select('taxonomy_index', 'ti');
Chris@0 307 $query->addExpression('COUNT(DISTINCT ti.nid)');
Chris@17 308 $query->leftJoin($this->getBaseTable(), 'td', 'ti.tid = td.tid');
Chris@0 309 $query->condition('td.vid', $vid);
Chris@0 310 $query->addTag('vocabulary_node_count');
Chris@0 311 return $query->execute()->fetchField();
Chris@0 312 }
Chris@0 313
Chris@0 314 /**
Chris@0 315 * {@inheritdoc}
Chris@0 316 */
Chris@0 317 public function resetWeights($vid) {
Chris@17 318 $this->database->update($this->getDataTable())
Chris@0 319 ->fields(['weight' => 0])
Chris@0 320 ->condition('vid', $vid)
Chris@0 321 ->execute();
Chris@0 322 }
Chris@0 323
Chris@0 324 /**
Chris@0 325 * {@inheritdoc}
Chris@0 326 */
Chris@0 327 public function getNodeTerms(array $nids, array $vocabs = [], $langcode = NULL) {
Chris@17 328 $query = db_select($this->getDataTable(), 'td');
Chris@0 329 $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
Chris@0 330 $query->fields('td', ['tid']);
Chris@0 331 $query->addField('tn', 'nid', 'node_nid');
Chris@0 332 $query->orderby('td.weight');
Chris@0 333 $query->orderby('td.name');
Chris@0 334 $query->condition('tn.nid', $nids, 'IN');
Chris@0 335 $query->addTag('taxonomy_term_access');
Chris@0 336 if (!empty($vocabs)) {
Chris@0 337 $query->condition('td.vid', $vocabs, 'IN');
Chris@0 338 }
Chris@0 339 if (!empty($langcode)) {
Chris@0 340 $query->condition('td.langcode', $langcode);
Chris@0 341 }
Chris@0 342
Chris@0 343 $results = [];
Chris@0 344 $all_tids = [];
Chris@0 345 foreach ($query->execute() as $term_record) {
Chris@0 346 $results[$term_record->node_nid][] = $term_record->tid;
Chris@0 347 $all_tids[] = $term_record->tid;
Chris@0 348 }
Chris@0 349
Chris@0 350 $all_terms = $this->loadMultiple($all_tids);
Chris@0 351 $terms = [];
Chris@0 352 foreach ($results as $nid => $tids) {
Chris@0 353 foreach ($tids as $tid) {
Chris@0 354 $terms[$nid][$tid] = $all_terms[$tid];
Chris@0 355 }
Chris@0 356 }
Chris@0 357 return $terms;
Chris@0 358 }
Chris@0 359
Chris@0 360 /**
Chris@0 361 * {@inheritdoc}
Chris@0 362 */
Chris@0 363 public function __sleep() {
Chris@0 364 $vars = parent::__sleep();
Chris@0 365 // Do not serialize static cache.
Chris@17 366 unset($vars['ancestors'], $vars['treeChildren'], $vars['treeParents'], $vars['treeTerms'], $vars['trees']);
Chris@0 367 return $vars;
Chris@0 368 }
Chris@0 369
Chris@0 370 /**
Chris@0 371 * {@inheritdoc}
Chris@0 372 */
Chris@0 373 public function __wakeup() {
Chris@0 374 parent::__wakeup();
Chris@0 375 // Initialize static caches.
Chris@17 376 $this->ancestors = [];
Chris@0 377 $this->treeChildren = [];
Chris@0 378 $this->treeParents = [];
Chris@0 379 $this->treeTerms = [];
Chris@0 380 $this->trees = [];
Chris@0 381 }
Chris@0 382
Chris@0 383 }