Mercurial > hg > isophonics-drupal-site
comparison core/modules/taxonomy/src/TermStorage.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\taxonomy; | |
4 | |
5 use Drupal\Core\Entity\Sql\SqlContentEntityStorage; | |
6 use Drupal\Core\Entity\EntityInterface; | |
7 | |
8 /** | |
9 * Defines a Controller class for taxonomy terms. | |
10 */ | |
11 class TermStorage extends SqlContentEntityStorage implements TermStorageInterface { | |
12 | |
13 /** | |
14 * Array of loaded parents keyed by child term ID. | |
15 * | |
16 * @var array | |
17 */ | |
18 protected $parents = []; | |
19 | |
20 /** | |
21 * Array of all loaded term ancestry keyed by ancestor term ID. | |
22 * | |
23 * @var array | |
24 */ | |
25 protected $parentsAll = []; | |
26 | |
27 /** | |
28 * Array of child terms keyed by parent term ID. | |
29 * | |
30 * @var array | |
31 */ | |
32 protected $children = []; | |
33 | |
34 /** | |
35 * Array of term parents keyed by vocabulary ID and child term ID. | |
36 * | |
37 * @var array | |
38 */ | |
39 protected $treeParents = []; | |
40 | |
41 /** | |
42 * Array of term ancestors keyed by vocabulary ID and parent term ID. | |
43 * | |
44 * @var array | |
45 */ | |
46 protected $treeChildren = []; | |
47 | |
48 /** | |
49 * Array of terms in a tree keyed by vocabulary ID and term ID. | |
50 * | |
51 * @var array | |
52 */ | |
53 protected $treeTerms = []; | |
54 | |
55 /** | |
56 * Array of loaded trees keyed by a cache id matching tree arguments. | |
57 * | |
58 * @var array | |
59 */ | |
60 protected $trees = []; | |
61 | |
62 /** | |
63 * {@inheritdoc} | |
64 * | |
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. | |
68 */ | |
69 public function create(array $values = []) { | |
70 // Save new terms with no parents by default. | |
71 if (empty($values['parent'])) { | |
72 $values['parent'] = [0]; | |
73 } | |
74 $entity = parent::create($values); | |
75 return $entity; | |
76 } | |
77 | |
78 /** | |
79 * {@inheritdoc} | |
80 */ | |
81 public function resetCache(array $ids = NULL) { | |
82 drupal_static_reset('taxonomy_term_count_nodes'); | |
83 $this->parents = []; | |
84 $this->parentsAll = []; | |
85 $this->children = []; | |
86 $this->treeChildren = []; | |
87 $this->treeParents = []; | |
88 $this->treeTerms = []; | |
89 $this->trees = []; | |
90 parent::resetCache($ids); | |
91 } | |
92 | |
93 /** | |
94 * {@inheritdoc} | |
95 */ | |
96 public function deleteTermHierarchy($tids) { | |
97 $this->database->delete('taxonomy_term_hierarchy') | |
98 ->condition('tid', $tids, 'IN') | |
99 ->execute(); | |
100 } | |
101 | |
102 /** | |
103 * {@inheritdoc} | |
104 */ | |
105 public function updateTermHierarchy(EntityInterface $term) { | |
106 $query = $this->database->insert('taxonomy_term_hierarchy') | |
107 ->fields(['tid', 'parent']); | |
108 | |
109 foreach ($term->parent as $parent) { | |
110 $query->values([ | |
111 'tid' => $term->id(), | |
112 'parent' => (int) $parent->target_id, | |
113 ]); | |
114 } | |
115 $query->execute(); | |
116 } | |
117 | |
118 /** | |
119 * {@inheritdoc} | |
120 */ | |
121 public function loadParents($tid) { | |
122 if (!isset($this->parents[$tid])) { | |
123 $parents = []; | |
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); | |
134 } | |
135 $this->parents[$tid] = $parents; | |
136 } | |
137 return $this->parents[$tid]; | |
138 } | |
139 | |
140 /** | |
141 * {@inheritdoc} | |
142 */ | |
143 public function loadAllParents($tid) { | |
144 if (!isset($this->parentsAll[$tid])) { | |
145 $parents = []; | |
146 if ($term = $this->load($tid)) { | |
147 $parents[$term->id()] = $term; | |
148 $terms_to_search[] = $term->id(); | |
149 | |
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(); | |
156 } | |
157 } | |
158 } | |
159 } | |
160 } | |
161 | |
162 $this->parentsAll[$tid] = $parents; | |
163 } | |
164 return $this->parentsAll[$tid]; | |
165 } | |
166 | |
167 /** | |
168 * {@inheritdoc} | |
169 */ | |
170 public function loadChildren($tid, $vid = NULL) { | |
171 if (!isset($this->children[$tid])) { | |
172 $children = []; | |
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); | |
177 if ($vid) { | |
178 $query->condition('t.vid', $vid); | |
179 } | |
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); | |
186 } | |
187 $this->children[$tid] = $children; | |
188 } | |
189 return $this->children[$tid]; | |
190 } | |
191 | |
192 /** | |
193 * {@inheritdoc} | |
194 */ | |
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 | |
199 // children, too. | |
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'); | |
206 $result = $query | |
207 ->addTag('taxonomy_term_access') | |
208 ->fields('t') | |
209 ->fields('h', ['parent']) | |
210 ->condition('t.vid', $vid) | |
211 ->condition('t.default_langcode', 1) | |
212 ->orderBy('t.weight') | |
213 ->orderBy('t.name') | |
214 ->execute(); | |
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; | |
219 } | |
220 } | |
221 | |
222 // Load full entities, if necessary. The entity controller statically | |
223 // caches the results. | |
224 $term_entities = []; | |
225 if ($load_entities) { | |
226 $term_entities = $this->loadMultiple(array_keys($this->treeTerms[$vid])); | |
227 } | |
228 | |
229 $max_depth = (!isset($max_depth)) ? count($this->treeChildren[$vid]) : $max_depth; | |
230 $tree = []; | |
231 | |
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; | |
236 | |
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]); | |
246 do { | |
247 if (empty($child)) { | |
248 break; | |
249 } | |
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. | |
254 $term = clone $term; | |
255 } | |
256 $term->depth = $depth; | |
257 unset($term->parent); | |
258 $tid = $load_entities ? $term->id() : $term->tid; | |
259 $term->parents = $this->treeParents[$vid][$tid]; | |
260 $tree[] = $term; | |
261 if (!empty($this->treeChildren[$vid][$tid])) { | |
262 $has_children = TRUE; | |
263 | |
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; | |
268 | |
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]); | |
274 break; | |
275 } | |
276 } while ($child = next($this->treeChildren[$vid][$parent])); | |
277 | |
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]); | |
282 } | |
283 } | |
284 } | |
285 $this->trees[$cache_key] = $tree; | |
286 } | |
287 return $this->trees[$cache_key]; | |
288 } | |
289 | |
290 /** | |
291 * {@inheritdoc} | |
292 */ | |
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(); | |
300 } | |
301 | |
302 /** | |
303 * {@inheritdoc} | |
304 */ | |
305 public function resetWeights($vid) { | |
306 $this->database->update('taxonomy_term_field_data') | |
307 ->fields(['weight' => 0]) | |
308 ->condition('vid', $vid) | |
309 ->execute(); | |
310 } | |
311 | |
312 /** | |
313 * {@inheritdoc} | |
314 */ | |
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'); | |
326 } | |
327 if (!empty($langcode)) { | |
328 $query->condition('td.langcode', $langcode); | |
329 } | |
330 | |
331 $results = []; | |
332 $all_tids = []; | |
333 foreach ($query->execute() as $term_record) { | |
334 $results[$term_record->node_nid][] = $term_record->tid; | |
335 $all_tids[] = $term_record->tid; | |
336 } | |
337 | |
338 $all_terms = $this->loadMultiple($all_tids); | |
339 $terms = []; | |
340 foreach ($results as $nid => $tids) { | |
341 foreach ($tids as $tid) { | |
342 $terms[$nid][$tid] = $all_terms[$tid]; | |
343 } | |
344 } | |
345 return $terms; | |
346 } | |
347 | |
348 /** | |
349 * {@inheritdoc} | |
350 */ | |
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']); | |
355 return $vars; | |
356 } | |
357 | |
358 /** | |
359 * {@inheritdoc} | |
360 */ | |
361 public function __wakeup() { | |
362 parent::__wakeup(); | |
363 // Initialize static caches. | |
364 $this->parents = []; | |
365 $this->parentsAll = []; | |
366 $this->children = []; | |
367 $this->treeChildren = []; | |
368 $this->treeParents = []; | |
369 $this->treeTerms = []; | |
370 $this->trees = []; | |
371 } | |
372 | |
373 } |