diff --git a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php index 2114b85..381b82f 100644 --- a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php +++ b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php @@ -10,6 +10,7 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\TypedData\TranslatableInterface; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Entity\ContentEntityInterface; /** * Parent plugin for entity reference formatters. @@ -30,33 +31,57 @@ protected function getEntitiesToView(FieldItemListInterface $items) { /* @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $parent */ $parent = $items->getParent(); - /* @var \Drupal\Core\Entity\ContentEntityBase $target_entity */ + /* @var \Drupal\Core\Entity\ContentEntityInterface $target_entity */ $parent_entity = $parent->getValue(); $active_langcode = $parent_entity->language()->getId(); /* @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item */ foreach ($items as $delta => $item) { - /* @var \Drupal\Core\Entity\ContentEntityBase $entity */ + /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $item->originalEntity; - if ($entity instanceof TranslatableInterface) { - if ($entity->hasTranslation($active_langcode)) { - $translated_entity = $entity->getTranslation($active_langcode); - if ($translated_entity->access('view')) { - $entity = $translated_entity; - } - } + if ($translated_entity = $this->getAccessibleTranslatedEntity($entity, $active_langcode)) { + $entity = $translated_entity; + $item->access = TRUE; + } + elseif ($entity->access('view')) { + $item->access = TRUE; } - if (!$entity->access('view')) { - continue; + if ($item->access) { + $entities[$delta] = $entity; } - $entities[$delta] = $entity; } return $entities; } /** + * Returns the translated entity if accessible. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The original entity. + * @param string $langcode + * The language of the translation. + * + * @return \Drupal\Core\Entity\ContentEntityInterface|null + * The translated entity or NULL. + */ + protected function getAccessibleTranslatedEntity(ContentEntityInterface $entity, $langcode) { + if ( + $entity instanceof TranslatableInterface && + $entity->language()->getId() != $langcode && + $entity->hasTranslation($langcode) + ) { + $translated_entity = $entity->getTranslation($langcode); + if ($translated_entity->access('view')) { + return $translated_entity; + } + } + + return NULL; + } + + /** * {@inheritdoc} * * Mark the accessible IDs a user can see. We do not unset unaccessible