protected function ConfigPagesReferenceFieldFormatter::getEntitiesToView in Config Pages 8.2
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.
Parameters
\Drupal\Core\Field\EntityReferenceFieldItemListInterface $items: The item list.
string $langcode: The language code of the referenced entities to display.
Return value
\Drupal\Core\Entity\EntityInterface[] The array of referenced entities to display, keyed by delta.
Overrides EntityReferenceFormatterBase::getEntitiesToView
See also
::prepareView()
File
- src/
Plugin/ Field/ FieldFormatter/ ConfigPagesReferenceFieldFormatter.php, line 28
Class
- ConfigPagesReferenceFieldFormatter
- Plugin implementation of the 'config page entity reference' formatter.
Namespace
Drupal\config_pages\Plugin\Field\FieldFormatterCode
protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
$entities = [];
foreach ($items as $delta => $item) {
// Ignore items where no entity could be loaded in prepareView().
if (empty($item->_loaded)) {
continue;
}
$entity = $item->entity;
$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];
$configPageType = $entity
->id();
$storage = $this->entityTypeManager
->getStorage('config_pages');
$configPage = $storage
->load($configPageType);
if ($configPage instanceof ConfigPagesInterface) {
$entities[$delta] = $configPage;
}
}
}
return $entities;
}