diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php index ec278b8..dd20d1d 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php @@ -16,6 +16,7 @@ use Drupal\Core\TypedData\TranslatableInterface; use Drupal\entity\Entity\EntityViewDisplay; use Drupal\Core\Render\Element; +use Drupal\Component\Utility\NestedArray; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -125,9 +126,7 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang foreach ($entities_by_bundle as $bundle => $bundle_entities) { $build = $displays[$bundle]->buildMultiple($bundle_entities); foreach ($bundle_entities as $id => $entity) { - $entity->content += array( - '#view_mode' => $view_mode, - ) + $build[$id]; + $entity->content = NestedArray::mergeDeepArray(array($entity->content, array('#view_mode' => $view_mode,), $build[$id],), TRUE); } } } diff --git a/core/lib/Drupal/Core/Field/FormatterBase.php b/core/lib/Drupal/Core/Field/FormatterBase.php index d75279f..abbc055 100644 --- a/core/lib/Drupal/Core/Field/FormatterBase.php +++ b/core/lib/Drupal/Core/Field/FormatterBase.php @@ -65,17 +65,24 @@ public function __construct($plugin_id, array $plugin_definition, FieldDefinitio $this->viewMode = $view_mode; } + public function getDefaults(FieldItemListInterface $items) { $info = array(); - // Gather cache tags from reference fields. - foreach ($items as $item) { - if (isset($item->format)) { - $info['#cache']['tags']['filter_format'] = $item->format; - } - - if (isset($item->entity)) { - $info['#cache']['tags'][$item->entity->getEntityTypeId()][] = $item->entity->id(); - $info['#cache']['tags'][$item->entity->getEntityTypeId() . '_view'] = TRUE; + $elements = $this->viewElements($items); + if ($elements) { + $info = array( + '#cache' => array('tags' => array()), + ); + // Gather cache tags from reference fields. + foreach ($items as $item) { + if (isset($item->format)) { + $info['#cache']['tags']['filter_format'] = $item->format; + } + + if (isset($item->entity)) { + $info['#cache']['tags'][$item->entity->getEntityTypeId()][] = $item->entity->id(); + $info['#cache']['tags'][$item->entity->getEntityTypeId() . '_view'] = TRUE; + } } } return $info; @@ -107,7 +114,6 @@ public function view(FieldItemListInterface $items) { '#object' => $entity, '#items' => $items, '#formatter' => $this->getPluginId(), - '#cache' => array('tags' => array()) ); $addition[$field_name] = array_merge($info, $elements); diff --git a/core/modules/entity/lib/Drupal/entity/Entity/EntityViewDisplay.php b/core/modules/entity/lib/Drupal/entity/Entity/EntityViewDisplay.php index a35c4ac..a87d175 100644 --- a/core/modules/entity/lib/Drupal/entity/Entity/EntityViewDisplay.php +++ b/core/modules/entity/lib/Drupal/entity/Entity/EntityViewDisplay.php @@ -211,7 +211,7 @@ public function build(ContentEntityInterface $entity) { public function buildMultiple(array $entities) { $build = array(); foreach ($entities as $key => $entity) { - $build[$key] = array(); + $build[$key] = $entity->content; } // Run field formatters. @@ -230,7 +230,13 @@ public function buildMultiple(array $entities) { // Then let the formatter build the output for each entity. foreach ($entities as $key => $entity) { $items = $entity->get($field_name); - $build[$key] += $formatter->view($items); + if (!isset($build[$key][$field_name])) { + $build[$key][$field_name] = array(); + } + // @todo, maybe just return the contents of view without a wrapper and the subsequent need to reference + // a $field_name index? + $built_field = $formatter->view($items); + $build[$key][$field_name] += (!empty($built_field[$field_name])) ? $built_field[$field_name] : array(); } } } diff --git a/core/modules/node/lib/Drupal/node/Tests/SummaryLengthTest.php b/core/modules/node/lib/Drupal/node/Tests/SummaryLengthTest.php index 9ad3373..eb168f8 100644 --- a/core/modules/node/lib/Drupal/node/Tests/SummaryLengthTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/SummaryLengthTest.php @@ -35,9 +35,8 @@ function testSummaryLength() { $web_user = $this->drupalCreateUser(array('access content', 'administer content types')); $this->loggedInUser = $web_user; - $controller = $this->container->get('entity.manager')->getViewBuilder('node'); // Render the node as a teaser. - $content = $controller->view($node, 'teaser'); + $content = $this->drupalBuildView($node, 'teaser'); $this->assertTrue(strlen($content['body'][0]['#markup']) < 600, 'Teaser is less than 600 characters long.'); $this->drupalSetContent(drupal_render($content)); // The string 'What is a Drupalism?' is between the 200th and 600th @@ -55,7 +54,7 @@ function testSummaryLength() { // Render the node as a teaser again and check that the summary is now only // 200 characters in length and so does not include 'What is a Drupalism?'. - $content = $controller->view($node, 'teaser'); + $content = $this->drupalBuildView($node, 'teaser'); $this->assertTrue(strlen($content['body'][0]['#markup']) < 200, 'Teaser is less than 200 characters long.'); $this->drupalSetContent(drupal_render($content)); $this->assertText($node->label()); diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index d0a1044..1ae38db 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -336,7 +336,7 @@ protected function drupalCreateContentType(array $values = array()) { } protected function drupalBuildView($entity, $view_mode = 'full', $langcode = NULL, $reset = FALSE) { - $render_controller = \Drupal::entityManager()->getViewBuilder($entity->getEntityTypeId()); + $render_controller = $this->container->get('entity.manager')->getViewBuilder($entity->getEntityTypeId()); if ($reset) { $render_controller->resetCache(array($entity->id())); }