3 namespace Drupal\taxonomy;
5 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
6 use Drupal\Core\Entity\EntityInterface;
9 * Defines a Controller class for taxonomy terms.
11 class TermStorage extends SqlContentEntityStorage implements TermStorageInterface {
14 * Array of loaded parents keyed by child term ID.
18 protected $parents = [];
21 * Array of all loaded term ancestry keyed by ancestor term ID.
25 protected $parentsAll = [];
28 * Array of child terms keyed by parent term ID.
32 protected $children = [];
35 * Array of term parents keyed by vocabulary ID and child term ID.
39 protected $treeParents = [];
42 * Array of term ancestors keyed by vocabulary ID and parent term ID.
46 protected $treeChildren = [];
49 * Array of terms in a tree keyed by vocabulary ID and term ID.
53 protected $treeTerms = [];
56 * Array of loaded trees keyed by a cache id matching tree arguments.
60 protected $trees = [];
65 * @param array $values
66 * An array of values to set, keyed by property name. A value for the
67 * vocabulary ID ('vid') is required.
69 public function create(array $values = []) {
70 // Save new terms with no parents by default.
71 if (empty($values['parent'])) {
72 $values['parent'] = [0];
74 $entity = parent::create($values);
81 public function resetCache(array $ids = NULL) {
82 drupal_static_reset('taxonomy_term_count_nodes');
84 $this->parentsAll = [];
86 $this->treeChildren = [];
87 $this->treeParents = [];
88 $this->treeTerms = [];
90 parent::resetCache($ids);
96 public function deleteTermHierarchy($tids) {
97 $this->database->delete('taxonomy_term_hierarchy')
98 ->condition('tid', $tids, 'IN')
105 public function updateTermHierarchy(EntityInterface $term) {
106 $query = $this->database->insert('taxonomy_term_hierarchy')
107 ->fields(['tid', 'parent']);
109 foreach ($term->parent as $parent) {
111 'tid' => $term->id(),
112 'parent' => (int) $parent->target_id,
121 public function loadParents($tid) {
122 if (!isset($this->parents[$tid])) {
124 $query = $this->database->select('taxonomy_term_field_data', 't');
125 $query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid');
126 $query->addField('t', 'tid');
127 $query->condition('h.tid', $tid);
128 $query->condition('t.default_langcode', 1);
129 $query->addTag('taxonomy_term_access');
130 $query->orderBy('t.weight');
131 $query->orderBy('t.name');
132 if ($ids = $query->execute()->fetchCol()) {
133 $parents = $this->loadMultiple($ids);
135 $this->parents[$tid] = $parents;
137 return $this->parents[$tid];
143 public function loadAllParents($tid) {
144 if (!isset($this->parentsAll[$tid])) {
146 if ($term = $this->load($tid)) {
147 $parents[$term->id()] = $term;
148 $terms_to_search[] = $term->id();
150 while ($tid = array_shift($terms_to_search)) {
151 if ($new_parents = $this->loadParents($tid)) {
152 foreach ($new_parents as $new_parent) {
153 if (!isset($parents[$new_parent->id()])) {
154 $parents[$new_parent->id()] = $new_parent;
155 $terms_to_search[] = $new_parent->id();
162 $this->parentsAll[$tid] = $parents;
164 return $this->parentsAll[$tid];
170 public function loadChildren($tid, $vid = NULL) {
171 if (!isset($this->children[$tid])) {
173 $query = $this->database->select('taxonomy_term_field_data', 't');
174 $query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
175 $query->addField('t', 'tid');
176 $query->condition('h.parent', $tid);
178 $query->condition('t.vid', $vid);
180 $query->condition('t.default_langcode', 1);
181 $query->addTag('taxonomy_term_access');
182 $query->orderBy('t.weight');
183 $query->orderBy('t.name');
184 if ($ids = $query->execute()->fetchCol()) {
185 $children = $this->loadMultiple($ids);
187 $this->children[$tid] = $children;
189 return $this->children[$tid];
195 public function loadTree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) {
196 $cache_key = implode(':', func_get_args());
197 if (!isset($this->trees[$cache_key])) {
198 // We cache trees, so it's not CPU-intensive to call on a term and its
200 if (!isset($this->treeChildren[$vid])) {
201 $this->treeChildren[$vid] = [];
202 $this->treeParents[$vid] = [];
203 $this->treeTerms[$vid] = [];
204 $query = $this->database->select('taxonomy_term_field_data', 't');
205 $query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
207 ->addTag('taxonomy_term_access')
209 ->fields('h', ['parent'])
210 ->condition('t.vid', $vid)
211 ->condition('t.default_langcode', 1)
212 ->orderBy('t.weight')
215 foreach ($result as $term) {
216 $this->treeChildren[$vid][$term->parent][] = $term->tid;
217 $this->treeParents[$vid][$term->tid][] = $term->parent;
218 $this->treeTerms[$vid][$term->tid] = $term;
222 // Load full entities, if necessary. The entity controller statically
223 // caches the results.
225 if ($load_entities) {
226 $term_entities = $this->loadMultiple(array_keys($this->treeTerms[$vid]));
229 $max_depth = (!isset($max_depth)) ? count($this->treeChildren[$vid]) : $max_depth;
232 // Keeps track of the parents we have to process, the last entry is used
233 // for the next processing step.
234 $process_parents = [];
235 $process_parents[] = $parent;
237 // Loops over the parent terms and adds its children to the tree array.
238 // Uses a loop instead of a recursion, because it's more efficient.
239 while (count($process_parents)) {
240 $parent = array_pop($process_parents);
241 // The number of parents determines the current depth.
242 $depth = count($process_parents);
243 if ($max_depth > $depth && !empty($this->treeChildren[$vid][$parent])) {
244 $has_children = FALSE;
245 $child = current($this->treeChildren[$vid][$parent]);
250 $term = $load_entities ? $term_entities[$child] : $this->treeTerms[$vid][$child];
251 if (isset($this->treeParents[$vid][$load_entities ? $term->id() : $term->tid])) {
252 // Clone the term so that the depth attribute remains correct
253 // in the event of multiple parents.
256 $term->depth = $depth;
257 unset($term->parent);
258 $tid = $load_entities ? $term->id() : $term->tid;
259 $term->parents = $this->treeParents[$vid][$tid];
261 if (!empty($this->treeChildren[$vid][$tid])) {
262 $has_children = TRUE;
264 // We have to continue with this parent later.
265 $process_parents[] = $parent;
266 // Use the current term as parent for the next iteration.
267 $process_parents[] = $tid;
269 // Reset pointers for child lists because we step in there more
270 // often with multi parents.
271 reset($this->treeChildren[$vid][$tid]);
272 // Move pointer so that we get the correct term the next time.
273 next($this->treeChildren[$vid][$parent]);
276 } while ($child = next($this->treeChildren[$vid][$parent]));
278 if (!$has_children) {
279 // We processed all terms in this hierarchy-level, reset pointer
280 // so that this function works the next time it gets called.
281 reset($this->treeChildren[$vid][$parent]);
285 $this->trees[$cache_key] = $tree;
287 return $this->trees[$cache_key];
293 public function nodeCount($vid) {
294 $query = $this->database->select('taxonomy_index', 'ti');
295 $query->addExpression('COUNT(DISTINCT ti.nid)');
296 $query->leftJoin('taxonomy_term_data', 'td', 'ti.tid = td.tid');
297 $query->condition('td.vid', $vid);
298 $query->addTag('vocabulary_node_count');
299 return $query->execute()->fetchField();
305 public function resetWeights($vid) {
306 $this->database->update('taxonomy_term_field_data')
307 ->fields(['weight' => 0])
308 ->condition('vid', $vid)
315 public function getNodeTerms(array $nids, array $vocabs = [], $langcode = NULL) {
316 $query = db_select('taxonomy_term_field_data', 'td');
317 $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
318 $query->fields('td', ['tid']);
319 $query->addField('tn', 'nid', 'node_nid');
320 $query->orderby('td.weight');
321 $query->orderby('td.name');
322 $query->condition('tn.nid', $nids, 'IN');
323 $query->addTag('taxonomy_term_access');
324 if (!empty($vocabs)) {
325 $query->condition('td.vid', $vocabs, 'IN');
327 if (!empty($langcode)) {
328 $query->condition('td.langcode', $langcode);
333 foreach ($query->execute() as $term_record) {
334 $results[$term_record->node_nid][] = $term_record->tid;
335 $all_tids[] = $term_record->tid;
338 $all_terms = $this->loadMultiple($all_tids);
340 foreach ($results as $nid => $tids) {
341 foreach ($tids as $tid) {
342 $terms[$nid][$tid] = $all_terms[$tid];
351 public function __sleep() {
352 $vars = parent::__sleep();
353 // Do not serialize static cache.
354 unset($vars['parents'], $vars['parentsAll'], $vars['children'], $vars['treeChildren'], $vars['treeParents'], $vars['treeTerms'], $vars['trees']);
361 public function __wakeup() {
363 // Initialize static caches.
365 $this->parentsAll = [];
366 $this->children = [];
367 $this->treeChildren = [];
368 $this->treeParents = [];
369 $this->treeTerms = [];