/** * Returns the referenced entities for display. * * The method takes care of: * - checking entity access, * - placing the entities in the language expected for display. * It is thus strongly recommended that formatters use it in their * implementation of viewElements($items) rather than dealing with $items * directly. * * For each entity, the EntityReferenceItem by which the entity is referenced * is available in $entity->_referringItem. This is useful for field types * that store additional values next to the reference itself. * * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items * The item list. * * @return \Drupal\Core\Entity\EntityInterface[] * The array of referenced entities to display, keyed by delta. */ 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)) { $entity = $item->entity; // Set the entity in the correct language for display. if ($entity instanceof TranslatableInterface && $entity->hasTranslation($parent_entity_langcode)) { $entity = $entity->getTranslation($parent_entity_langcode); } // Check entity access if needed. if (!$this->needsAccessCheck($item) || $entity->access('view')) { // Add the referring item, in case the formatter needs it. $entity->_referringItem = $items[$delta]; $entities[$delta] = $entity; } } } return $entities; }
/** * Returns the referenced entities for display. * * The method takes care of: * - checking entity access, * - placing the entities in the language expected for display. * It is thus strongly recommended that formatters use it in their * implementation of viewElements($items) rather than dealing with $items * directly. * * For each entity, the EntityReferenceItem by which the entity is referenced * is available in $entity->_referringItem. This is useful for field types * that store additional values next to the reference itself. * * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items * The item list. * * @return \Drupal\Core\Entity\EntityInterface[] * The array of referenced entities to display, keyed by delta. * * @see ::prepareView() */ 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)) { $entity = $item->entity; // 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($entity); // Add the access result's cacheability, ::view() needs it. $item->_accessCacheability = CacheableMetadata::createFromObject($access); if ($access->isAllowed()) { // Add the referring item, in case the formatter needs it. $entity->_referringItem = $items[$delta]; $entities[$delta] = $entity; } } } return $entities; }