TranslationLanguageRenderer.php in Drupal 10
File
core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php
View source
<?php
namespace Drupal\views\Entity\Render;
use Drupal\Core\Language\LanguageInterface;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ResultRow;
class TranslationLanguageRenderer extends EntityTranslationRendererBase {
protected $langcodeAlias;
public function query(QueryPluginBase $query, $relationship = NULL) {
if (!$this->languageManager
->isMultilingual() || !$this->entityType
->hasKey('langcode')) {
return;
}
$langcode_table = $this
->getLangcodeTable($query, $relationship);
if ($langcode_table) {
$table_alias = $query
->ensureTable($langcode_table, $relationship);
$langcode_key = $this->entityType
->getKey('langcode');
$this->langcodeAlias = $query
->addField($table_alias, $langcode_key);
}
}
protected function getLangcodeTable(QueryPluginBase $query, $relationship) {
$storage = \Drupal::entityTypeManager()
->getStorage($this->entityType
->id());
$langcode_key = $this->entityType
->getKey('langcode');
$langcode_table = $storage
->getTableMapping()
->getFieldTableName($langcode_key);
if ($this->entityType
->isRevisionable()) {
$query_base_table = $query->relationships[$relationship]['base'] ?? $this->view->storage
->get('base_table');
$revision_table = $storage
->getRevisionTable();
$revision_data_table = $storage
->getRevisionDataTable();
if ($query_base_table === $revision_table) {
$langcode_table = $revision_table;
}
elseif ($query_base_table === $revision_data_table) {
$langcode_table = $revision_data_table;
}
}
return $langcode_table;
}
public function preRender(array $result) {
$view_builder = \Drupal::entityTypeManager()
->getViewBuilder($this->entityType
->id());
foreach ($result as $row) {
$entity = $row->_entity;
$entity->view = $this->view;
$langcode = $this
->getLangcode($row);
$this->build[$entity
->id()][$langcode] = $view_builder
->view($entity, $this->view->rowPlugin->options['view_mode'], $this
->getLangcode($row));
}
}
public function render(ResultRow $row) {
$entity_id = $row->_entity
->id();
$langcode = $this
->getLangcode($row);
return $this->build[$entity_id][$langcode];
}
public function getLangcode(ResultRow $row) {
return $row->{$this->langcodeAlias} ?? $this->languageManager
->getDefaultLanguage()
->getId();
}
public function getCacheContexts() {
return [
'languages:' . LanguageInterface::TYPE_CONTENT,
];
}
}