2002-04-14 20:46:41 +00:00
<?php
2002-04-20 11:52:50 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
* Enables the organization of content into categories.
*/
2014-02-13 11:58:51 +00:00
use Drupal\Component\Utility\Tags;
2013-09-01 06:20:08 +00:00
use Drupal\Core\Entity\EntityInterface;
SA-CORE-2016-005 by larowlan, xjm, David_Rothstein, Dave Reid, Crell, cilefen, alexpott, mlhess, catch, pwolanin, YesCT, dawehner, quicksketch, Heine, znerol, charlotte.b, jnicola, ezraw
(cherry picked from commit c5da97f9e7e8f145541c5422e3ccde6a8b5ae680)
2016-11-16 18:45:27 +00:00
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
2014-03-31 17:37:55 +00:00
use Drupal\Core\Render\Element;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
2014-03-28 23:07:00 +00:00
use Drupal\Core\Url;
2013-08-18 21:16:19 +00:00
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
use Drupal\taxonomy\TermInterface;
2013-09-01 06:20:08 +00:00
use Drupal\taxonomy\VocabularyInterface;
2012-04-26 16:44:37 +00:00
2011-12-23 03:42:04 +00:00
/**
* Denotes that no term in the vocabulary has a parent.
2016-09-28 15:46:58 +00:00
*
* @deprecated in Drupal 8.2.x and will be removed before 9.0.0. Use
* \Drupal\taxonomy\VocabularyInterface::HIERARCHY_DISABLED instead.
2017-10-10 07:30:23 +00:00
*
* @see https://www.drupal.org/node/2807795
2011-12-23 03:42:04 +00:00
*/
const TAXONOMY_HIERARCHY_DISABLED = 0;
/**
* Denotes that one or more terms in the vocabulary has a single parent.
2016-09-28 15:46:58 +00:00
*
* @deprecated in Drupal 8.2.x and will be removed before 9.0.0. Use
* \Drupal\taxonomy\VocabularyInterface::HIERARCHY_SINGLE instead.
2017-10-10 07:30:23 +00:00
*
* @see https://www.drupal.org/node/2807795
2011-12-23 03:42:04 +00:00
*/
const TAXONOMY_HIERARCHY_SINGLE = 1;
/**
* Denotes that one or more terms in the vocabulary have multiple parents.
2016-09-28 15:46:58 +00:00
*
* @deprecated in Drupal 8.2.x and will be removed before 9.0.0. Use
* \Drupal\taxonomy\VocabularyInterface::HIERARCHY_MULTIPLE instead.
2017-10-10 07:30:23 +00:00
*
* @see https://www.drupal.org/node/2807795
2011-12-23 03:42:04 +00:00
*/
const TAXONOMY_HIERARCHY_MULTIPLE = 2;
2009-12-02 07:42:43 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2009-12-02 07:42:43 +00:00
*/
2014-06-30 03:33:08 +00:00
function taxonomy_help($route_name, RouteMatchInterface $route_match) {
2014-05-07 02:04:53 +00:00
switch ($route_name) {
case 'help.page.taxonomy':
2019-04-16 05:38:27 +00:00
$field_ui_url = \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#';
2009-12-02 07:42:43 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2019-04-16 05:38:27 +00:00
$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>';
2017-03-04 01:20:24 +00:00
$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>';
2009-12-02 07:42:43 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
2015-02-16 23:24:30 +00:00
$output .= '<dt>' . t('Managing vocabularies') . '</dt>';
2019-04-16 05:38:27 +00:00
$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>';
2015-02-16 23:24:30 +00:00
$output .= '<dt>' . t('Managing terms') . '</dt>';
2019-04-16 05:38:27 +00:00
$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>';
2015-02-16 23:24:30 +00:00
$output .= '<dt>' . t('Classifying entity content') . '</dt>';
2019-04-16 05:38:27 +00:00
$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>';
2015-02-16 23:24:30 +00:00
$output .= '<dt>' . t('Adding new terms during content creation') . '</dt>';
2017-09-14 02:47:52 +00:00
$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>';
Issue #1847596 by amateescu, amitaibu, YesCT, Berdir, David_Rothstein, xjm, rteijeiro, ParisLiakos, webchick, Wim Leers, yched, jhodgdon, Bojhan: Remove Taxonomy term reference field in favor of Entity reference
2015-03-27 21:53:41 +00:00
$output .= '<dt>' . t('Configuring displays and form displays') . '</dt>';
2019-04-16 05:38:27 +00:00
$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>';
2015-02-16 23:24:30 +00:00
$output .= '</ul>';
$output .= '</dd>';
2009-12-02 07:42:43 +00:00
$output .= '</dl>';
return $output;
2014-05-07 02:04:53 +00:00
2015-01-19 09:37:11 +00:00
case 'entity.taxonomy_vocabulary.collection':
2010-01-10 21:26:30 +00:00
$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>';
2009-12-02 07:42:43 +00:00
return $output;
}
}
Issue #2880149 by amateescu, timmillwood, Manuel Garcia, Jo Fitzgerald, fidodido06, plach, blazey, hchonov, benjifisher, lbodiguel, vpeltot, larowlan, dixon_, jojototh, Wim Leers, catch, jibran, Berdir, mikelutz, pameeela, webchick, Bojhan: Convert taxonomy terms to be revisionable
2019-03-11 19:56:03 +00:00
/**
* Implements hook_entity_type_alter().
*/
function taxonomy_entity_type_alter(array &$entity_types) {
Issue #2899923 by Manuel Garcia, vijaycs85, chr.fritsch, andypost, jibran, Anybody, amateescu, alexpott, larowlan, jedgar1mx, merauluka, longwave: UI for publishing/unpublishing taxonomy terms
2019-06-13 11:09:28 +00:00
// @todo Moderation is disabled for taxonomy terms until integration is
// enabled for them.
// @see https://www.drupal.org/project/drupal/issues/3047110
Issue #2880149 by amateescu, timmillwood, Manuel Garcia, Jo Fitzgerald, fidodido06, plach, blazey, hchonov, benjifisher, lbodiguel, vpeltot, larowlan, dixon_, jojototh, Wim Leers, catch, jibran, Berdir, mikelutz, pameeela, webchick, Bojhan: Convert taxonomy terms to be revisionable
2019-03-11 19:56:03 +00:00
$entity_types['taxonomy_term']->setHandlerClass('moderation', '');
}
2010-01-13 06:26:49 +00:00
/**
2012-09-02 04:50:06 +00:00
* Entity URI callback.
2010-01-13 06:26:49 +00:00
*/
2010-02-11 15:52:13 +00:00
function taxonomy_term_uri($term) {
2017-03-04 01:20:24 +00:00
return new Url('entity.taxonomy_term.canonical', [
2014-03-28 23:07:00 +00:00
'taxonomy_term' => $term->id(),
2017-03-04 01:20:24 +00:00
]);
2010-01-13 06:26:49 +00:00
}
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
/**
2014-10-16 12:36:06 +00:00
* Implements hook_page_attachments_alter().
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
*/
2014-10-16 12:36:06 +00:00
function taxonomy_page_attachments_alter(array &$page) {
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
$route_match = \Drupal::routeMatch();
if ($route_match->getRouteName() == 'entity.taxonomy_term.canonical' && ($term = $route_match->getParameter('taxonomy_term')) && $term instanceof TermInterface) {
foreach ($term->uriRelationships() as $rel) {
// Set the URI relationships, like canonical.
2017-03-04 01:20:24 +00:00
$page['#attached']['html_head_link'][] = [
[
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
'rel' => $rel,
2018-12-23 13:30:20 +00:00
'href' => $term->toUrl($rel)->toString(),
2017-03-04 01:20:24 +00:00
],
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
TRUE,
2017-03-04 01:20:24 +00:00
];
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
// Set the term path as the canonical URL to prevent duplicate content.
if ($rel == 'canonical') {
// Set the non-aliased canonical path as a default shortlink.
2017-03-04 01:20:24 +00:00
$page['#attached']['html_head_link'][] = [
[
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
'rel' => 'shortlink',
2018-12-23 13:30:20 +00:00
'href' => $term->toUrl($rel, ['alias' => TRUE])->toString(),
2017-03-04 01:20:24 +00:00
],
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
TRUE,
2017-03-04 01:20:24 +00:00
];
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
}
}
}
}
2007-04-06 13:27:23 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2007-04-06 13:27:23 +00:00
*/
function taxonomy_theme() {
2017-03-04 01:20:24 +00:00
return [
'taxonomy_term' => [
2010-02-10 06:28:10 +00:00
'render element' => 'elements',
2017-03-04 01:20:24 +00:00
],
];
2007-04-06 13:27:23 +00:00
}
2007-11-26 19:46:52 +00:00
/**
2018-11-15 14:42:08 +00:00
* Checks the hierarchy flag of a vocabulary.
2007-11-28 10:29:21 +00:00
*
2018-11-15 14:42:08 +00:00
* Checks the current parents of all terms in a vocabulary. If no term has
* parent terms then the vocabulary will be given a hierarchy of
2016-09-28 15:46:58 +00:00
* VocabularyInterface::HIERARCHY_DISABLED. If any term has a single parent then
* the vocabulary will be given a hierarchy of
* VocabularyInterface::HIERARCHY_SINGLE. If any term has multiple parents then
* the vocabulary will be given a hierarchy of
* VocabularyInterface::HIERARCHY_MULTIPLE.
2007-11-28 10:29:21 +00:00
*
2013-07-20 13:41:10 +00:00
* @param \Drupal\taxonomy\VocabularyInterface $vocabulary
2012-04-02 02:55:32 +00:00
* A taxonomy vocabulary entity.
2007-11-26 19:46:52 +00:00
* @param $changed_term
* An array of the term structure that was updated.
2011-07-28 19:33:59 +00:00
*
2018-11-15 14:42:08 +00:00
* @return int
2011-07-28 19:33:59 +00:00
* An integer that represents the level of the vocabulary's hierarchy.
2018-11-15 14:42:08 +00:00
*
* @deprecated in Drupal 8.7.x. Will be removed before Drupal 9.0.0. Use
* \Drupal\taxonomy\TermStorage::getVocabularyHierarchyType() instead.
2007-11-26 19:46:52 +00:00
*/
2013-07-20 13:41:10 +00:00
function taxonomy_check_vocabulary_hierarchy(VocabularyInterface $vocabulary, $changed_term) {
2018-11-15 14:42:08 +00:00
@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);
return \Drupal::entityTypeManager()->getStorage('taxonomy_term')->getVocabularyHierarchyType($vocabulary->id());
2007-11-26 19:46:52 +00:00
}
2010-02-10 06:28:10 +00:00
/**
2012-09-06 06:25:20 +00:00
* Generates an array which displays a term detail page.
2010-02-10 06:28:10 +00:00
*
2013-10-03 11:26:25 +00:00
* @param \Drupal\taxonomy\Entity\Term $term
2012-09-06 06:25:20 +00:00
* A taxonomy term object.
* @param string $view_mode
2015-06-08 22:40:25 +00:00
* View mode; e.g., 'full', 'teaser', etc.
2012-09-06 06:25:20 +00:00
* @param string $langcode
2010-10-03 01:15:34 +00:00
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
2010-02-10 06:28:10 +00:00
*
2012-09-06 06:25:20 +00:00
* @return array
Issue #2938970 by MerryHamster, surbz, msankhala, alexpott, Jo Fitzgerald, Yasiru Nilan, Manuel Garcia, ZeiP, boaloysius, xjm, wturrell, tameeshb, darrenwh, ritzz: Replace drupal_render() in @param, @return, @see, @link, etc
2018-05-03 16:58:24 +00:00
* A $page element suitable for use by
* \Drupal\Core\Render\RendererInterface::render().
2019-04-29 21:35:23 +00:00
*
* @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use
* \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->view()
* instead.
*
* @see https://www.drupal.org/node/3033656
2012-09-06 06:25:20 +00:00
*/
2012-10-14 06:40:03 +00:00
function taxonomy_term_view(Term $term, $view_mode = 'full', $langcode = NULL) {
2019-04-29 21:35:23 +00:00
@trigger_error("taxonomy_term_view() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->view() instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED);
return \Drupal::entityTypeManager()
->getViewBuilder('taxonomy_term')
->view($term, $view_mode, $langcode);
2012-09-06 06:25:20 +00:00
}
2010-02-10 06:28:10 +00:00
2014-04-24 13:06:35 +00:00
/**
2012-10-14 06:40:03 +00:00
* Constructs a drupal_render() style array from an array of loaded terms.
2012-09-06 06:25:20 +00:00
*
2012-10-14 06:40:03 +00:00
* @param array $terms
2014-10-08 15:31:02 +00:00
* An array of taxonomy terms as returned by Term::loadMultiple().
2012-09-06 06:25:20 +00:00
* @param string $view_mode
2015-06-08 22:40:25 +00:00
* View mode; e.g., 'full', 'teaser', etc.
2012-09-06 06:25:20 +00:00
* @param string $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
*
2012-10-14 06:40:03 +00:00
* @return array
Issue #2938970 by MerryHamster, surbz, msankhala, alexpott, Jo Fitzgerald, Yasiru Nilan, Manuel Garcia, ZeiP, boaloysius, xjm, wturrell, tameeshb, darrenwh, ritzz: Replace drupal_render() in @param, @return, @see, @link, etc
2018-05-03 16:58:24 +00:00
* An array in the format expected by
* \Drupal\Core\Render\RendererInterface::render().
2019-04-29 21:35:23 +00:00
*
* @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use
* \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->viewMultiple()
* instead.
*
* @see https://www.drupal.org/node/3033656
2012-09-06 06:25:20 +00:00
*/
2012-10-14 06:40:03 +00:00
function taxonomy_term_view_multiple(array $terms, $view_mode = 'full', $langcode = NULL) {
2019-04-29 21:35:23 +00:00
@trigger_error("taxonomy_term_view_multiple() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->viewMultiple() instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED);
return \Drupal::entityTypeManager()
->getViewBuilder('taxonomy_term')
->viewMultiple($terms, $view_mode, $langcode);
2010-02-10 06:28:10 +00:00
}
2013-09-29 07:19:59 +00:00
/**
* Implements hook_theme_suggestions_HOOK().
*/
function taxonomy_theme_suggestions_taxonomy_term(array $variables) {
2017-03-04 01:20:24 +00:00
$suggestions = [];
2013-09-29 07:19:59 +00:00
2014-02-04 16:02:26 +00:00
/** @var \Drupal\taxonomy\TermInterface $term */
$term = $variables['elements']['#taxonomy_term'];
2013-09-29 07:19:59 +00:00
$suggestions[] = 'taxonomy_term__' . $term->bundle();
$suggestions[] = 'taxonomy_term__' . $term->id();
return $suggestions;
}
2010-02-10 06:28:10 +00:00
/**
Issue #1898460 by pixelmord, bstoppel, steveoliver, jenlampton, johnnygamba, jastraat, Cottser, lbainbridge, myke, thedavidmeister, c4rl, joelpittet, ezeedub: Convert taxonomy module to Twig.
2013-05-24 16:52:00 +00:00
* Prepares variables for taxonomy term templates.
*
* Default template: taxonomy-term.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the taxonomy term and any
* fields attached to the term. Properties used:
2014-02-04 16:02:26 +00:00
* - #taxonomy_term: A \Drupal\taxonomy\TermInterface object.
Issue #1898460 by pixelmord, bstoppel, steveoliver, jenlampton, johnnygamba, jastraat, Cottser, lbainbridge, myke, thedavidmeister, c4rl, joelpittet, ezeedub: Convert taxonomy module to Twig.
2013-05-24 16:52:00 +00:00
* - #view_mode: The current view mode for this taxonomy term, e.g.
* 'full' or 'teaser'.
* - attributes: HTML attributes for the containing element.
2010-02-10 06:28:10 +00:00
*/
function template_preprocess_taxonomy_term(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
2014-02-04 16:02:26 +00:00
$variables['term'] = $variables['elements']['#taxonomy_term'];
/** @var \Drupal\taxonomy\TermInterface $term */
2010-02-10 06:28:10 +00:00
$term = $variables['term'];
2018-12-23 13:30:20 +00:00
$variables['url'] = $term->toUrl()->toString();
Issue #1898460 by pixelmord, bstoppel, steveoliver, jenlampton, johnnygamba, jastraat, Cottser, lbainbridge, myke, thedavidmeister, c4rl, joelpittet, ezeedub: Convert taxonomy module to Twig.
2013-05-24 16:52:00 +00:00
// We use name here because that is what appears in the UI.
2014-03-30 11:24:35 +00:00
$variables['name'] = $variables['elements']['name'];
unset($variables['elements']['name']);
Issue #1898460 by pixelmord, bstoppel, steveoliver, jenlampton, johnnygamba, jastraat, Cottser, lbainbridge, myke, thedavidmeister, c4rl, joelpittet, ezeedub: Convert taxonomy module to Twig.
2013-05-24 16:52:00 +00:00
$variables['page'] = $variables['view_mode'] == 'full' && taxonomy_term_is_page($term);
2010-02-10 06:28:10 +00:00
// Helpful $content variable for templates.
2017-03-04 01:20:24 +00:00
$variables['content'] = [];
2014-03-31 17:37:55 +00:00
foreach (Element::children($variables['elements']) as $key) {
2010-02-10 06:28:10 +00:00
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
2011-04-12 20:54:16 +00:00
* Returns whether the current page is the page of the passed-in term.
2010-02-10 06:28:10 +00:00
*
2013-10-03 11:26:25 +00:00
* @param \Drupal\taxonomy\Entity\Term $term
2012-04-02 02:55:32 +00:00
* A taxonomy term entity.
2010-02-10 06:28:10 +00:00
*/
2012-05-17 01:53:35 +00:00
function taxonomy_term_is_page(Term $term) {
Issue #1857256 by dawehner, xjm, tim.plunkett, jibran, ParisLiakos, hussainweb, pcambra, ekes, InternetDevels, rhabbachi, rdrh555, tstoeckler, oadaeh, Gábor Hojtsy, vijaycs85: Fixed Convert the taxonomy listing and feed at /taxonomy/term/%term to Views.
2014-09-15 09:17:06 +00:00
if (\Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_term.canonical' && $page_term_id = \Drupal::routeMatch()->getRawParameter('taxonomy_term')) {
return $page_term_id == $term->id();
2013-09-25 00:17:27 +00:00
}
return FALSE;
2010-02-10 06:28:10 +00:00
}
2009-04-02 20:39:45 +00:00
/**
2011-10-29 11:36:18 +00:00
* Clear all static cache variables for terms.
2009-04-02 20:39:45 +00:00
*/
function taxonomy_terms_static_reset() {
2019-05-24 06:44:36 +00:00
\Drupal::entityTypeManager()->getStorage('taxonomy_term')->resetCache();
2009-04-02 20:39:45 +00:00
}
2011-09-28 04:58:42 +00:00
/**
* Clear all static cache variables for vocabularies.
2011-09-30 21:38:03 +00:00
*
2011-09-28 04:58:42 +00:00
* @param $ids
2014-08-22 11:59:25 +00:00
* An array of ids to reset in the entity cache.
2011-09-28 04:58:42 +00:00
*/
2012-04-02 02:55:32 +00:00
function taxonomy_vocabulary_static_reset(array $ids = NULL) {
2019-05-24 06:44:36 +00:00
\Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary')->resetCache($ids);
2011-09-28 04:58:42 +00:00
}
2009-06-12 13:59:56 +00:00
/**
* Get names for all taxonomy vocabularies.
*
2012-12-19 17:06:30 +00:00
* @return array
* A list of existing vocabulary IDs.
2009-06-12 13:59:56 +00:00
*/
function taxonomy_vocabulary_get_names() {
2011-09-28 04:58:42 +00:00
$names = &drupal_static(__FUNCTION__);
if (!isset($names)) {
2017-03-04 01:20:24 +00:00
$names = [];
2014-03-01 19:02:16 +00:00
$config_names = \Drupal::configFactory()->listAll('taxonomy.vocabulary.');
2012-12-19 17:06:30 +00:00
foreach ($config_names as $config_name) {
$id = substr($config_name, strlen('taxonomy.vocabulary.'));
$names[$id] = $id;
}
2011-09-28 04:58:42 +00:00
}
2011-09-30 21:38:03 +00:00
2009-06-12 13:59:56 +00:00
return $names;
}
2002-12-12 18:54:17 +00:00
/**
2004-06-18 15:04:37 +00:00
* Try to map a string to an existing term, as for glossary use.
2002-12-12 18:54:17 +00:00
*
2004-06-18 15:04:37 +00:00
* Provides a case-insensitive and trimmed mapping, to maximize the
* likelihood of a successful match.
*
2011-09-30 21:38:03 +00:00
* @param $name
2004-06-18 15:04:37 +00:00
* Name of the term to search for.
2012-01-08 14:02:21 +00:00
* @param $vocabulary
* (optional) Vocabulary machine name to limit the search. Defaults to NULL.
2004-06-18 15:04:37 +00:00
*
* @return
* An array of matching term objects.
2002-12-12 18:54:17 +00:00
*/
2012-01-18 03:37:45 +00:00
function taxonomy_term_load_multiple_by_name($name, $vocabulary = NULL) {
2017-03-04 01:20:24 +00:00
$values = ['name' => trim($name)];
2012-01-08 14:02:21 +00:00
if (isset($vocabulary)) {
$vocabularies = taxonomy_vocabulary_get_names();
2016-05-05 10:07:32 +00:00
if (isset($vocabularies[$vocabulary])) {
2012-12-19 17:06:30 +00:00
$values['vid'] = $vocabulary;
2012-01-08 14:02:21 +00:00
}
else {
// Return an empty array when filtering by a non-existing vocabulary.
2017-03-04 01:20:24 +00:00
return [];
2012-01-08 14:02:21 +00:00
}
}
2019-04-30 07:41:05 +00:00
return \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties($values);
2002-12-12 18:54:17 +00:00
}
2008-12-05 22:18:46 +00:00
/**
* Load multiple taxonomy terms based on certain conditions.
*
* This function should be used whenever you need to load more than one term
* from the database. Terms are loaded into memory and will not require
* database access if loaded again during the same page request.
*
2012-08-21 15:38:04 +00:00
* @param array $tids
* (optional) An array of entity IDs. If omitted, all entities are loaded.
2008-12-05 22:18:46 +00:00
*
2012-04-26 04:07:55 +00:00
* @return array
2012-04-02 02:55:32 +00:00
* An array of taxonomy term entities, indexed by tid. When no results are
* found, an empty array is returned.
2019-02-04 12:29:50 +00:00
*
* @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
2008-12-05 22:18:46 +00:00
*/
2012-08-21 15:38:04 +00:00
function taxonomy_term_load_multiple(array $tids = NULL) {
2019-02-04 12:29:50 +00:00
@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);
2014-05-20 09:29:40 +00:00
return Term::loadMultiple($tids);
2009-08-25 21:53:48 +00:00
}
2008-12-05 22:18:46 +00:00
2009-08-25 21:53:48 +00:00
/**
2012-04-26 04:07:55 +00:00
* Loads multiple taxonomy vocabularies based on certain conditions.
2009-08-25 21:53:48 +00:00
*
* This function should be used whenever you need to load more than one
* vocabulary from the database. Terms are loaded into memory and will not
* require database access if loaded again during the same page request.
*
2012-08-21 15:38:04 +00:00
* @param array $vids
* (optional) An array of entity IDs. If omitted, all entities are loaded.
2009-08-25 21:53:48 +00:00
*
2012-04-26 04:07:55 +00:00
* @return array
2016-06-02 14:20:55 +00:00
* An array of vocabulary objects, indexed by vid.
2019-02-04 12:29:50 +00:00
*
* @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
2009-08-25 21:53:48 +00:00
*/
2012-08-21 15:38:04 +00:00
function taxonomy_vocabulary_load_multiple(array $vids = NULL) {
2019-02-04 12:29:50 +00:00
@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);
2014-05-20 09:29:40 +00:00
return Vocabulary::loadMultiple($vids);
2009-08-25 21:53:48 +00:00
}
2008-12-05 22:18:46 +00:00
2010-03-25 11:53:25 +00:00
/**
2012-12-19 17:06:30 +00:00
* Return the taxonomy vocabulary entity matching a vocabulary ID.
2010-03-25 11:53:25 +00:00
*
2012-12-19 17:06:30 +00:00
* @param int $vid
* The vocabulary's ID.
2010-03-25 11:53:25 +00:00
*
2013-08-18 21:16:19 +00:00
* @return \Drupal\taxonomy\Entity\Vocabulary|null
2013-07-01 00:09:20 +00:00
* The taxonomy vocabulary entity, if exists, NULL otherwise. Results are
2012-04-02 02:55:32 +00:00
* statically cached.
Issue #2723593 by alexpott, Jo Fitzgerald, daffie, JacobSanford, rajeshwari10, chishah92, Saviktor, marvin_B8, valthebald, Mile23, Berdir: Properly deprecate entity_load() and friends
2019-02-05 12:08:32 +00:00
*
* @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
2010-03-25 11:53:25 +00:00
*/
2012-12-19 17:06:30 +00:00
function taxonomy_vocabulary_load($vid) {
Issue #2723593 by alexpott, Jo Fitzgerald, daffie, JacobSanford, rajeshwari10, chishah92, Saviktor, marvin_B8, valthebald, Mile23, Berdir: Properly deprecate entity_load() and friends
2019-02-05 12:08:32 +00:00
@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);
2014-05-20 09:29:40 +00:00
return Vocabulary::load($vid);
2010-03-25 11:53:25 +00:00
}
2004-06-18 15:04:37 +00:00
/**
2012-04-02 02:55:32 +00:00
* Return the taxonomy term entity matching a term ID.
2006-10-18 11:15:51 +00:00
*
* @param $tid
* A term's ID
*
2013-08-18 21:16:19 +00:00
* @return \Drupal\taxonomy\Entity\Term|null
2013-07-01 00:09:20 +00:00
* A taxonomy term entity, or NULL if the term was not found. Results are
2012-07-19 23:11:59 +00:00
* statically cached.
Issue #2723593 by alexpott, Jo Fitzgerald, daffie, JacobSanford, rajeshwari10, chishah92, Saviktor, marvin_B8, valthebald, Mile23, Berdir: Properly deprecate entity_load() and friends
2019-02-05 12:08:32 +00:00
*
* @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
2004-06-18 15:04:37 +00:00
*/
2009-04-02 20:39:45 +00:00
function taxonomy_term_load($tid) {
Issue #2723593 by alexpott, Jo Fitzgerald, daffie, JacobSanford, rajeshwari10, chishah92, Saviktor, marvin_B8, valthebald, Mile23, Berdir: Properly deprecate entity_load() and friends
2019-02-05 12:08:32 +00:00
@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);
2008-09-19 20:25:03 +00:00
if (!is_numeric($tid)) {
2013-07-01 00:09:20 +00:00
return NULL;
2008-09-19 20:25:03 +00:00
}
2014-05-20 09:29:40 +00:00
return Term::load($tid);
2008-11-02 14:42:45 +00:00
}
2007-04-05 03:08:48 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implodes a list of tags of a certain vocabulary into a string.
2010-06-27 16:49:58 +00:00
*
2014-03-17 18:39:20 +00:00
* @see \Drupal\Component\Utility\Tags::explode()
2007-04-05 03:08:48 +00:00
*/
function taxonomy_implode_tags($tags, $vid = NULL) {
2017-03-04 01:20:24 +00:00
$typed_tags = [];
2007-04-05 03:08:48 +00:00
foreach ($tags as $tag) {
// Extract terms belonging to the vocabulary in question.
2012-12-19 17:06:30 +00:00
if (!isset($vid) || $tag->bundle() == $vid) {
2010-04-30 12:52:10 +00:00
// Make sure we have a completed loaded taxonomy term.
2012-08-19 10:33:53 +00:00
if ($tag instanceof EntityInterface && $label = $tag->label()) {
2010-04-30 12:52:10 +00:00
// Commas and quotes in tag names are special cases, so encode 'em.
2014-02-13 11:58:51 +00:00
$typed_tags[] = Tags::encode($label);
2007-04-05 03:08:48 +00:00
}
}
}
2007-04-06 13:27:23 +00:00
return implode(', ', $typed_tags);
2007-04-05 03:08:48 +00:00
}
2007-06-29 18:06:51 +00:00
2009-07-08 07:18:08 +00:00
/**
* Title callback for term pages.
*
2013-10-03 11:26:25 +00:00
* @param \Drupal\taxonomy\Entity\Term $term
2012-04-02 02:55:32 +00:00
* A taxonomy term entity.
2011-01-18 18:48:59 +00:00
*
2009-07-08 07:18:08 +00:00
* @return
* The term name to be used as the page title.
*/
2012-05-17 01:53:35 +00:00
function taxonomy_term_title(Term $term) {
2014-03-17 10:11:38 +00:00
return $term->getName();
2009-07-08 07:18:08 +00:00
}
2009-08-18 06:01:07 +00:00
2009-10-08 07:58:47 +00:00
/**
2011-01-03 18:03:54 +00:00
* @defgroup taxonomy_index Taxonomy indexing
2011-01-02 17:26:40 +00:00
* @{
* Functions to maintain taxonomy indexing.
2009-10-08 07:58:47 +00:00
*
* Taxonomy uses default field storage to store canonical relationships
* between terms and fieldable entities. However its most common use case
* requires listing all content associated with a term or group of terms
* sorted by creation date. To avoid slow queries due to joining across
* multiple node and field tables with various conditions and order by criteria,
* we maintain a denormalized table with all relationships between terms,
2015-01-20 12:50:25 +00:00
* published nodes and common sort criteria such as status, sticky and created.
* When using other field storage engines or alternative methods of
* denormalizing this data you should set the
* taxonomy.settings:maintain_index_table to '0' to avoid unnecessary writes in
* SQL.
2009-10-08 07:58:47 +00:00
*/
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_insert() for node entities.
2009-10-08 07:58:47 +00:00
*/
2013-03-10 19:05:24 +00:00
function taxonomy_node_insert(EntityInterface $node) {
2011-12-08 03:52:37 +00:00
// Add taxonomy index entries for the node.
taxonomy_build_node_index($node);
2009-10-08 07:58:47 +00:00
}
/**
2011-12-08 03:52:37 +00:00
* Builds and inserts taxonomy index entries for a given node.
*
* The index lists all terms that are related to a given node entity, and is
* therefore maintained at the entity level.
*
2013-08-18 21:16:19 +00:00
* @param \Drupal\node\Entity\Node $node
2012-04-26 16:44:37 +00:00
* The node entity.
2009-10-08 07:58:47 +00:00
*/
2011-12-08 03:52:37 +00:00
function taxonomy_build_node_index($node) {
// We maintain a denormalized table of term/node relationships, containing
// only data for current, published nodes.
2019-05-24 06:44:36 +00:00
if (!\Drupal::config('taxonomy.settings')->get('maintain_index_table') || !(\Drupal::entityTypeManager()->getStorage('node') instanceof SqlContentEntityStorage)) {
2013-09-01 06:20:08 +00:00
return;
2011-12-08 03:52:37 +00:00
}
2013-09-01 06:20:08 +00:00
$status = $node->isPublished();
$sticky = (int) $node->isSticky();
2011-12-08 03:52:37 +00:00
// We only maintain the taxonomy index for published nodes.
2012-09-06 20:32:19 +00:00
if ($status && $node->isDefaultRevision()) {
2011-12-08 03:52:37 +00:00
// Collect a unique list of all the term IDs from all node fields.
2017-03-04 01:20:24 +00:00
$tid_all = [];
2015-10-06 11:02:22 +00:00
$entity_reference_class = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';
2014-04-11 16:01:24 +00:00
foreach ($node->getFieldDefinitions() as $field) {
2013-12-09 23:19:58 +00:00
$field_name = $field->getName();
2015-10-06 11:02:22 +00:00
$class = $field->getItemDefinition()->getClass();
$is_entity_reference_class = ($class === $entity_reference_class) || is_subclass_of($class, $entity_reference_class);
if ($is_entity_reference_class && $field->getSetting('target_type') == 'taxonomy_term') {
2013-08-27 10:36:16 +00:00
foreach ($node->getTranslationLanguages() as $language) {
2014-10-13 09:10:32 +00:00
foreach ($node->getTranslation($language->getId())->$field_name as $item) {
2013-08-27 10:36:16 +00:00
if (!$item->isEmpty()) {
$tid_all[$item->target_id] = $item->target_id;
2011-12-08 03:52:37 +00:00
}
}
}
}
}
// Insert index entries for all the node's terms.
if (!empty($tid_all)) {
Issue #2848952 by voleger, andypost, mondrake, jeetendrakumar, pk188, hgunicamp, piggito, JayKandari, daffie, shashikant_chauhan, xjm, cilefen, chiranjeeb, Pavan B S: Replace all calls to db_merge(), which is deprecated
2018-08-24 00:21:59 +00:00
$connection = \Drupal::database();
2011-12-08 03:52:37 +00:00
foreach ($tid_all as $tid) {
Issue #2848952 by voleger, andypost, mondrake, jeetendrakumar, pk188, hgunicamp, piggito, JayKandari, daffie, shashikant_chauhan, xjm, cilefen, chiranjeeb, Pavan B S: Replace all calls to db_merge(), which is deprecated
2018-08-24 00:21:59 +00:00
$connection->merge('taxonomy_index')
2017-03-04 01:20:24 +00:00
->key(['nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished()])
->fields(['sticky' => $sticky, 'created' => $node->getCreatedTime()])
2014-04-15 10:46:45 +00:00
->execute();
2009-10-08 07:58:47 +00:00
}
}
}
}
2011-12-08 03:52:37 +00:00
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_update() for node entities.
2011-12-08 03:52:37 +00:00
*/
2013-03-10 19:05:24 +00:00
function taxonomy_node_update(EntityInterface $node) {
2016-06-23 21:31:25 +00:00
// If we're not dealing with the default revision of the node, do not make any
// change to the taxonomy index.
if (!$node->isDefaultRevision()) {
return;
}
2011-12-08 03:52:37 +00:00
taxonomy_delete_node_index($node);
taxonomy_build_node_index($node);
}
2009-10-08 07:58:47 +00:00
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_predelete() for node entities.
2009-10-08 07:58:47 +00:00
*/
2013-03-10 19:05:24 +00:00
function taxonomy_node_predelete(EntityInterface $node) {
2011-12-08 03:52:37 +00:00
// Clean up the {taxonomy_index} table when nodes are deleted.
taxonomy_delete_node_index($node);
}
/**
* Deletes taxonomy index entries for a given node.
*
2013-03-10 19:05:24 +00:00
* @param \Drupal\Core\Entity\EntityInterface $node
2012-04-26 16:44:37 +00:00
* The node entity.
2011-12-08 03:52:37 +00:00
*/
2013-03-10 19:05:24 +00:00
function taxonomy_delete_node_index(EntityInterface $node) {
2013-09-16 03:58:06 +00:00
if (\Drupal::config('taxonomy.settings')->get('maintain_index_table')) {
2018-08-31 01:15:01 +00:00
\Drupal::database()->delete('taxonomy_index')->condition('nid', $node->id())->execute();
2009-10-08 07:58:47 +00:00
}
}
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_delete() for taxonomy_term entities.
2009-10-08 07:58:47 +00:00
*/
2012-05-17 01:53:35 +00:00
function taxonomy_taxonomy_term_delete(Term $term) {
2013-09-16 03:58:06 +00:00
if (\Drupal::config('taxonomy.settings')->get('maintain_index_table')) {
2009-10-08 07:58:47 +00:00
// Clean up the {taxonomy_index} table when terms are deleted.
2018-08-31 01:15:01 +00:00
\Drupal::database()->delete('taxonomy_index')->condition('tid', $term->id())->execute();
2009-10-08 07:58:47 +00:00
}
}
/**
2012-05-17 12:58:49 +00:00
* @} End of "defgroup taxonomy_index".
2009-10-08 07:58:47 +00:00
*/