.../EntityReferenceFormatterBase.php | 37 ++++++++++++---------- .../Field/FieldFormatter/AuthorFormatter.php | 20 ++++++------ 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php index 35ee5ad..8c843ff 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php @@ -41,16 +41,25 @@ * * @see ::prepareView() */ - protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items) { + protected function getEntitiesToView(EntityReferenceFieldItemListInterface &$items) { $entities = array(); + $parent_entity_langcode = $items->getEntity()->language()->getId(); foreach ($items as $delta => $item) { // Ignore items where no entity could be loaded in prepareView(). if (!empty($item->_loaded)) { /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $item->entity; - if ($item->_access->isAllowed()) { + // Set the entity in the correct language for display. + if ($entity instanceof TranslatableInterface && $entity->hasTranslation($parent_entity_langcode)) { + $entity = $entity->getTranslation($parent_entity_langcode); + } + + $access = $this->checkAccess($item); + // Add the access result's cacheability, ::view() needs it. + $item->_accessCacheability = BubbleableMetadata::createFromObject($access); + if ($access->isAllowed()) { // Add the referring item, in case the formatter needs it. $entity->_referringItem = $items[$delta]; $entities[$delta] = $entity; @@ -65,31 +74,27 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item * {@inheritdoc} * * @see ::prepareView() + * @see ::getEntitiestoView() */ public function view(FieldItemListInterface $items) { $addition = parent::view($items); $field_level_access_cacheability = new BubbleableMetadata(); - // Try to map the cacheability of the access result that was set at _access - // in prepareView() to the corresponding render subtree. If no such subtree - // is found, then merge it with the field-level access cacheability. + // Try to map the cacheability of the access result that was set at + // _accessCacheability in getEntitiesToView() to the corresponding render + // subtree. If no such subtree is found, then merge it with the field-level + // access cacheability. foreach ($items as $delta => $item) { // Ignore items where no entity could be loaded in prepareView(). if (!empty($item->_loaded)) { - $access_cacheability = BubbleableMetadata::createFromObject($item->_access); - if ($item->_access->isAllowed()) { - if (isset($addition[$delta])) { - BubbleableMetadata::createFromRenderArray($addition[$delta]) - ->merge($access_cacheability) - ->applyTo($addition[$delta]); - } - else { - $field_level_access_cacheability = $field_level_access_cacheability->merge($access_cacheability); - } + if (isset($addition[$delta])) { + BubbleableMetadata::createFromRenderArray($addition[$delta]) + ->merge($item->_accessCacheability) + ->applyTo($addition[$delta]); } else { - $field_level_access_cacheability = $field_level_access_cacheability->merge($access_cacheability); + $field_level_access_cacheability = $field_level_access_cacheability->merge($item->_accessCacheability); } } } diff --git a/core/modules/user/src/Plugin/Field/FieldFormatter/AuthorFormatter.php b/core/modules/user/src/Plugin/Field/FieldFormatter/AuthorFormatter.php index 328f513..674b936 100644 --- a/core/modules/user/src/Plugin/Field/FieldFormatter/AuthorFormatter.php +++ b/core/modules/user/src/Plugin/Field/FieldFormatter/AuthorFormatter.php @@ -31,18 +31,16 @@ class AuthorFormatter extends EntityReferenceFormatterBase { public function viewElements(FieldItemListInterface $items) { $elements = array(); - foreach ($items as $delta => $item) { + foreach ($this->getEntitiesToView($items) as $delta => $entity) { /** @var $referenced_user \Drupal\user\UserInterface */ - if ($referenced_user = $item->entity) { - $elements[$delta] = array( - '#theme' => 'username', - '#account' => $referenced_user, - '#link_options' => array('attributes' => array('rel' => 'author')), - '#cache' => array( - 'tags' => $referenced_user->getCacheTags(), - ), - ); - } + $elements[$delta] = array( + '#theme' => 'username', + '#account' => $entity, + '#link_options' => array('attributes' => array('rel' => 'author')), + '#cache' => array( + 'tags' => $entity->getCacheTags(), + ), + ); } return $elements;