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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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 ; } |
Please login to continue.