diff --git a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php index ed01995..cadf626 100644 --- a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php +++ b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php @@ -69,10 +69,8 @@ public function settingsSummary() { */ public function viewElements(FieldItemListInterface $items) { $view_mode = $this->getSetting('view_mode'); - $elements = array(); - /* @var \Drupal\Core\Entity\EntityInterface $entity */ foreach ($this->getEntitiesToView($items) as $delta => $entity) { // Protect ourselves from recursive rendering. static $depth = 0; 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 71763d0..e038d64 100644 --- a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php +++ b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php @@ -20,26 +20,28 @@ /** * Returns the accessible and translated entities for view. * - * @param FieldItemListInterface $items + * @param \Drupal\Core\Field\FieldItemListInterface $items * The item list. * - * @return array + * @return \Drupal\Core\Entity\EntityInterface[] * The entities to view. */ protected function getEntitiesToView(FieldItemListInterface $items) { $entities = array(); - /* @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $parent */ - $parent = $items->getParent(); - /* @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 */ + $active_langcode = $items->getEntity()->language()->getId(); foreach ($items as $delta => $item) { - /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - if ($entity = $this->getAccessibleTranslatedEntity($item->originalEntity, $active_langcode)) { + if ($item->originalEntity instanceof TranslatableInterface && $item->originalEntity->hasTranslation($active_langcode)) { + $entity = $item->originalEntity->getTranslation($active_langcode); + } + else { + $entity = $item->originalEntity; + } + + if ($entity->access('view')) { $entities[$delta] = $entity; + + // Mark item as accessible. $item->access = TRUE; } } @@ -48,32 +50,6 @@ protected function getEntitiesToView(FieldItemListInterface $items) { } /** - * Returns the translated entity if accessible. - * - * @param \Drupal\Core\Entity\EntityInterface $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(EntityInterface $entity, $langcode) { - if ( - $entity instanceof TranslatableInterface && - $entity->hasTranslation($langcode) - ) { - $entity = $entity->getTranslation($langcode); - } - - if ($entity->access('view')) { - return $entity; - } - - return NULL; - } - - /** * {@inheritdoc} * * Mark the accessible IDs a user can see. We do not unset unaccessible diff --git a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php index 81c02d9..bd22eea 100644 --- a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php +++ b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php @@ -30,7 +30,6 @@ class EntityReferenceIdFormatter extends EntityReferenceFormatterBase { public function viewElements(FieldItemListInterface $items) { $elements = array(); - /* @var \Drupal\Core\Entity\EntityInterface $entity */ foreach ($this->getEntitiesToView($items) as $delta => $entity) { if ($entity->id()) { $elements[$delta] = array( diff --git a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php index 1318675..eb1568e 100644 --- a/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php +++ b/core/modules/entity_reference/src/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php @@ -60,10 +60,8 @@ public function settingsSummary() { * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { - $this->getEntitiesToView($items); $elements = array(); - /* @var \Drupal\Core\Entity\EntityInterface $entity */ foreach ($this->getEntitiesToView($items) as $delta => $entity) { $label = $entity->label(); // If the link is to be displayed and the entity has a uri, display a diff --git a/core/modules/taxonomy/src/Plugin/Field/FieldFormatter/EntityReferenceTaxonomyTermRssFormatter.php b/core/modules/taxonomy/src/Plugin/Field/FieldFormatter/EntityReferenceTaxonomyTermRssFormatter.php index eec4659..02fc472 100644 --- a/core/modules/taxonomy/src/Plugin/Field/FieldFormatter/EntityReferenceTaxonomyTermRssFormatter.php +++ b/core/modules/taxonomy/src/Plugin/Field/FieldFormatter/EntityReferenceTaxonomyTermRssFormatter.php @@ -31,9 +31,6 @@ class EntityReferenceTaxonomyTermRssFormatter extends EntityReferenceFormatterBa public function viewElements(FieldItemListInterface $items) { $elements = array(); - - - /* @var \Drupal\Core\Entity\EntityInterface $entity */ foreach ($this->getEntitiesToView($items) as $delta => $entity) { $entity->rss_elements[] = array( 'key' => 'category',