diff --git a/core/modules/taxonomy/src/Entity/Term.php b/core/modules/taxonomy/src/Entity/Term.php index 4cb6ce8..1855dae 100644 --- a/core/modules/taxonomy/src/Entity/Term.php +++ b/core/modules/taxonomy/src/Entity/Term.php @@ -224,6 +224,13 @@ public function getName() { /** * {@inheritdoc} */ + public function getTranslatedName() { + return \Drupal::entityManager()->getTranslationFromContext($this)->label(); + } + + /** + * {@inheritdoc} + */ public function setName($name) { $this->set('name', $name); return $this; @@ -254,7 +261,7 @@ public function getVocabularyId() { /** * {@inheritdoc} */ - public function getDepth() { + public function getTreeDepth() { return $this->depth; } diff --git a/core/modules/taxonomy/src/Plugin/views/argument/IndexTid.php b/core/modules/taxonomy/src/Plugin/views/argument/IndexTid.php index 3c59804..bccee08 100644 --- a/core/modules/taxonomy/src/Plugin/views/argument/IndexTid.php +++ b/core/modules/taxonomy/src/Plugin/views/argument/IndexTid.php @@ -24,7 +24,7 @@ public function titleQuery() { $titles = array(); $terms = Term::loadMultiple($this->value); foreach ($terms as $term) { - $titles[] = String::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label()); + $titles[] = String::checkPlain($term->getTranslatedName()); } return $titles; } diff --git a/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php b/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php index 7e79ed3..67fe7bf 100644 --- a/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php +++ b/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php @@ -106,17 +106,19 @@ public function preRender(&$values) { if (empty($this->options['limit'])) { $vocabs = array(); } - $result = \Drupal::entityManager()->getStorage('taxonomy_term')->nodeGetTerms($nids, $vocabs); - - foreach ($result as $term_record) { - $this->items[$term_record->node_nid][$term_record->tid]['name'] = String::checkPlain($term_record->name); - $this->items[$term_record->node_nid][$term_record->tid]['tid'] = $term_record->tid; - $this->items[$term_record->node_nid][$term_record->tid]['vocabulary_vid'] = $term_record->vid; - $this->items[$term_record->node_nid][$term_record->tid]['vocabulary'] = String::checkPlain($vocabularies[$term_record->vid]->label()); - - if (!empty($this->options['link_to_taxonomy'])) { - $this->items[$term_record->node_nid][$term_record->tid]['make_link'] = TRUE; - $this->items[$term_record->node_nid][$term_record->tid]['path'] = 'taxonomy/term/' . $term_record->tid; + $result = \Drupal::entityManager()->getStorage('taxonomy_term')->getNodeTerms($nids, $vocabs); + + foreach ($result as $node_nid => $data) { + foreach ($data as $tid => $term) { + $this->items[$node_nid][$tid]['name'] = $term->getTranslatedName(); + $this->items[$node_nid][$tid]['tid'] = $tid; + $this->items[$node_nid][$tid]['vocabulary_vid'] = $term->getVocabularyId(); + $this->items[$node_nid][$tid]['vocabulary'] = String::checkPlain($vocabularies[$term->getVocabularyId()]->label()); + + if (!empty($this->options['link_to_taxonomy'])) { + $this->items[$node_nid][$tid]['make_link'] = TRUE; + $this->items[$node_nid][$tid]['path'] = 'taxonomy/term/' . $tid; + } } } } diff --git a/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php index 7946fce..84dd855 100644 --- a/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php @@ -114,7 +114,7 @@ protected function valueForm(&$form, &$form_state) { if ($default) { $default .= ', '; } - $default .= String::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label()); + $default .= String::checkPlain($term->getTranslatedName()); } } @@ -135,9 +135,9 @@ protected function valueForm(&$form, &$form_state) { $options = array(); if ($tree) { - foreach ($tree as $term_record) { + foreach ($tree as $term) { $choice = new \stdClass(); - $choice->option = array($term_record->id() => str_repeat('-', $term_record->getDepth()) . String::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term_record)->label())); + $choice->option = array($term->id() => str_repeat('-', $term->getTreeDepth()) . String::checkPlain($term->getTranslatedName())); $options[] = $choice; } } @@ -154,7 +154,7 @@ protected function valueForm(&$form, &$form_state) { } $terms = Term::loadMultiple($query->execute()); foreach ($terms as $term) { - $options[$term->id()] = String::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label()); + $options[$term->id()] = String::checkPlain($term->getTranslatedTerm()); } } @@ -314,7 +314,7 @@ function validate_term_strings(&$form, $values) { ->addTag('term_access'); $terms = Term::loadMultiple($query->execute()); foreach ($terms as $term) { - unset($missing[strtolower(\Drupal::entityManager()->getTranslationFromContext($term)->label())]); + unset($missing[strtolower($term->getTranslatedTerm())]); $tids[] = $term->id(); } @@ -352,7 +352,7 @@ public function adminSummary() { $this->value = array_filter($this->value); $terms = Term::loadMultiple($this->value); foreach ($terms as $term) { - $this->value_options[$term->id()] = String::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label()); + $this->value_options[$term->id()] = String::checkPlain($term->getTranslatedName()); } } return parent::adminSummary(); diff --git a/core/modules/taxonomy/src/TermInterface.php b/core/modules/taxonomy/src/TermInterface.php index c6a550a..faf2081 100644 --- a/core/modules/taxonomy/src/TermInterface.php +++ b/core/modules/taxonomy/src/TermInterface.php @@ -60,6 +60,14 @@ public function setFormat($format); public function getName(); /** + * Gets the translated name of the term. + * + * @return string + * The translated name of the term. + */ + public function getTranslatedName(); + + /** * Sets the name of the term. * * @param int $name @@ -101,6 +109,6 @@ public function getVocabularyId(); * @return int * The depth of the term in the hierarchy; 0 is root level. */ - public function getDepth(); + public function getTreeDepth(); } diff --git a/core/modules/taxonomy/src/TermStorage.php b/core/modules/taxonomy/src/TermStorage.php index 3b799b4..8156213 100644 --- a/core/modules/taxonomy/src/TermStorage.php +++ b/core/modules/taxonomy/src/TermStorage.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Entity\ContentEntityDatabaseStorage; +use Drupal\taxonomy\Entity\Term; /** * Defines a Controller class for taxonomy terms. @@ -257,11 +258,11 @@ public function getSchema() { /** * {@inheritdoc} */ - public function nodesGetTerms($nids, $vocabs = array()) { + public function getNodeTerms($nids, $vocabs = array(), $langcode = NULL) { $query = db_select('taxonomy_term_field_data', 'td'); $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid'); - $query->fields('td', 'tid'); - $query->addField('tn', 'nid'); + $query->fields('td', array('tid')); + $query->addField('tn', 'nid', 'node_nid'); $query->orderby('td.weight'); $query->orderby('td.name'); $query->condition('tn.nid', $nids); @@ -269,8 +270,23 @@ public function nodesGetTerms($nids, $vocabs = array()) { if (!empty($vocabs)) { $query->condition('td.vid', $vocabs); } + if (!empty($langcode)) { + $query->condition('td.langcode', $langcode); + } + + $results = array(); foreach ($query->execute() as $term_record) { - $result[$term_record->node_nid][$term_record->tid] = $this->load($term_record->tid); + $results[$term_record->node_nid][] = $term_record->tid; + $all_tids[] = $term_record->tid; + } + + $all_terms = Term::loadMultiple($all_tids); + $terms = array(); + foreach ($results as $nid => $tids) { + foreach ($tids as $tid) { + $terms[$nid][$tid] = $all_terms[$term_record->tid]; + } } + return $terms; } } diff --git a/core/modules/taxonomy/src/TermStorageInterface.php b/core/modules/taxonomy/src/TermStorageInterface.php index 8511656..09a860b 100644 --- a/core/modules/taxonomy/src/TermStorageInterface.php +++ b/core/modules/taxonomy/src/TermStorageInterface.php @@ -91,10 +91,12 @@ public function resetWeights($vid); * @param array $nids * Node IDs to retrieve terms for. * @param array $vocabs - * An optional vocabularies array to restrict the term search. + * (optional) A vocabularies array to restrict the term search. + * @param string $langcode + * (optional) A language code to restrict the term search. * * @return array * An array of nids and the term entities they were tagged with. */ - public function nodesGetTerms($nids, $vocabs = array()); + public function getNodeTerms($nids, $vocabs = array(), $langcode = NULL); } diff --git a/core/modules/taxonomy/taxonomy.views.inc b/core/modules/taxonomy/taxonomy.views.inc index 3b27a12..0e17245 100644 --- a/core/modules/taxonomy/taxonomy.views.inc +++ b/core/modules/taxonomy/taxonomy.views.inc @@ -14,8 +14,7 @@ function taxonomy_views_data() { $data = array(); - // taxonomy_term_data table - + // taxonomy_term_data table. $data['taxonomy_term_data']['table']['group'] = t('Taxonomy term'); $data['taxonomy_term_data']['table']['base'] = array( 'field' => 'tid', @@ -34,7 +33,7 @@ function taxonomy_views_data() { ), ); - // taxonomy_term_field_data table + // taxonomy_term_field_data table. $data['taxonomy_term_field_data']['table']['group'] = t('Content'); $data['taxonomy_term_field_data']['table']['entity type'] = 'taxonomy_term'; $data['taxonomy_term_field_data']['table']['join']['taxonomy_term_data'] = array( @@ -43,7 +42,7 @@ function taxonomy_views_data() { 'field' => 'tid', ); - // tid field + // tid field. $data['taxonomy_term_data']['tid'] = array( 'title' => t('Term ID'), 'help' => t('The tid of a taxonomy term.'), @@ -67,7 +66,7 @@ function taxonomy_views_data() { ), ); - // raw tid field + // Raw tid field. $data['taxonomy_term_data']['tid_raw'] = array( 'title' => t('Term ID'), 'help' => t('The tid of a taxonomy term.'), @@ -94,7 +93,7 @@ function taxonomy_views_data() { ), ); - // Term name field + // Term name field. $data['taxonomy_term_field_data']['name'] = array( 'title' => t('Name'), 'help' => t('The taxonomy term name.'), @@ -116,7 +115,7 @@ function taxonomy_views_data() { ), ); - // taxonomy weight + // Term weight. $data['taxonomy_term_field_data']['weight'] = array( 'title' => t('Weight'), 'help' => t('The term weight field'), @@ -134,7 +133,7 @@ function taxonomy_views_data() { ), ); - // Term description + // Term description. $data['taxonomy_term_field_data']['description__value'] = array( 'title' => t('Term description'), 'help' => t('The description associated with a taxonomy term.'), @@ -148,7 +147,7 @@ function taxonomy_views_data() { ), ); - // Term vocabulary + // Term vocabulary. $data['taxonomy_term_field_data']['vid'] = array( 'title' => t('Vocabulary'), 'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'), @@ -174,7 +173,7 @@ function taxonomy_views_data() { ), ); - // Link to edit the term + // Link to edit the term. $data['taxonomy_term_data']['edit_term'] = array( 'field' => array( 'title' => t('Term edit link'), @@ -263,8 +262,7 @@ function taxonomy_views_data() { ); } - // taxonomy_index table - + // taxonomy_index table. $data['taxonomy_index']['table']['group'] = t('Taxonomy term'); $data['taxonomy_index']['table']['join'] = array( @@ -321,8 +319,7 @@ function taxonomy_views_data() { ), ); - // term_hierarchy table - + // term_hierarchy table. $data['taxonomy_term_hierarchy']['table']['group'] = t('Taxonomy term'); $data['taxonomy_term_hierarchy']['table']['join'] = array(