You are here

public static function EntityViewDisplay::collectRenderDisplays in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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, entity_get_display() 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

entity_get_display()

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 76
Contains \Drupal\Core\Entity\Entity\EntityViewDisplay.

Class

EntityViewDisplay
Configuration entity that contains display options for all components of a rendered entity in a given view mode.

Namespace

Drupal\Core\Entity\Entity

Code

public static function collectRenderDisplays($entities, $view_mode) {
  if (empty($entities)) {
    return array();
  }

  // Collect entity type and bundles.
  $entity_type = current($entities)
    ->getEntityTypeId();
  $bundles = array();
  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 = array();
  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 = array();
  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::entityManager()
    ->getStorage('entity_view_display');
  $displays = $storage
    ->loadMultiple($load_ids);
  $displays_by_bundle = array();
  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(array(
        '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 = array(
      '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;
}