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