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;
2014-11-03 08:13:20 +00:00
use Drupal\Component\Utility\Unicode;
2014-09-08 12:12:45 +00:00
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
2013-09-01 06:20:08 +00:00
use Drupal\Core\Entity\EntityInterface;
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.
*/
const TAXONOMY_HIERARCHY_DISABLED = 0;
/**
* Denotes that one or more terms in the vocabulary has a single parent.
*/
const TAXONOMY_HIERARCHY_SINGLE = 1;
/**
* Denotes that one or more terms in the vocabulary have multiple parents.
*/
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':
2015-02-16 23:24:30 +00:00
$field_ui_url = \Drupal::moduleHandler()->moduleExists('field_ui') ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#';
2009-12-02 07:42:43 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
Issue #2560783 by stefan.r, joelpittet, lauriii, Cottser, Sutharsan, kgoel, justAChris, Gábor Hojtsy, dawehner, jhodgdon, effulgentsia, xjm, andypost, googletorp: Replace !placeholder with :placeholder for URLs in hook_help() implementations
2015-09-21 11:38:19 +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.', array(':permissions' => \Drupal::url('user.admin_permissions', array(), array('fragment' => 'module-taxonomy')))) . '</p>';
$output .= '<p>' . t('For more information, see the <a href=":taxonomy">online documentation for the Taxonomy module</a>.', array(':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>';
2016-04-28 08:42:12 +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>.', array(':taxonomy_admin' => \Drupal::url('entity.taxonomy_vocabulary.collection'), ':field_ui' => $field_ui_url)) . '</dd>';
2015-02-16 23:24:30 +00:00
$output .= '<dt>' . t('Managing terms') . '</dt>';
2016-04-28 08:42:12 +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.', array(':taxonomy_admin' => \Drupal::url('entity.taxonomy_vocabulary.collection'))) . ' </dd>';
2015-02-16 23:24:30 +00:00
$output .= '<dt>' . t('Classifying entity content') . '</dt>';
2016-04-28 08:42:12 +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.', array(':field_ui' => $field_ui_url, ':field' => \Drupal::url('help.page', array('name' => 'field')), ':entity_reference' => \Drupal::url('help.page', array('name' => 'entity_reference')))) . '</dd>';
2015-02-16 23:24:30 +00:00
$output .= '<dt>' . t('Adding new terms during content creation') . '</dt>';
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 .= '<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>';
$output .= '<dt>' . t('Configuring displays and form displays') . '</dt>';
Issue #2560783 by stefan.r, joelpittet, lauriii, Cottser, Sutharsan, kgoel, justAChris, Gábor Hojtsy, dawehner, jhodgdon, effulgentsia, xjm, andypost, googletorp: Replace !placeholder with :placeholder for URLs in hook_help() implementations
2015-09-21 11:38:19 +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.', array(':entity_reference' => \Drupal::url('help.page', array('name' => 'entity_reference')))) . '</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;
2014-05-07 02:04:53 +00:00
2014-08-22 00:54:34 +00:00
case 'entity.taxonomy_vocabulary.overview_form':
2014-06-30 03:33:08 +00:00
$vocabulary = $route_match->getParameter('taxonomy_vocabulary');
2015-01-05 13:18:58 +00:00
switch ($vocabulary->getHierarchy()) {
2011-12-23 03:42:04 +00:00
case TAXONOMY_HIERARCHY_DISABLED:
2015-01-05 13:18:58 +00:00
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.', array('%capital_name' => Unicode::ucfirst($vocabulary->label()), '%name' => $vocabulary->label())) . '</p>';
2011-12-23 03:42:04 +00:00
case TAXONOMY_HIERARCHY_SINGLE:
2015-01-05 13:18:58 +00:00
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.', array('%capital_name' => Unicode::ucfirst($vocabulary->label()), '%name' => $vocabulary->label())) . '</p>';
2011-12-23 03:42:04 +00:00
case TAXONOMY_HIERARCHY_MULTIPLE:
2015-01-05 13:18:58 +00:00
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.', array('%capital_name' => Unicode::ucfirst($vocabulary->label()))) . '</p>';
2009-12-02 07:42:43 +00:00
}
}
}
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) {
2014-08-22 00:54:34 +00:00
return new Url('entity.taxonomy_term.canonical', array(
2014-03-28 23:07:00 +00:00
'taxonomy_term' => $term->id(),
));
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.
2014-10-10 11:48:22 +00:00
$page['#attached']['html_head_link'][] = array(
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
array(
'rel' => $rel,
'href' => $term->url($rel),
),
TRUE,
);
// 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.
2014-10-10 11:48:22 +00:00
$page['#attached']['html_head_link'][] = array(
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
array(
'rel' => 'shortlink',
'href' => $term->url($rel, array('alias' => TRUE)),
),
TRUE,
);
}
}
}
}
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() {
return array(
2010-02-10 06:28:10 +00:00
'taxonomy_term' => array(
'render element' => 'elements',
),
2007-04-06 13:27:23 +00:00
);
}
2007-11-26 19:46:52 +00:00
/**
2011-07-28 19:33:59 +00:00
* Checks and updates the hierarchy flag of a vocabulary.
2007-11-28 10:29:21 +00:00
*
2007-11-26 19:46:52 +00:00
* Checks the current parents of all terms in a vocabulary and updates the
2011-07-28 19:33:59 +00:00
* vocabulary's hierarchy setting to the lowest possible level. If no term
2011-12-23 03:42:04 +00:00
* has parent terms then the vocabulary will be given a hierarchy of
* TAXONOMY_HIERARCHY_DISABLED. If any term has a single parent then the
* vocabulary will be given a hierarchy of TAXONOMY_HIERARCHY_SINGLE. If any
* term has multiple parents then the vocabulary will be given a hierarchy of
* TAXONOMY_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
*
* @return
* An integer that represents the level of the vocabulary's hierarchy.
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) {
2015-06-14 23:34:42 +00:00
$tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vocabulary->id());
2011-12-23 03:42:04 +00:00
$hierarchy = TAXONOMY_HIERARCHY_DISABLED;
2007-11-26 19:46:52 +00:00
foreach ($tree as $term) {
2008-12-30 16:43:20 +00:00
// Update the changed term with the new parent value before comparison.
2007-11-26 19:46:52 +00:00
if ($term->tid == $changed_term['tid']) {
2010-05-06 05:59:31 +00:00
$term = (object) $changed_term;
2007-11-26 19:46:52 +00:00
$term->parents = $term->parent;
}
// Check this term's parent count.
if (count($term->parents) > 1) {
2011-12-23 03:42:04 +00:00
$hierarchy = TAXONOMY_HIERARCHY_MULTIPLE;
2007-11-26 19:46:52 +00:00
break;
}
2012-04-17 03:05:39 +00:00
elseif (count($term->parents) == 1 && !isset($term->parents[0])) {
2011-12-23 03:42:04 +00:00
$hierarchy = TAXONOMY_HIERARCHY_SINGLE;
2007-11-26 19:46:52 +00:00
}
}
2015-01-05 13:18:58 +00:00
if ($hierarchy != $vocabulary->getHierarchy()) {
$vocabulary->setHierarchy($hierarchy);
2013-05-04 16:10:20 +00:00
$vocabulary->save();
2007-11-26 19:46:52 +00:00
}
return $hierarchy;
}
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
2014-02-13 00:24:37 +00:00
* A $page element suitable for use by drupal_render().
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) {
return entity_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
* An array in the format expected by drupal_render().
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) {
return entity_view_multiple($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) {
$suggestions = array();
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'];
2014-01-31 15:08:43 +00:00
$variables['url'] = $term->url();
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.
2011-05-08 19:37:54 +00:00
$variables['content'] = array();
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() {
2014-03-27 11:54:40 +00:00
\Drupal::entityManager()->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) {
2014-03-27 11:54:40 +00:00
\Drupal::entityManager()->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)) {
2012-12-19 17:06:30 +00:00
$names = array();
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) {
2012-08-21 15:38:04 +00:00
$values = array('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.
return array();
}
}
2012-08-21 15:38:04 +00:00
return entity_load_multiple_by_properties('taxonomy_term', $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-04-26 04:07:55 +00:00
* @see entity_load_multiple()
2013-10-03 11:26:25 +00:00
* @see \Drupal\Core\Entity\Query\EntityQueryInterface
2009-08-25 21:53:48 +00:00
*
2014-05-20 09:29:40 +00:00
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use \Drupal\taxonomy\Entity\Term::loadMultiple().
2013-07-29 09:34:33 +00:00
*
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.
2008-12-05 22:18:46 +00:00
*/
2012-08-21 15:38:04 +00:00
function taxonomy_term_load_multiple(array $tids = NULL) {
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-04-26 04:07:55 +00:00
* @see entity_load_multiple()
2009-08-25 21:53:48 +00:00
*
2014-05-20 09:29:40 +00:00
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use \Drupal\taxonomy\Entity\Vocabulary::loadMultiple().
2013-07-29 09:34:33 +00:00
*
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.
2009-08-25 21:53:48 +00:00
*/
2012-08-21 15:38:04 +00:00
function taxonomy_vocabulary_load_multiple(array $vids = NULL) {
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
*
2014-05-20 09:29:40 +00:00
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use \Drupal\taxonomy\Entity\Vocabulary::load().
2013-07-29 09:34:33 +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.
2010-03-25 11:53:25 +00:00
*/
2012-12-19 17:06:30 +00:00
function taxonomy_vocabulary_load($vid) {
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
*
2014-05-20 09:29:40 +00:00
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use \Drupal\taxonomy\Entity\Term::load().
2013-07-29 09:34:33 +00:00
*
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.
2004-06-18 15:04:37 +00:00
*/
2009-04-02 20:39:45 +00:00
function taxonomy_term_load($tid) {
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) {
$typed_tags = array();
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.
2014-09-08 12:12:45 +00:00
if (!\Drupal::config('taxonomy.settings')->get('maintain_index_table') || !(\Drupal::entityManager()->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.
$tid_all = array();
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)) {
foreach ($tid_all as $tid) {
2014-04-15 10:46:45 +00:00
db_merge('taxonomy_index')
2014-11-19 11:40:15 +00:00
->key(array('nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished()))
2014-04-15 10:46:45 +00:00
->fields(array('sticky' => $sticky, 'created' => $node->getCreatedTime()))
->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')) {
2013-07-20 12:21:43 +00:00
db_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.
2013-04-26 16:10:49 +00:00
db_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
*/