3 namespace Drupal\views\Entity\Render;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\views\Plugin\views\query\QueryPluginBase;
7 use Drupal\views\ResultRow;
10 * Renders entity translations in their row language.
12 class TranslationLanguageRenderer extends EntityTranslationRendererBase {
15 * Stores the field alias of the langcode column.
19 protected $langcodeAlias;
24 public function query(QueryPluginBase $query, $relationship = NULL) {
25 // In order to render in the translation language of the entity, we need
26 // to add the language code of the entity to the query. Skip if the site
27 // is not multilingual or the entity is not translatable.
28 if (!$this->languageManager->isMultilingual() || !$this->entityType->hasKey('langcode')) {
31 $langcode_table = $this->getLangcodeTable($query, $relationship);
32 if ($langcode_table) {
33 /** @var \Drupal\views\Plugin\views\query\Sql $query */
34 $table_alias = $query->ensureTable($langcode_table, $relationship);
35 $langcode_key = $this->entityType->getKey('langcode');
36 $this->langcodeAlias = $query->addField($table_alias, $langcode_key);
41 * Returns the name of the table holding the "langcode" field.
43 * @param \Drupal\views\Plugin\views\query\QueryPluginBase $query
44 * The query being executed.
45 * @param string $relationship
46 * The relationship used by the entity type.
51 protected function getLangcodeTable(QueryPluginBase $query, $relationship) {
52 /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
53 $storage = \Drupal::entityTypeManager()->getStorage($this->entityType->id());
54 $langcode_key = $this->entityType->getKey('langcode');
55 $langcode_table = $storage->getTableMapping()->getFieldTableName($langcode_key);
57 // If the entity type is revisionable, we need to take into account views of
58 // entity revisions. Usually the view will use the entity data table as the
59 // query base table, however, in case of an entity revision view, we need to
60 // use the revision table or the revision data table, depending on which one
61 // is being used as query base table.
62 if ($this->entityType->isRevisionable()) {
63 $query_base_table = isset($query->relationships[$relationship]['base']) ?
64 $query->relationships[$relationship]['base'] :
65 $this->view->storage->get('base_table');
66 $revision_table = $storage->getRevisionTable();
67 $revision_data_table = $storage->getRevisionDataTable();
68 if ($query_base_table === $revision_table) {
69 $langcode_table = $revision_table;
71 elseif ($query_base_table === $revision_data_table) {
72 $langcode_table = $revision_data_table;
76 return $langcode_table;
82 public function preRender(array $result) {
83 $view_builder = $this->view->rowPlugin->entityManager->getViewBuilder($this->entityType->id());
85 /** @var \Drupal\views\ResultRow $row */
86 foreach ($result as $row) {
87 $entity = $row->_entity;
88 $entity->view = $this->view;
89 $langcode = $this->getLangcode($row);
90 $this->build[$entity->id()][$langcode] = $view_builder->view($entity, $this->view->rowPlugin->options['view_mode'], $this->getLangcode($row));
97 public function render(ResultRow $row) {
98 $entity_id = $row->_entity->id();
99 $langcode = $this->getLangcode($row);
100 return $this->build[$entity_id][$langcode];
106 public function getLangcode(ResultRow $row) {
107 return isset($row->{$this->langcodeAlias}) ? $row->{$this->langcodeAlias} : $this->languageManager->getDefaultLanguage()->getId();
113 public function getCacheContexts() {
114 return ['languages:' . LanguageInterface::TYPE_CONTENT];