diff --git a/core/lib/Drupal/Core/Entity/Controller/EntityViewController.php b/core/lib/Drupal/Core/Entity/Controller/EntityViewController.php index 672eed7..b9667d2 100644 --- a/core/lib/Drupal/Core/Entity/Controller/EntityViewController.php +++ b/core/lib/Drupal/Core/Entity/Controller/EntityViewController.php @@ -51,7 +51,8 @@ public static function create(ContainerInterface $container) { * than $entity to prevent collisions with other named placeholders in the * route. * @param string $view_mode - * The view mode that should be used to display the entity. + * (optional) The view mode that should be used to display the entity. + * Defaults to 'full'. * @param string $langcode * (optional) For which language the entity should be rendered, defaults to * the current content language. @@ -59,7 +60,7 @@ public static function create(ContainerInterface $container) { * @return array * A render array as expected by drupal_render(). */ - public function view(EntityInterface $_entity, $view_mode, $langcode = NULL) { + public function view(EntityInterface $_entity, $view_mode = 'full', $langcode = NULL) { return $this->entityManager ->getRenderController($_entity->entityType()) ->view($_entity, $view_mode, $langcode); diff --git a/core/lib/Drupal/Core/Entity/Enhancer/EntityRouteEnhancer.php b/core/lib/Drupal/Core/Entity/Enhancer/EntityRouteEnhancer.php index 9bc8ee4..2cf8d47 100644 --- a/core/lib/Drupal/Core/Entity/Enhancer/EntityRouteEnhancer.php +++ b/core/lib/Drupal/Core/Entity/Enhancer/EntityRouteEnhancer.php @@ -51,7 +51,16 @@ public function enhance(array $defaults, Request $request) { elseif (!empty($defaults['_entity_view'])) { $defaults['_controller'] = 'controller.page:content'; $defaults['_content'] = '\Drupal\Core\Entity\Controller\EntityViewController::view'; - list($entity_type, $view_mode) = explode('.', $defaults['_entity_view']); + if (strpos($defaults['_entity_view'], '.') !== FALSE) { + // The _entity_view entry is of the form entity_type.view_mode. + list($entity_type, $view_mode) = explode('.', $defaults['_entity_view']); + $defaults['view_mode'] = $view_mode; + } + else { + // Only the entity type is nominated, the view mode will use the + // default. + $entity_type = $defaults['_entity_view']; + } // Set by reference so that we get the upcast value. if (!empty($defaults[$entity_type])) { $defaults['_entity'] = &$defaults[$entity_type]; @@ -85,7 +94,6 @@ public function enhance(array $defaults, Request $request) { throw new \RuntimeException(sprintf('Failed to find entity of type %s in route named %s', $entity_type, $defaults[RouteObjectInterface::ROUTE_NAME])); } } - $defaults['view_mode'] = $view_mode; unset($defaults['_entity_view']); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityViewControllerTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityViewControllerTest.php index 8c885ac..502a206 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityViewControllerTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityViewControllerTest.php @@ -62,6 +62,10 @@ function testEntityViewController() { $this->drupalGet('entity-test-render-converter/' . $entity->id()); $this->assertRaw($entity->label()); $this->assertRaw('full'); + + $this->drupalGet('entity-test-render-no-view-mode/' . $entity->id()); + $this->assertRaw($entity->label()); + $this->assertRaw('full'); } } } diff --git a/core/modules/system/tests/modules/entity_test/entity_test.routing.yml b/core/modules/system/tests/modules/entity_test/entity_test.routing.yml index 08b18fa..645fae1 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.routing.yml +++ b/core/modules/system/tests/modules/entity_test/entity_test.routing.yml @@ -15,3 +15,10 @@ entity_test_render_options: _entity_view: 'entity_test_render.full' requirements: _access: 'TRUE' + +entity_test_render_no_view_mode: + pattern: '/entity-test-render-no-view-mode/{entity_test_render}' + defaults: + _entity_view: 'entity_test_render' + requirements: + _access: 'TRUE' diff --git a/core/tests/Drupal/Tests/Core/Entity/Enhancer/EntityRouteEnhancerTest.php b/core/tests/Drupal/Tests/Core/Entity/Enhancer/EntityRouteEnhancerTest.php index 00dd172..9376b0a 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Enhancer/EntityRouteEnhancerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Enhancer/EntityRouteEnhancerTest.php @@ -66,19 +66,19 @@ public function testEnhancer() { // Set _entity_view and ensure that the entity view controller is set. $defaults = array(); - $defaults['_entity_view'] = 'entity_test.default'; + $defaults['_entity_view'] = 'entity_test.full'; $defaults['entity_test'] = 'Mock entity'; $defaults = $route_enhancer->enhance($defaults, $request); $this->assertEquals('controller.page:content', $defaults['_controller']); $this->assertEquals('\Drupal\Core\Entity\Controller\EntityViewController::view', $defaults['_content'], 'The entity view controller was not set.'); $this->assertEquals($defaults['_entity'], 'Mock entity'); - $this->assertEquals($defaults['view_mode'], 'default'); + $this->assertEquals($defaults['view_mode'], 'full'); $this->assertFalse(isset($defaults['_entity_view'])); // Set _entity_view and ensure that the entity view controller is set using // a converter. $defaults = array(); - $defaults['_entity_view'] = 'entity_test.default'; + $defaults['_entity_view'] = 'entity_test.full'; $defaults['foo'] = 'Mock entity'; // Add a converter. $options['parameters']['foo'] = array('type' => 'entity:entity_test'); @@ -96,7 +96,18 @@ public function testEnhancer() { $this->assertEquals('controller.page:content', $defaults['_controller']); $this->assertEquals('\Drupal\Core\Entity\Controller\EntityViewController::view', $defaults['_content'], 'The entity view controller was not set.'); $this->assertEquals($defaults['_entity'], 'Mock entity'); - $this->assertEquals($defaults['view_mode'], 'default'); + $this->assertEquals($defaults['view_mode'], 'full'); + $this->assertFalse(isset($defaults['_entity_view'])); + + // Set _entity_view without a view mode. + $defaults = array(); + $defaults['_entity_view'] = 'entity_test'; + $defaults['entity_test'] = 'Mock entity'; + $defaults = $route_enhancer->enhance($defaults, $request); + $this->assertEquals('controller.page:content', $defaults['_controller']); + $this->assertEquals('\Drupal\Core\Entity\Controller\EntityViewController::view', $defaults['_content'], 'The entity view controller was not set.'); + $this->assertEquals($defaults['_entity'], 'Mock entity'); + $this->assertEmpty($defaults['view_mode']); $this->assertFalse(isset($defaults['_entity_view'])); }