Mercurial > hg > isophonics-drupal-site
comparison core/modules/taxonomy/taxonomy.module @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Enables the organization of content into categories. | |
6 */ | |
7 | |
8 use Drupal\Component\Utility\Tags; | |
9 use Drupal\Component\Utility\Unicode; | |
10 use Drupal\Core\Entity\EntityInterface; | |
11 use Drupal\Core\Entity\Sql\SqlContentEntityStorage; | |
12 use Drupal\Core\Render\Element; | |
13 use Drupal\Core\Routing\RouteMatchInterface; | |
14 use Drupal\Core\Url; | |
15 use Drupal\taxonomy\Entity\Term; | |
16 use Drupal\taxonomy\Entity\Vocabulary; | |
17 use Drupal\taxonomy\TermInterface; | |
18 use Drupal\taxonomy\VocabularyInterface; | |
19 | |
20 /** | |
21 * Denotes that no term in the vocabulary has a parent. | |
22 * | |
23 * @deprecated in Drupal 8.2.x and will be removed before 9.0.0. Use | |
24 * \Drupal\taxonomy\VocabularyInterface::HIERARCHY_DISABLED instead. | |
25 * | |
26 * @see https://www.drupal.org/node/2807795 | |
27 */ | |
28 const TAXONOMY_HIERARCHY_DISABLED = 0; | |
29 | |
30 /** | |
31 * Denotes that one or more terms in the vocabulary has a single parent. | |
32 * | |
33 * @deprecated in Drupal 8.2.x and will be removed before 9.0.0. Use | |
34 * \Drupal\taxonomy\VocabularyInterface::HIERARCHY_SINGLE instead. | |
35 * | |
36 * @see https://www.drupal.org/node/2807795 | |
37 */ | |
38 const TAXONOMY_HIERARCHY_SINGLE = 1; | |
39 | |
40 /** | |
41 * Denotes that one or more terms in the vocabulary have multiple parents. | |
42 * | |
43 * @deprecated in Drupal 8.2.x and will be removed before 9.0.0. Use | |
44 * \Drupal\taxonomy\VocabularyInterface::HIERARCHY_MULTIPLE instead. | |
45 * | |
46 * @see https://www.drupal.org/node/2807795 | |
47 */ | |
48 const TAXONOMY_HIERARCHY_MULTIPLE = 2; | |
49 | |
50 /** | |
51 * Implements hook_help(). | |
52 */ | |
53 function taxonomy_help($route_name, RouteMatchInterface $route_match) { | |
54 switch ($route_name) { | |
55 case 'help.page.taxonomy': | |
56 $field_ui_url = \Drupal::moduleHandler()->moduleExists('field_ui') ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#'; | |
57 $output = ''; | |
58 $output .= '<h3>' . t('About') . '</h3>'; | |
59 $output .= '<p>' . t('The Taxonomy module allows users who have permission to create and edit content to categorize (tag) content of that type. Users who have the <em>Administer vocabularies and terms</em> <a href=":permissions" title="Taxonomy module permissions">permission</a> can add <em>vocabularies</em> that contain a set of related <em>terms</em>. The terms in a vocabulary can either be pre-set by an administrator or built gradually as content is added and edited. Terms may be organized hierarchically if desired.', [':permissions' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-taxonomy'])]) . '</p>'; | |
60 $output .= '<p>' . t('For more information, see the <a href=":taxonomy">online documentation for the Taxonomy module</a>.', [':taxonomy' => 'https://www.drupal.org/documentation/modules/taxonomy/']) . '</p>'; | |
61 $output .= '<h3>' . t('Uses') . '</h3>'; | |
62 $output .= '<dl>'; | |
63 $output .= '<dt>' . t('Managing vocabularies') . '</dt>'; | |
64 $output .= '<dd>' . t('Users who have the <em>Administer vocabularies and terms</em> permission can add and edit vocabularies from the <a href=":taxonomy_admin">Taxonomy administration page</a>. Vocabularies can be deleted from their <em>Edit vocabulary</em> page. Users with the <em>Taxonomy term: Administer fields</em> permission may add additional fields for terms in that vocabulary using the <a href=":field_ui">Field UI module</a>.', [':taxonomy_admin' => \Drupal::url('entity.taxonomy_vocabulary.collection'), ':field_ui' => $field_ui_url]) . '</dd>'; | |
65 $output .= '<dt>' . t('Managing terms') . '</dt>'; | |
66 $output .= '<dd>' . t('Users who have the <em>Administer vocabularies and terms</em> permission or the <em>Edit terms</em> permission for a particular vocabulary can add, edit, and organize the terms in a vocabulary from a vocabulary\'s term listing page, which can be accessed by going to the <a href=":taxonomy_admin">Taxonomy administration page</a> and clicking <em>List terms</em> in the <em>Operations</em> column. Users must have the <em>Administer vocabularies and terms</em> permission or the <em>Delete terms</em> permission for a particular vocabulary to delete terms.', [':taxonomy_admin' => \Drupal::url('entity.taxonomy_vocabulary.collection')]) . ' </dd>'; | |
67 $output .= '<dt>' . t('Classifying entity content') . '</dt>'; | |
68 $output .= '<dd>' . t('A user with the <em>Administer fields</em> permission for a certain entity type may add <em>Taxonomy term</em> reference fields to the entity type, which will allow entities to be classified using taxonomy terms. See the <a href=":entity_reference">Entity Reference help</a> for more information about reference fields. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them.', [':field_ui' => $field_ui_url, ':field' => \Drupal::url('help.page', ['name' => 'field']), ':entity_reference' => \Drupal::url('help.page', ['name' => 'entity_reference'])]) . '</dd>'; | |
69 $output .= '<dt>' . t('Adding new terms during content creation') . '</dt>'; | |
70 $output .= '<dd>' . t("Allowing users to add new terms gradually builds a vocabulary as content is added and edited. Users can add new terms if either of the two <em>Autocomplete</em> widgets is chosen for the Taxonomy term reference field in the <em>Manage form display</em> page for the field. You will also need to enable the <em>Create referenced entities if they don't already exist</em> option, and restrict the field to one vocabulary.") . '</dd>'; | |
71 $output .= '<dt>' . t('Configuring displays and form displays') . '</dt>'; | |
72 $output .= '<dd>' . t('See the <a href=":entity_reference">Entity Reference help</a> page for the field widgets and formatters that can be configured for any reference field on the <em>Manage display</em> and <em>Manage form display</em> pages. Taxonomy additionally provides an <em>RSS category</em> formatter that displays nothing when the entity item is displayed as HTML, but displays an RSS category instead of a list when the entity item is displayed in an RSS feed.', [':entity_reference' => \Drupal::url('help.page', ['name' => 'entity_reference'])]) . '</li>'; | |
73 $output .= '</ul>'; | |
74 $output .= '</dd>'; | |
75 $output .= '</dl>'; | |
76 return $output; | |
77 | |
78 case 'entity.taxonomy_vocabulary.collection': | |
79 $output = '<p>' . t('Taxonomy is for categorizing content. Terms are grouped into vocabularies. For example, a vocabulary called "Fruit" would contain the terms "Apple" and "Banana".') . '</p>'; | |
80 return $output; | |
81 | |
82 case 'entity.taxonomy_vocabulary.overview_form': | |
83 $vocabulary = $route_match->getParameter('taxonomy_vocabulary'); | |
84 switch ($vocabulary->getHierarchy()) { | |
85 case VocabularyInterface::HIERARCHY_DISABLED: | |
86 return '<p>' . t('You can reorganize the terms in %capital_name using their drag-and-drop handles, and group terms under a parent term by sliding them under and to the right of the parent.', ['%capital_name' => Unicode::ucfirst($vocabulary->label()), '%name' => $vocabulary->label()]) . '</p>'; | |
87 case VocabularyInterface::HIERARCHY_SINGLE: | |
88 return '<p>' . t('%capital_name contains terms grouped under parent terms. You can reorganize the terms in %capital_name using their drag-and-drop handles.', ['%capital_name' => Unicode::ucfirst($vocabulary->label()), '%name' => $vocabulary->label()]) . '</p>'; | |
89 case VocabularyInterface::HIERARCHY_MULTIPLE: | |
90 return '<p>' . t('%capital_name contains terms with multiple parents. Drag and drop of terms with multiple parents is not supported, but you can re-enable drag-and-drop support by editing each term to include only a single parent.', ['%capital_name' => Unicode::ucfirst($vocabulary->label())]) . '</p>'; | |
91 } | |
92 } | |
93 } | |
94 | |
95 /** | |
96 * Entity URI callback. | |
97 */ | |
98 function taxonomy_term_uri($term) { | |
99 return new Url('entity.taxonomy_term.canonical', [ | |
100 'taxonomy_term' => $term->id(), | |
101 ]); | |
102 } | |
103 | |
104 /** | |
105 * Implements hook_page_attachments_alter(). | |
106 */ | |
107 function taxonomy_page_attachments_alter(array &$page) { | |
108 $route_match = \Drupal::routeMatch(); | |
109 if ($route_match->getRouteName() == 'entity.taxonomy_term.canonical' && ($term = $route_match->getParameter('taxonomy_term')) && $term instanceof TermInterface) { | |
110 foreach ($term->uriRelationships() as $rel) { | |
111 // Set the URI relationships, like canonical. | |
112 $page['#attached']['html_head_link'][] = [ | |
113 [ | |
114 'rel' => $rel, | |
115 'href' => $term->url($rel), | |
116 ], | |
117 TRUE, | |
118 ]; | |
119 | |
120 // Set the term path as the canonical URL to prevent duplicate content. | |
121 if ($rel == 'canonical') { | |
122 // Set the non-aliased canonical path as a default shortlink. | |
123 $page['#attached']['html_head_link'][] = [ | |
124 [ | |
125 'rel' => 'shortlink', | |
126 'href' => $term->url($rel, ['alias' => TRUE]), | |
127 ], | |
128 TRUE, | |
129 ]; | |
130 } | |
131 } | |
132 } | |
133 } | |
134 | |
135 /** | |
136 * Implements hook_theme(). | |
137 */ | |
138 function taxonomy_theme() { | |
139 return [ | |
140 'taxonomy_term' => [ | |
141 'render element' => 'elements', | |
142 ], | |
143 ]; | |
144 } | |
145 | |
146 /** | |
147 * Checks and updates the hierarchy flag of a vocabulary. | |
148 * | |
149 * Checks the current parents of all terms in a vocabulary and updates the | |
150 * vocabulary's hierarchy setting to the lowest possible level. If no term | |
151 * has parent terms then the vocabulary will be given a hierarchy of | |
152 * VocabularyInterface::HIERARCHY_DISABLED. If any term has a single parent then | |
153 * the vocabulary will be given a hierarchy of | |
154 * VocabularyInterface::HIERARCHY_SINGLE. If any term has multiple parents then | |
155 * the vocabulary will be given a hierarchy of | |
156 * VocabularyInterface::HIERARCHY_MULTIPLE. | |
157 * | |
158 * @param \Drupal\taxonomy\VocabularyInterface $vocabulary | |
159 * A taxonomy vocabulary entity. | |
160 * @param $changed_term | |
161 * An array of the term structure that was updated. | |
162 * | |
163 * @return | |
164 * An integer that represents the level of the vocabulary's hierarchy. | |
165 */ | |
166 function taxonomy_check_vocabulary_hierarchy(VocabularyInterface $vocabulary, $changed_term) { | |
167 $tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vocabulary->id()); | |
168 $hierarchy = VocabularyInterface::HIERARCHY_DISABLED; | |
169 foreach ($tree as $term) { | |
170 // Update the changed term with the new parent value before comparison. | |
171 if ($term->tid == $changed_term['tid']) { | |
172 $term = (object) $changed_term; | |
173 $term->parents = $term->parent; | |
174 } | |
175 // Check this term's parent count. | |
176 if (count($term->parents) > 1) { | |
177 $hierarchy = VocabularyInterface::HIERARCHY_MULTIPLE; | |
178 break; | |
179 } | |
180 elseif (count($term->parents) == 1 && !isset($term->parents[0])) { | |
181 $hierarchy = VocabularyInterface::HIERARCHY_SINGLE; | |
182 } | |
183 } | |
184 if ($hierarchy != $vocabulary->getHierarchy()) { | |
185 $vocabulary->setHierarchy($hierarchy); | |
186 $vocabulary->save(); | |
187 } | |
188 | |
189 return $hierarchy; | |
190 } | |
191 | |
192 /** | |
193 * Generates an array which displays a term detail page. | |
194 * | |
195 * @param \Drupal\taxonomy\Entity\Term $term | |
196 * A taxonomy term object. | |
197 * @param string $view_mode | |
198 * View mode; e.g., 'full', 'teaser', etc. | |
199 * @param string $langcode | |
200 * (optional) A language code to use for rendering. Defaults to the global | |
201 * content language of the current request. | |
202 * | |
203 * @return array | |
204 * A $page element suitable for use by drupal_render(). | |
205 */ | |
206 function taxonomy_term_view(Term $term, $view_mode = 'full', $langcode = NULL) { | |
207 return entity_view($term, $view_mode, $langcode); | |
208 } | |
209 | |
210 /** | |
211 * Constructs a drupal_render() style array from an array of loaded terms. | |
212 * | |
213 * @param array $terms | |
214 * An array of taxonomy terms as returned by Term::loadMultiple(). | |
215 * @param string $view_mode | |
216 * View mode; e.g., 'full', 'teaser', etc. | |
217 * @param string $langcode | |
218 * (optional) A language code to use for rendering. Defaults to the global | |
219 * content language of the current request. | |
220 * | |
221 * @return array | |
222 * An array in the format expected by drupal_render(). | |
223 */ | |
224 function taxonomy_term_view_multiple(array $terms, $view_mode = 'full', $langcode = NULL) { | |
225 return entity_view_multiple($terms, $view_mode, $langcode); | |
226 } | |
227 | |
228 /** | |
229 * Implements hook_theme_suggestions_HOOK(). | |
230 */ | |
231 function taxonomy_theme_suggestions_taxonomy_term(array $variables) { | |
232 $suggestions = []; | |
233 | |
234 /** @var \Drupal\taxonomy\TermInterface $term */ | |
235 $term = $variables['elements']['#taxonomy_term']; | |
236 | |
237 $suggestions[] = 'taxonomy_term__' . $term->bundle(); | |
238 $suggestions[] = 'taxonomy_term__' . $term->id(); | |
239 | |
240 return $suggestions; | |
241 } | |
242 | |
243 /** | |
244 * Prepares variables for taxonomy term templates. | |
245 * | |
246 * Default template: taxonomy-term.html.twig. | |
247 * | |
248 * @param array $variables | |
249 * An associative array containing: | |
250 * - elements: An associative array containing the taxonomy term and any | |
251 * fields attached to the term. Properties used: | |
252 * - #taxonomy_term: A \Drupal\taxonomy\TermInterface object. | |
253 * - #view_mode: The current view mode for this taxonomy term, e.g. | |
254 * 'full' or 'teaser'. | |
255 * - attributes: HTML attributes for the containing element. | |
256 */ | |
257 function template_preprocess_taxonomy_term(&$variables) { | |
258 $variables['view_mode'] = $variables['elements']['#view_mode']; | |
259 $variables['term'] = $variables['elements']['#taxonomy_term']; | |
260 /** @var \Drupal\taxonomy\TermInterface $term */ | |
261 $term = $variables['term']; | |
262 | |
263 $variables['url'] = $term->url(); | |
264 // We use name here because that is what appears in the UI. | |
265 $variables['name'] = $variables['elements']['name']; | |
266 unset($variables['elements']['name']); | |
267 $variables['page'] = $variables['view_mode'] == 'full' && taxonomy_term_is_page($term); | |
268 | |
269 // Helpful $content variable for templates. | |
270 $variables['content'] = []; | |
271 foreach (Element::children($variables['elements']) as $key) { | |
272 $variables['content'][$key] = $variables['elements'][$key]; | |
273 } | |
274 } | |
275 | |
276 /** | |
277 * Returns whether the current page is the page of the passed-in term. | |
278 * | |
279 * @param \Drupal\taxonomy\Entity\Term $term | |
280 * A taxonomy term entity. | |
281 */ | |
282 function taxonomy_term_is_page(Term $term) { | |
283 if (\Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_term.canonical' && $page_term_id = \Drupal::routeMatch()->getRawParameter('taxonomy_term')) { | |
284 return $page_term_id == $term->id(); | |
285 } | |
286 return FALSE; | |
287 } | |
288 | |
289 /** | |
290 * Clear all static cache variables for terms. | |
291 */ | |
292 function taxonomy_terms_static_reset() { | |
293 \Drupal::entityManager()->getStorage('taxonomy_term')->resetCache(); | |
294 } | |
295 | |
296 /** | |
297 * Clear all static cache variables for vocabularies. | |
298 * | |
299 * @param $ids | |
300 * An array of ids to reset in the entity cache. | |
301 */ | |
302 function taxonomy_vocabulary_static_reset(array $ids = NULL) { | |
303 \Drupal::entityManager()->getStorage('taxonomy_vocabulary')->resetCache($ids); | |
304 } | |
305 | |
306 /** | |
307 * Get names for all taxonomy vocabularies. | |
308 * | |
309 * @return array | |
310 * A list of existing vocabulary IDs. | |
311 */ | |
312 function taxonomy_vocabulary_get_names() { | |
313 $names = &drupal_static(__FUNCTION__); | |
314 | |
315 if (!isset($names)) { | |
316 $names = []; | |
317 $config_names = \Drupal::configFactory()->listAll('taxonomy.vocabulary.'); | |
318 foreach ($config_names as $config_name) { | |
319 $id = substr($config_name, strlen('taxonomy.vocabulary.')); | |
320 $names[$id] = $id; | |
321 } | |
322 } | |
323 | |
324 return $names; | |
325 } | |
326 | |
327 /** | |
328 * Try to map a string to an existing term, as for glossary use. | |
329 * | |
330 * Provides a case-insensitive and trimmed mapping, to maximize the | |
331 * likelihood of a successful match. | |
332 * | |
333 * @param $name | |
334 * Name of the term to search for. | |
335 * @param $vocabulary | |
336 * (optional) Vocabulary machine name to limit the search. Defaults to NULL. | |
337 * | |
338 * @return | |
339 * An array of matching term objects. | |
340 */ | |
341 function taxonomy_term_load_multiple_by_name($name, $vocabulary = NULL) { | |
342 $values = ['name' => trim($name)]; | |
343 if (isset($vocabulary)) { | |
344 $vocabularies = taxonomy_vocabulary_get_names(); | |
345 if (isset($vocabularies[$vocabulary])) { | |
346 $values['vid'] = $vocabulary; | |
347 } | |
348 else { | |
349 // Return an empty array when filtering by a non-existing vocabulary. | |
350 return []; | |
351 } | |
352 } | |
353 return entity_load_multiple_by_properties('taxonomy_term', $values); | |
354 } | |
355 | |
356 /** | |
357 * Load multiple taxonomy terms based on certain conditions. | |
358 * | |
359 * This function should be used whenever you need to load more than one term | |
360 * from the database. Terms are loaded into memory and will not require | |
361 * database access if loaded again during the same page request. | |
362 * | |
363 * @see entity_load_multiple() | |
364 * @see \Drupal\Core\Entity\Query\EntityQueryInterface | |
365 * | |
366 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0. | |
367 * Use \Drupal\taxonomy\Entity\Term::loadMultiple(). | |
368 * | |
369 * @param array $tids | |
370 * (optional) An array of entity IDs. If omitted, all entities are loaded. | |
371 * | |
372 * @return array | |
373 * An array of taxonomy term entities, indexed by tid. When no results are | |
374 * found, an empty array is returned. | |
375 */ | |
376 function taxonomy_term_load_multiple(array $tids = NULL) { | |
377 return Term::loadMultiple($tids); | |
378 } | |
379 | |
380 /** | |
381 * Loads multiple taxonomy vocabularies based on certain conditions. | |
382 * | |
383 * This function should be used whenever you need to load more than one | |
384 * vocabulary from the database. Terms are loaded into memory and will not | |
385 * require database access if loaded again during the same page request. | |
386 * | |
387 * @see entity_load_multiple() | |
388 * | |
389 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0. | |
390 * Use \Drupal\taxonomy\Entity\Vocabulary::loadMultiple(). | |
391 * | |
392 * @param array $vids | |
393 * (optional) An array of entity IDs. If omitted, all entities are loaded. | |
394 * | |
395 * @return array | |
396 * An array of vocabulary objects, indexed by vid. | |
397 */ | |
398 function taxonomy_vocabulary_load_multiple(array $vids = NULL) { | |
399 return Vocabulary::loadMultiple($vids); | |
400 } | |
401 | |
402 /** | |
403 * Return the taxonomy vocabulary entity matching a vocabulary ID. | |
404 * | |
405 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0. | |
406 * Use \Drupal\taxonomy\Entity\Vocabulary::load(). | |
407 * | |
408 * @param int $vid | |
409 * The vocabulary's ID. | |
410 * | |
411 * @return \Drupal\taxonomy\Entity\Vocabulary|null | |
412 * The taxonomy vocabulary entity, if exists, NULL otherwise. Results are | |
413 * statically cached. | |
414 */ | |
415 function taxonomy_vocabulary_load($vid) { | |
416 return Vocabulary::load($vid); | |
417 } | |
418 | |
419 /** | |
420 * Return the taxonomy term entity matching a term ID. | |
421 * | |
422 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0. | |
423 * Use \Drupal\taxonomy\Entity\Term::load(). | |
424 * | |
425 * @param $tid | |
426 * A term's ID | |
427 * | |
428 * @return \Drupal\taxonomy\Entity\Term|null | |
429 * A taxonomy term entity, or NULL if the term was not found. Results are | |
430 * statically cached. | |
431 */ | |
432 function taxonomy_term_load($tid) { | |
433 if (!is_numeric($tid)) { | |
434 return NULL; | |
435 } | |
436 return Term::load($tid); | |
437 } | |
438 | |
439 /** | |
440 * Implodes a list of tags of a certain vocabulary into a string. | |
441 * | |
442 * @see \Drupal\Component\Utility\Tags::explode() | |
443 */ | |
444 function taxonomy_implode_tags($tags, $vid = NULL) { | |
445 $typed_tags = []; | |
446 foreach ($tags as $tag) { | |
447 // Extract terms belonging to the vocabulary in question. | |
448 if (!isset($vid) || $tag->bundle() == $vid) { | |
449 // Make sure we have a completed loaded taxonomy term. | |
450 if ($tag instanceof EntityInterface && $label = $tag->label()) { | |
451 // Commas and quotes in tag names are special cases, so encode 'em. | |
452 $typed_tags[] = Tags::encode($label); | |
453 } | |
454 } | |
455 } | |
456 return implode(', ', $typed_tags); | |
457 } | |
458 | |
459 /** | |
460 * Title callback for term pages. | |
461 * | |
462 * @param \Drupal\taxonomy\Entity\Term $term | |
463 * A taxonomy term entity. | |
464 * | |
465 * @return | |
466 * The term name to be used as the page title. | |
467 */ | |
468 function taxonomy_term_title(Term $term) { | |
469 return $term->getName(); | |
470 } | |
471 | |
472 /** | |
473 * @defgroup taxonomy_index Taxonomy indexing | |
474 * @{ | |
475 * Functions to maintain taxonomy indexing. | |
476 * | |
477 * Taxonomy uses default field storage to store canonical relationships | |
478 * between terms and fieldable entities. However its most common use case | |
479 * requires listing all content associated with a term or group of terms | |
480 * sorted by creation date. To avoid slow queries due to joining across | |
481 * multiple node and field tables with various conditions and order by criteria, | |
482 * we maintain a denormalized table with all relationships between terms, | |
483 * published nodes and common sort criteria such as status, sticky and created. | |
484 * When using other field storage engines or alternative methods of | |
485 * denormalizing this data you should set the | |
486 * taxonomy.settings:maintain_index_table to '0' to avoid unnecessary writes in | |
487 * SQL. | |
488 */ | |
489 | |
490 /** | |
491 * Implements hook_ENTITY_TYPE_insert() for node entities. | |
492 */ | |
493 function taxonomy_node_insert(EntityInterface $node) { | |
494 // Add taxonomy index entries for the node. | |
495 taxonomy_build_node_index($node); | |
496 } | |
497 | |
498 /** | |
499 * Builds and inserts taxonomy index entries for a given node. | |
500 * | |
501 * The index lists all terms that are related to a given node entity, and is | |
502 * therefore maintained at the entity level. | |
503 * | |
504 * @param \Drupal\node\Entity\Node $node | |
505 * The node entity. | |
506 */ | |
507 function taxonomy_build_node_index($node) { | |
508 // We maintain a denormalized table of term/node relationships, containing | |
509 // only data for current, published nodes. | |
510 if (!\Drupal::config('taxonomy.settings')->get('maintain_index_table') || !(\Drupal::entityManager()->getStorage('node') instanceof SqlContentEntityStorage)) { | |
511 return; | |
512 } | |
513 | |
514 $status = $node->isPublished(); | |
515 $sticky = (int) $node->isSticky(); | |
516 // We only maintain the taxonomy index for published nodes. | |
517 if ($status && $node->isDefaultRevision()) { | |
518 // Collect a unique list of all the term IDs from all node fields. | |
519 $tid_all = []; | |
520 $entity_reference_class = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem'; | |
521 foreach ($node->getFieldDefinitions() as $field) { | |
522 $field_name = $field->getName(); | |
523 $class = $field->getItemDefinition()->getClass(); | |
524 $is_entity_reference_class = ($class === $entity_reference_class) || is_subclass_of($class, $entity_reference_class); | |
525 if ($is_entity_reference_class && $field->getSetting('target_type') == 'taxonomy_term') { | |
526 foreach ($node->getTranslationLanguages() as $language) { | |
527 foreach ($node->getTranslation($language->getId())->$field_name as $item) { | |
528 if (!$item->isEmpty()) { | |
529 $tid_all[$item->target_id] = $item->target_id; | |
530 } | |
531 } | |
532 } | |
533 } | |
534 } | |
535 // Insert index entries for all the node's terms. | |
536 if (!empty($tid_all)) { | |
537 foreach ($tid_all as $tid) { | |
538 db_merge('taxonomy_index') | |
539 ->key(['nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished()]) | |
540 ->fields(['sticky' => $sticky, 'created' => $node->getCreatedTime()]) | |
541 ->execute(); | |
542 } | |
543 } | |
544 } | |
545 } | |
546 | |
547 /** | |
548 * Implements hook_ENTITY_TYPE_update() for node entities. | |
549 */ | |
550 function taxonomy_node_update(EntityInterface $node) { | |
551 // If we're not dealing with the default revision of the node, do not make any | |
552 // change to the taxonomy index. | |
553 if (!$node->isDefaultRevision()) { | |
554 return; | |
555 } | |
556 taxonomy_delete_node_index($node); | |
557 taxonomy_build_node_index($node); | |
558 } | |
559 | |
560 /** | |
561 * Implements hook_ENTITY_TYPE_predelete() for node entities. | |
562 */ | |
563 function taxonomy_node_predelete(EntityInterface $node) { | |
564 // Clean up the {taxonomy_index} table when nodes are deleted. | |
565 taxonomy_delete_node_index($node); | |
566 } | |
567 | |
568 /** | |
569 * Deletes taxonomy index entries for a given node. | |
570 * | |
571 * @param \Drupal\Core\Entity\EntityInterface $node | |
572 * The node entity. | |
573 */ | |
574 function taxonomy_delete_node_index(EntityInterface $node) { | |
575 if (\Drupal::config('taxonomy.settings')->get('maintain_index_table')) { | |
576 db_delete('taxonomy_index')->condition('nid', $node->id())->execute(); | |
577 } | |
578 } | |
579 | |
580 /** | |
581 * Implements hook_ENTITY_TYPE_delete() for taxonomy_term entities. | |
582 */ | |
583 function taxonomy_taxonomy_term_delete(Term $term) { | |
584 if (\Drupal::config('taxonomy.settings')->get('maintain_index_table')) { | |
585 // Clean up the {taxonomy_index} table when terms are deleted. | |
586 db_delete('taxonomy_index')->condition('tid', $term->id())->execute(); | |
587 } | |
588 } | |
589 | |
590 /** | |
591 * @} End of "defgroup taxonomy_index". | |
592 */ |