diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php index c9a92b4..23ae3b3 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php @@ -227,7 +227,9 @@ public function buildMultiple(array $entities) { // Run field formatters. foreach ($this->getComponents() as $name => $options) { if ($formatter = $this->getRenderer($name)) { - $this->formatFieldMultiple($build_list, $entities, $formatter, $name); + foreach ($this->formatFieldMultiple($entities, $name, $formatter) as $key => $build) { + $build_list[$key][$name] = $build; + }; } } @@ -254,27 +256,23 @@ public function buildMultiple(array $entities) { /** * Formats a field for multiple entities. * - * @param array &$build_list - * A reference to a renderable array, indexed by the same keys as the - * $entities array parameter. * @param \Drupal\Core\Entity\FieldableEntityInterface[] $entities * The entities being displayed. - * @param \Drupal\Core\Field\FormatterInterface $formatter - * The field formatter to be used. * @param string $field_name * The name of the field to be formatted. - * @param string $field_key - * (optional) The key every field render array will be keyed with in the - * build list. Defaults to the field name. + * @param \Drupal\Core\Field\FormatterInterface $formatter + * The field formatter to be used. + * + * @return array + * A renderable array, indexed by the same keys as the $entities array + * parameter. */ - protected function formatFieldMultiple(array &$build_list, array $entities, FormatterInterface $formatter, $field_name, $field_key = NULL) { - if (!isset($field_key)) { - $field_key = $field_name; - } + protected function formatFieldMultiple(array $entities, $field_name, FormatterInterface $formatter) { + $build_list = []; // Group items across all entities and pass them to the formatter's // prepareView() method. - $grouped_items = array(); + $grouped_items = []; foreach ($entities as $index => $entity) { $items = $entity->get($field_name); $items->filterEmptyItems(); @@ -282,21 +280,17 @@ protected function formatFieldMultiple(array &$build_list, array $entities, Form } $formatter->prepareView($grouped_items); + // Then let the formatter build the output for each entity. foreach ($entities as $index => $entity) { /** @var \Drupal\Core\Field\FieldItemListInterface $items */ $items = $grouped_items[$index]; - // If a field key is explicitly not specified, it means we do not need the - // additional nesting level. - if ($field_key) { - $build = &$build_list[$index][$field_key]; - } - else { - $build = &$build_list[$index]; - } - // Then let the formatter build the output for each entity. + // @todo Will need to be adjusted after https://www.drupal.org/node/2099137 $build = $formatter->view($items); $build['#access'] = $items->access('view'); + $build_list[$index] = $build; } + + return $build_list; } /** @@ -311,8 +305,6 @@ public function buildField(FieldableEntityInterface $entity, FieldDefinitionInte * {@inheritdoc} */ public function buildFieldMultiple(array $entities, FieldDefinitionInterface $field_definition, $type, $settings) { - $build_list = []; - $options = [ 'field_definition' => $field_definition, 'configuration' => array( @@ -323,13 +315,12 @@ public function buildFieldMultiple(array $entities, FieldDefinitionInterface $fi ), 'view_mode' => static::CUSTOM_MODE, ]; - /** @var \Drupal\Core\Field\FormatterInterface $formatter */ if ($formatter = $this->pluginManager->getInstance($options)) { - $this->formatFieldMultiple($build_list, $entities, $formatter, $field_definition->getName(), FALSE); + return $this->formatFieldMultiple($entities, $field_definition->getName(), $formatter); } - return $build_list; + return []; } /**