From 8080823733702f608f5e4320e1735847cb213fe5 Mon Sep 17 00:00:00 2001 From: William Hearn Date: Sun, 17 Dec 2017 12:40:13 -0500 Subject: [PATCH] Issue #2765297 by sylus, heikki, googletorp, dnotes: Return translated term name on views "Content: Has taxonomy term ID (with depth)" --- .../src/Plugin/views/argument/IndexTidDepth.php | 19 ++++++++++++++++--- .../taxonomy/src/Plugin/views/argument/Taxonomy.php | 14 +++++++++++--- core/modules/taxonomy/taxonomy.tokens.inc | 3 ++- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/core/modules/taxonomy/src/Plugin/views/argument/IndexTidDepth.php b/core/modules/taxonomy/src/Plugin/views/argument/IndexTidDepth.php index 02b5995..b62605b 100644 --- a/core/modules/taxonomy/src/Plugin/views/argument/IndexTidDepth.php +++ b/core/modules/taxonomy/src/Plugin/views/argument/IndexTidDepth.php @@ -3,6 +3,7 @@ namespace Drupal\taxonomy\Plugin\views\argument; use Drupal\Core\Database\Query\Condition; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -22,6 +23,11 @@ class IndexTidDepth extends ArgumentPluginBase implements ContainerFactoryPluginInterface { /** + * @var EntityRepositoryInterface + */ + protected $entityRepository; + + /** * @var \Drupal\Core\Entity\EntityStorageInterface */ protected $termStorage; @@ -29,9 +35,10 @@ class IndexTidDepth extends ArgumentPluginBase implements ContainerFactoryPlugin /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $termStorage) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $termStorage, EntityRepositoryInterface $entityRepository) { parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->entityRepository = $entityRepository; $this->termStorage = $termStorage; } @@ -39,7 +46,13 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity.manager')->getStorage('taxonomy_term')); + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.manager')->getStorage('taxonomy_term'), + $container->get('entity.repository') + ); } protected function defineOptions() { @@ -134,7 +147,7 @@ public function query($group_by = FALSE) { public function title() { $term = $this->termStorage->load($this->argument); if (!empty($term)) { - return $term->getName(); + return $this->entityRepository->getTranslationFromContext($term)->label(); } // TODO review text return $this->t('No name'); diff --git a/core/modules/taxonomy/src/Plugin/views/argument/Taxonomy.php b/core/modules/taxonomy/src/Plugin/views/argument/Taxonomy.php index b02b84f..af29c59 100644 --- a/core/modules/taxonomy/src/Plugin/views/argument/Taxonomy.php +++ b/core/modules/taxonomy/src/Plugin/views/argument/Taxonomy.php @@ -2,6 +2,7 @@ namespace Drupal\taxonomy\Plugin\views\argument; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\views\Plugin\views\argument\NumericArgument; @@ -17,6 +18,11 @@ class Taxonomy extends NumericArgument implements ContainerFactoryPluginInterface { /** + * @var EntityRepositoryInterface + */ + protected $entityRepository; + + /** * @var \Drupal\Core\Entity\EntityStorageInterface */ protected $termStorage; @@ -24,9 +30,10 @@ class Taxonomy extends NumericArgument implements ContainerFactoryPluginInterfac /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $term_storage) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $term_storage, EntityRepositoryInterface $entityRepository) { parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->entityRepository = $entityRepository; $this->termStorage = $term_storage; } @@ -38,7 +45,8 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('entity.manager')->getStorage('taxonomy_term') + $container->get('entity.manager')->getStorage('taxonomy_term'), + $container->get('entity.repository') ); } @@ -50,7 +58,7 @@ public function title() { if ($this->argument) { $term = $this->termStorage->load($this->argument); if (!empty($term)) { - return $term->getName(); + return $this->entityRepository->getTranslationFromContext($term)->label(); } } // TODO review text diff --git a/core/modules/taxonomy/taxonomy.tokens.inc b/core/modules/taxonomy/taxonomy.tokens.inc index 211a121..764fd04 100644 --- a/core/modules/taxonomy/taxonomy.tokens.inc +++ b/core/modules/taxonomy/taxonomy.tokens.inc @@ -98,6 +98,7 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable $taxonomy_storage = \Drupal::entityManager()->getStorage('taxonomy_term'); if ($type == 'term' && !empty($data['term'])) { $term = $data['term']; + $term = \Drupal::entityManager()->getTranslationFromContext($term); foreach ($tokens as $name => $original) { switch ($name) { @@ -106,7 +107,7 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable break; case 'name': - $replacements[$original] = $term->getName(); + $replacements[$original] = $term->label(); break; case 'description': -- 2.5.4 (Apple Git-61)