EntityViewDisplay::buildMultiple

public EntityViewDisplay::buildMultiple(array $entities)

Builds a renderable array for the components of a set of entities.

This only includes the components handled by the Display object, but excludes 'extra fields', that are typically rendered through specific, ad-hoc code in EntityViewBuilderInterface::buildComponents() or in hook_entity_view() implementations.

hook_entity_display_build_alter() is invoked on each entity, allowing 3rd party code to alter the render array.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface[] $entities: The entities being displayed.

Return value

array A renderable array for the entities, indexed by the same keys as the $entities array parameter.

Overrides EntityViewDisplayInterface::buildMultiple

See also

hook_entity_display_build_alter()

File

core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php, line 224

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 function buildMultiple(array $entities) {
  $build_list = array();
  foreach ($entities as $key => $entity) {
    $build_list[$key] = array();
  }

  // Run field formatters.
  foreach ($this->getComponents() as $name => $options) {
    if ($formatter = $this->getRenderer($name)) {
      // Group items across all entities and pass them to the formatter's
      // prepareView() method.
      $grouped_items = array();
      foreach ($entities as $id => $entity) {
        $items = $entity->get($name);
        $items->filterEmptyItems();
        $grouped_items[$id] = $items;
      }
      $formatter->prepareView($grouped_items);

      // Then let the formatter build the output for each entity.
      foreach ($entities as $id => $entity) {
        $items = $grouped_items[$id];
        /** @var \Drupal\Core\Access\AccessResultInterface $field_access */
        $field_access = $items->access('view', NULL, TRUE);
        // The language of the field values to display is already determined
        // in the incoming $entity. The formatter should build its output of
        // those values using:
        // - the entity language if the entity is translatable,
        // - the current "content language" otherwise.
        if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {
          $view_langcode = $entity->language()->getId();
        }
        else {
          $view_langcode = NULL;
        }
        $build_list[$id][$name] = $field_access->isAllowed() ? $formatter->view($items, $view_langcode) : [];
        // Apply the field access cacheability metadata to the render array.
        $this->renderer->addCacheableDependency($build_list[$id][$name], $field_access);
      }
    }
  }

  foreach ($entities as $id => $entity) {
    // Assign the configured weights.
    foreach ($this->getComponents() as $name => $options) {
      if (isset($build_list[$id][$name])) {
        $build_list[$id][$name]['#weight'] = $options['weight'];
      }
    }

    // Let other modules alter the renderable array.
    $context = array(
      'entity' => $entity,
      'view_mode' => $this->originalMode,
      'display' => $this,
    );
    \Drupal::moduleHandler()->alter('entity_display_build', $build_list[$id], $context);
  }

  return $build_list;
}
doc_Drupal
2016-10-29 09:08:55
Comments
Leave a Comment

Please login to continue.