function EntityViewDisplay::collectRenderDisplays
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php \Drupal\Core\Entity\Entity\EntityViewDisplay::collectRenderDisplays()
- 10 core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php \Drupal\Core\Entity\Entity\EntityViewDisplay::collectRenderDisplays()
- 11.x core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php \Drupal\Core\Entity\Entity\EntityViewDisplay::collectRenderDisplays()
Returns the display objects used to render a set of entities.
Depending on the configuration of the view mode for each bundle, this can be either the display object associated with the view mode, or the 'default' display.
This method should only be used internally when rendering an entity. When assigning suggested display options for a component in a given view mode, EntityDisplayRepositoryInterface::getViewDisplay() should be used instead, in order to avoid inadvertently modifying the output of other view modes that might happen to use the 'default' display too. Those options will then be effectively applied only if the view mode is configured to use them.
hook_entity_view_display_alter() is invoked on each display, allowing 3rd party code to alter the display options held in the display before they are used to generate render arrays.
Parameters
\Drupal\Core\Entity\FieldableEntityInterface[] $entities: The entities being rendered. They should all be of the same entity type.
string $view_mode: The view mode being rendered.
Return value
\Drupal\Core\Entity\Display\EntityViewDisplayInterface[] The display objects to use to render the entities, keyed by entity bundle.
See also
\Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewDisplay()
hook_entity_view_display_alter()
2 calls to EntityViewDisplay::collectRenderDisplays()
- EntityViewBuilder::buildMultiple in core/
lib/ Drupal/ Core/ Entity/ EntityViewBuilder.php - Builds multiple entities' views; augments entity defaults.
- EntityViewDisplay::collectRenderDisplay in core/
lib/ Drupal/ Core/ Entity/ Entity/ EntityViewDisplay.php - Returns the display object used to render an entity.
File
-
core/
lib/ Drupal/ Core/ Entity/ Entity/ EntityViewDisplay.php, line 77
Class
- EntityViewDisplay
- Configuration entity.
Namespace
Drupal\Core\Entity\EntityCode
public static function collectRenderDisplays($entities, $view_mode) {
if (empty($entities)) {
return [];
}
// Collect entity type and bundles.
$entity_type = current($entities)->getEntityTypeId();
$bundles = [];
foreach ($entities as $entity) {
$bundles[$entity->bundle()] = TRUE;
}
$bundles = array_keys($bundles);
// For each bundle, check the existence and status of:
// - the display for the view mode,
// - the 'default' display.
$candidate_ids = [];
foreach ($bundles as $bundle) {
if ($view_mode != 'default') {
$candidate_ids[$bundle][] = $entity_type . '.' . $bundle . '.' . $view_mode;
}
$candidate_ids[$bundle][] = $entity_type . '.' . $bundle . '.default';
}
$results = \Drupal::entityQuery('entity_view_display')->condition('id', NestedArray::mergeDeepArray($candidate_ids))
->condition('status', TRUE)
->execute();
// For each bundle, select the first valid candidate display, if any.
$load_ids = [];
foreach ($bundles as $bundle) {
foreach ($candidate_ids[$bundle] as $candidate_id) {
if (isset($results[$candidate_id])) {
$load_ids[$bundle] = $candidate_id;
break;
}
}
}
// Load the selected displays.
$storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
$displays = $storage->loadMultiple($load_ids);
$displays_by_bundle = [];
foreach ($bundles as $bundle) {
// Use the selected display if any, or create a fresh runtime object.
if (isset($load_ids[$bundle])) {
$display = $displays[$load_ids[$bundle]];
}
else {
$display = $storage->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $view_mode,
'status' => TRUE,
]);
}
// Let the display know which view mode was originally requested.
$display->originalMode = $view_mode;
// Let modules alter the display.
$display_context = [
'entity_type' => $entity_type,
'bundle' => $bundle,
'view_mode' => $view_mode,
];
\Drupal::moduleHandler()->alter('entity_view_display', $display, $display_context);
$displays_by_bundle[$bundle] = $display;
}
return $displays_by_bundle;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.