diff --git a/core/modules/views/src/Entity/Render/ConfigurableLanguageRenderer.php b/core/modules/views/src/Entity/Render/ConfigurableLanguageRenderer.php new file mode 100644 index 0000000..d529025 --- /dev/null +++ b/core/modules/views/src/Entity/Render/ConfigurableLanguageRenderer.php @@ -0,0 +1,51 @@ +langcode = $langcode; + } + + /** + * {@inheritdoc} + */ + public function getLangcode(ResultRow $row) { + return $this->langcode; + } + +} diff --git a/core/modules/views/src/Entity/Render/CurrentLanguageRenderer.php b/core/modules/views/src/Entity/Render/CurrentLanguageRenderer.php deleted file mode 100644 index 8ade3e2..0000000 --- a/core/modules/views/src/Entity/Render/CurrentLanguageRenderer.php +++ /dev/null @@ -1,15 +0,0 @@ -getEntityManager()->getDefinition($this->getEntityTypeId()); diff --git a/core/modules/views/src/Entity/Render/RendererBase.php b/core/modules/views/src/Entity/Render/RendererBase.php index f562eb8..be8aea6 100644 --- a/core/modules/views/src/Entity/Render/RendererBase.php +++ b/core/modules/views/src/Entity/Render/RendererBase.php @@ -23,7 +23,7 @@ * * @var \Drupal\views\ViewExecutable */ - public $view = NULL; + public $view; /** * The language manager. @@ -40,13 +40,6 @@ protected $entityType; /** - * A specific language code for rendering if available. - * - * @var string|null - */ - protected $langcode; - - /** * Contains an array of render arrays, one for each rendered entity. * * @var array @@ -62,14 +55,11 @@ * The language manager. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. - * @param string|null $langcode - * A specific language code to set, if available. */ - public function __construct(ViewExecutable $view, LanguageManagerInterface $language_manager, EntityTypeInterface $entity_type, $langcode = NULL) { + public function __construct(ViewExecutable $view, LanguageManagerInterface $language_manager, EntityTypeInterface $entity_type) { $this->view = $view; $this->languageManager = $language_manager; $this->entityType = $entity_type; - $this->langcode = $langcode; } /** @@ -81,9 +71,7 @@ public function __construct(ViewExecutable $view, LanguageManagerInterface $lang * @return string * A language code. */ - public function getLangcode(ResultRow $row) { - return $this->langcode; - } + abstract public function getLangcode(ResultRow $row); /** * Alters the query if needed. diff --git a/core/modules/views/src/Plugin/views/field/Field.php b/core/modules/views/src/Plugin/views/field/Field.php index 48063dc..2285c20 100644 --- a/core/modules/views/src/Plugin/views/field/Field.php +++ b/core/modules/views/src/Plugin/views/field/Field.php @@ -271,6 +271,7 @@ public function query($use_groupby = FALSE) { $this->addAdditionalFields($fields); } + // Let the configured entity translation renderer alter the query if needed. $this->getEntityTranslationRenderer()->query($this->query); } @@ -765,7 +766,7 @@ public function getItems(ResultRow $values) { function process_entity(ResultRow $values, EntityInterface $entity) { $processed_entity = clone $entity; - $langcode = $this->fieldLangcodeAvailable($processed_entity, $values); + $langcode = $this->getFieldLangcode($processed_entity, $values); $processed_entity = $processed_entity->getTranslation($langcode); // If we are grouping, copy our group fields into the cloned entity. @@ -893,12 +894,23 @@ protected function addSelfTokens(&$tokens, $item) { } /** - * Return the language code of the language the field should be displayed in, - * according to the settings. + * Return the code of the language the field should be displayed in. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity object the field value being processed is attached to. + * @param \Drupal\views\ResultRow $row + * The result row the field value being processed belongs to. + * + * @return string + * The field language code. */ - public function fieldLangcodeAvailable(EntityInterface $entity, ResultRow $row) { + public function getFieldLangcode(EntityInterface $entity, ResultRow $row) { if ($this->getFieldDefinition()->isTranslatable()) { - $langcode = $this->fieldLangcodeConfigured($row); + // Even if the current field is not attached to the main entity, we use it + // to determine the field language, as we assume the same language should + // be used for all values belonging to a single row, when possible. Below + // we apply language fallback to ensure a valid value is always picked. + $langcode = $this->getEntityTranslationRenderer()->getLangcode($row); // Give the Entity Field API a chance to fallback to a different language // (or LanguageInterface::LANGCODE_NOT_SPECIFIED), in case the field has @@ -915,19 +927,6 @@ public function fieldLangcodeAvailable(EntityInterface $entity, ResultRow $row) } /** - * Resolve the language code to be used for fields. - * - * @param \Drupal\views\ResultRow $row - * (optional) The result row. Defaults to NULL. - * - * @return string - * Language code to be used. - */ - protected function fieldLangcodeConfigured(ResultRow $row = NULL) { - return $this->getEntityTranslationRenderer()->getLangcode($row); - } - - /** * {@inheritdoc} */ public function calculateDependencies() {