diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php index d73a0a6..1240cba 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php @@ -170,10 +170,8 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la if (!isset($langcode)) { $langcode = $this->languageManager->getCurrentLanguage(Language::TYPE_CONTENT)->id; } - - // Allow modules to change the view mode. + // Context is used in the hook_entity_view_mode_alter() invocation below. $context = array('langcode' => $langcode); - $this->moduleHandler->alter('entity_view_mode', $view_mode, $entity, $context); $build = array( '#sorted' => TRUE, @@ -186,6 +184,9 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la // object. $entity = $this->entityManager->getTranslationFromContext($entity, $langcode); + // Allow modules to change the view mode. + $this->moduleHandler->alter('entity_view_mode', $view_mode, $entity, $context); + // Set render defaults. $build[$key] = $this->getBuildDefaults($entity, $view_mode, $langcode); $entityType = $this->entityTypeId; @@ -252,6 +253,7 @@ public function buildEntityMultiple(array $build) { $view_modes = array(); $langcode = $build['#langcode']; $entity_type_key = "#{$this->entityTypeId}"; + $view_hook = "{$this->entityTypeId}_view"; // Find the keys for the ContentEntities in the build; Store entities for // rendering by view_mode. @@ -284,25 +286,24 @@ public function buildEntityMultiple(array $build) { // Allow for alterations while building, before rendering. $entity = $build[$key][$entity_type_key]; $display = $displays[$entity->bundle()]; - $view_hook = "{$this->entityTypeId}_view"; - $this->moduleHandler()->invokeAll($view_hook, array(&$build, $entity, $display, $view_mode, $langcode)); - $this->moduleHandler()->invokeAll('entity_view', array(&$build, $entity, $display, $view_mode, $langcode)); + $this->moduleHandler()->invokeAll($view_hook, array(&$build[$key], $entity, $display, $view_mode, $langcode)); + $this->moduleHandler()->invokeAll('entity_view', array(&$build[$key], $entity, $display, $view_mode, $langcode)); - $this->alterBuild($build, $entity, $display, $view_mode, $langcode); + $this->alterBuild($build[$key], $entity, $display, $view_mode, $langcode); // Assign the weights configured in the display. // @todo: Once https://drupal.org/node/1875974 provides the missing API, // only do it for 'extra fields', since other components have been // taken care of in EntityViewDisplay::buildMultiple(). foreach ($display->getComponents() as $name => $options) { - if (isset($build[$name])) { - $build['#weight'] = $options['weight']; + if (isset($build[$key][$name])) { + $build[$key]['#weight'] = $options['weight']; } } // Allow modules to modify the render array. - \Drupal::moduleHandler()->alter(array($view_hook, 'entity_view'), $build, $entity, $display); + \Drupal::moduleHandler()->alter(array($view_hook, 'entity_view'), $build[$key], $entity, $display); } } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php index b3f61a8..c20038f 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php @@ -8,6 +8,7 @@ namespace Drupal\entity_reference\Plugin\Field\FieldFormatter; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Component\Utility\String; /** * Plugin implementation of the 'entity reference ID' formatter. @@ -35,8 +36,13 @@ public function viewElements(FieldItemListInterface $items) { continue; } if (!empty($item->entity) && !empty($item->target_id)) { + /** @var $referenced_entity \Drupal\Core\Entity\EntityInterface */ + $referenced_entity = $item->entity; $elements[$delta] = array( - '#markup' => check_plain($item->target_id), + '#markup' => String::checkPlain($item->target_id), + // Create a cache tag entry for the referenced entity. In the case + // that the referenced entity is deleted, the cache for referring + // entities must be cleared. '#cache' => array( 'tags' => array( $referenced_entity->getEntityTypeID() => $referenced_entity->id(), diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFormatterTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFormatterTest.php index 44bcca1..00140cc 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFormatterTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFormatterTest.php @@ -92,4 +92,31 @@ public function testAccess() { $this->assertEqual($entity_2->{$field_name}->value, $entity_1->id(), format_string('The un-accessible item still exists after @name formatter was executed.', array('@name' => $name))); } } + + /** + * Tests the ID formatter. + */ + public function testIdFormatter() { + $field_name = $this->fieldName; + $formatter = 'entity_reference_entity_id'; + + // Create the entity to be referenced. + $entity_1 = entity_create($this->entityType, array('name' => $this->randomName())); + $entity_1->save(); + + // Create the entity that will have the entity reference field. + $entity_2 = entity_create($this->entityType, array('name' => $this->randomName())); + $entity_2->save(); + $entity_2->{$field_name}->entity = $entity_1; + $entity_2->{$field_name}->access = TRUE; + + $items = $entity_2->get($field_name); + + // Build the renderable array for the entity reference field. + $build = $items->view(array('type' => $formatter)); + + $this->assertEqual($build[0]['#markup'], $entity_1->id(), format_string('The markup returned by the @formatter formatter is correct.', array('@formatter' => $formatter))); + $this->assertEqual($build[0]['#cache']['tags'][$this->entityType], $entity_1->id(), format_string('The @formatter formatter assigned the correct cache tag entity ID.', array('@formatter' => $formatter))); + $this->assertTrue($build[0]['#cache']['tags'][$this->entityType . '_view'], format_string('The @formatter formatter assigned the correct value for the entity type view cache tag.', array('@formatter' => $formatter))); + } }