Chris@0: languageManager->isMultilingual() || !$this->entityType->hasKey('langcode')) { Chris@0: return; Chris@0: } Chris@0: $langcode_table = $this->getLangcodeTable($query, $relationship); Chris@0: if ($langcode_table) { Chris@0: /** @var \Drupal\views\Plugin\views\query\Sql $query */ Chris@0: $table_alias = $query->ensureTable($langcode_table, $relationship); Chris@0: $langcode_key = $this->entityType->getKey('langcode'); Chris@0: $this->langcodeAlias = $query->addField($table_alias, $langcode_key); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the name of the table holding the "langcode" field. Chris@0: * Chris@0: * @param \Drupal\views\Plugin\views\query\QueryPluginBase $query Chris@0: * The query being executed. Chris@0: * @param string $relationship Chris@0: * The relationship used by the entity type. Chris@0: * Chris@0: * @return string Chris@0: * A table name. Chris@0: */ Chris@0: protected function getLangcodeTable(QueryPluginBase $query, $relationship) { Chris@0: /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */ Chris@0: $storage = \Drupal::entityTypeManager()->getStorage($this->entityType->id()); Chris@0: $langcode_key = $this->entityType->getKey('langcode'); Chris@0: $langcode_table = $storage->getTableMapping()->getFieldTableName($langcode_key); Chris@0: Chris@0: // If the entity type is revisionable, we need to take into account views of Chris@0: // entity revisions. Usually the view will use the entity data table as the Chris@0: // query base table, however, in case of an entity revision view, we need to Chris@0: // use the revision table or the revision data table, depending on which one Chris@0: // is being used as query base table. Chris@0: if ($this->entityType->isRevisionable()) { Chris@0: $query_base_table = isset($query->relationships[$relationship]['base']) ? Chris@0: $query->relationships[$relationship]['base'] : Chris@0: $this->view->storage->get('base_table'); Chris@0: $revision_table = $storage->getRevisionTable(); Chris@0: $revision_data_table = $storage->getRevisionDataTable(); Chris@0: if ($query_base_table === $revision_table) { Chris@0: $langcode_table = $revision_table; Chris@0: } Chris@0: elseif ($query_base_table === $revision_data_table) { Chris@0: $langcode_table = $revision_data_table; Chris@0: } Chris@0: } Chris@0: Chris@0: return $langcode_table; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function preRender(array $result) { Chris@18: $view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->entityType->id()); Chris@0: Chris@0: /** @var \Drupal\views\ResultRow $row */ Chris@0: foreach ($result as $row) { Chris@0: $entity = $row->_entity; Chris@0: $entity->view = $this->view; Chris@0: $langcode = $this->getLangcode($row); Chris@0: $this->build[$entity->id()][$langcode] = $view_builder->view($entity, $this->view->rowPlugin->options['view_mode'], $this->getLangcode($row)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function render(ResultRow $row) { Chris@0: $entity_id = $row->_entity->id(); Chris@0: $langcode = $this->getLangcode($row); Chris@0: return $this->build[$entity_id][$langcode]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getLangcode(ResultRow $row) { Chris@0: return isset($row->{$this->langcodeAlias}) ? $row->{$this->langcodeAlias} : $this->languageManager->getDefaultLanguage()->getId(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCacheContexts() { Chris@0: return ['languages:' . LanguageInterface::TYPE_CONTENT]; Chris@0: } Chris@0: Chris@0: }