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.
\Drupal\Core\Entity\FieldableEntityInterface[] $entities: The entities being displayed.
array A renderable array for the entities, indexed by the same keys as the $entities array parameter.
Overrides EntityViewDisplayInterface::buildMultiple
hook_entity_display_build_alter()
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[$key], $context); } return $build_list; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Entity!EntityViewDisplay.php/function/EntityViewDisplay::buildMultiple/8.1.x