diff --git a/core/modules/views/src/Entity/Render/DefaultLanguageRenderer.php b/core/modules/views/src/Entity/Render/DefaultLanguageRenderer.php index a292efe271..15811aa257 100644 --- a/core/modules/views/src/Entity/Render/DefaultLanguageRenderer.php +++ b/core/modules/views/src/Entity/Render/DefaultLanguageRenderer.php @@ -19,7 +19,7 @@ public function getLangcode(ResultRow $row) { /** * {@inheritdoc} */ - public function getLangcodeByRelationship(ResultRow $row, $relationship = 'none') { + public function getLangcodeByRelationship(ResultRow $row, string $relationship = 'none'): string { $entity = $this->getEntity($row, $relationship); return $entity->getUntranslated()->language()->getId(); } diff --git a/core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php b/core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php index c7d4bf6774..1a862482b8 100644 --- a/core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php +++ b/core/modules/views/src/Entity/Render/EntityTranslationRenderTrait.php @@ -80,20 +80,19 @@ public function getEntityTranslation(EntityInterface $entity, ResultRow $row) { * @param string $relationship * The relationship to be used, or 'none' by default. * - * @return \Drupal\Core\Entity\FieldableEntityInterface + * @return \Drupal\Core\Entity\EntityInterface * The entity translation object for the specified row. */ - public function getEntityTranslationByRelationship(EntityInterface $entity, ResultRow $row, $relationship = 'none') { + public function getEntityTranslationByRelationship(EntityInterface $entity, ResultRow $row, string $relationship = 'none'): EntityInterface { // We assume the same language should be used for all entity fields // belonging to a single row, even if they are attached to different entity // types. Below we apply language fallback to ensure a valid value is always // picked. - $translation = $entity; if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) { $langcode = $this->getEntityTranslationRenderer()->getLangcodeByRelationship($row, $relationship); $translation = $this->getEntityRepository()->getTranslationFromContext($entity, $langcode); } - return $translation; + return $translation ?? $entity; } /** diff --git a/core/modules/views/src/Entity/Render/EntityTranslationRendererBase.php b/core/modules/views/src/Entity/Render/EntityTranslationRendererBase.php index 4915b757c8..0db4f0eb20 100644 --- a/core/modules/views/src/Entity/Render/EntityTranslationRendererBase.php +++ b/core/modules/views/src/Entity/Render/EntityTranslationRendererBase.php @@ -2,6 +2,7 @@ namespace Drupal\views\Entity\Render; +use Drupal\Core\Entity\EntityInterface; use Drupal\views\Plugin\views\query\QueryPluginBase; use Drupal\views\ResultRow; @@ -31,7 +32,7 @@ abstract public function getLangcode(ResultRow $row); * * @return string */ - public function getLangcodeByRelationship(ResultRow $row, $relationship) { + public function getLangcodeByRelationship(ResultRow $row, string $relationship): string { // This method needs to be overridden if the relationship is needed in the // implementation of getLangcode(). return $this->getLangcode($row); @@ -58,7 +59,7 @@ public function preRender(array $result) { * @param string $relationship * The relationship to be used. */ - public function preRenderByRelationship(array $result, $relationship) { + public function preRenderByRelationship(array $result, string $relationship): void { $view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->entityType->id()); foreach ($result as $row) { @@ -87,7 +88,7 @@ public function render(ResultRow $row) { * @return array * A renderable array for the entity data contained in the result row. */ - public function renderByRelationship(ResultRow $row, $relationship) { + public function renderByRelationship(ResultRow $row, string $relationship): array { if ($entity = $this->getEntity($row, $relationship)) { $entity_id = $entity->id(); return $this->build[$entity_id]; @@ -107,7 +108,7 @@ public function renderByRelationship(ResultRow $row, $relationship) { * The entity might be optional, because the relationship entity might not * always exist. */ - protected function getEntity($row, $relationship = 'none') { + protected function getEntity(ResultRow $row, string $relationship = 'none'): ?EntityInterface { if ($relationship === 'none') { return $row->_entity; } diff --git a/core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php b/core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php index 1cd55f2e6e..07976988e8 100644 --- a/core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php +++ b/core/modules/views/src/Entity/Render/TranslationLanguageRenderer.php @@ -78,7 +78,7 @@ protected function getLangcodeTable(QueryPluginBase $query, $relationship) { /** * {@inheritdoc} */ - public function preRenderByRelationship(array $result, $relationship) { + public function preRenderByRelationship(array $result, string $relationship): void { $view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->entityType->id()); /** @var \Drupal\views\ResultRow $row */ @@ -94,11 +94,12 @@ public function preRenderByRelationship(array $result, $relationship) { /** * {@inheritdoc} */ - public function renderByRelationship(ResultRow $row, $relationship) { + public function renderByRelationship(ResultRow $row, string $relationship): array { if ($entity = $this->getEntity($row, $relationship)) { $entity_id = $entity->id(); return $this->build[$entity_id][$this->getLangcodeByRelationship($row, $relationship)]; } + return []; } /**