Commit 73e83adb authored by webchick's avatar webchick

Issue #2507967 by tim.plunkett:...

Issue #2507967 by tim.plunkett: \Drupal\Core\Entity\Controller\EntityViewController::buildTitle() assumes the  is in the render array
parent c66eb738
......@@ -105,6 +105,7 @@ public function view(EntityInterface $_entity, $view_mode = 'full', $langcode =
$page['#pre_render'][] = [$this, 'buildTitle'];
$page['#entity_type'] = $_entity->getEntityTypeId();
$page['#' . $page['#entity_type']] = $_entity;
return $page;
......
......@@ -34,9 +34,7 @@ protected function setUp() {
parent::setUp();
// Create some dummy entity_test entities.
for ($i = 0; $i < 2; $i++) {
$random_label = $this->randomMachineName();
$data = array('bundle' => 'entity_test', 'name' => $random_label);
$entity_test = $this->container->get('entity.manager')->getStorage('entity_test')->create($data);
$entity_test = $this->createTestEntity('entity_test');
$entity_test->save();
$this->entities[] = $entity_test;
}
......@@ -116,4 +114,31 @@ public function testFieldItemAttributes() {
$this->assertTrue($xpath, 'The field item attributes from both modules have been found in the rendered output of the field.');
}
/**
* Tests that a view builder can successfully override the view builder.
*/
public function testEntityViewControllerViewBuilder() {
$entity_test = $this->createTestEntity('entity_test_view_builder');
$entity_test->save();
$this->drupalGet('entity_test_view_builder/' . $entity_test->id());
$this->assertText($entity_test->label());
}
/**
* Creates an entity for testing.
*
* @param string $entity_type
* The entity type.
*
* @return \Drupal\Core\Entity\EntityInterface
* The created entity.
*/
protected function createTestEntity($entity_type) {
$data = array(
'bundle' => $entity_type,
'name' => $this->randomMachineName(),
);
return $this->container->get('entity.manager')->getStorage($entity_type)->create($data);
}
}
......@@ -76,5 +76,12 @@ entity.user_role.test_operation:
requirements:
_access: 'TRUE'
entity.entity_test_view_builder.canonical:
path: '/entity_test_view_builder/{entity_test_view_builder}'
defaults:
_entity_view: 'entity_test_view_builder'
requirements:
_access: 'TRUE'
route_callbacks:
- '\Drupal\entity_test\Routing\EntityTestRoutes::routes'
<?php
/**
* @file
* Contains \Drupal\entity_test\Entity\EntityTestViewBuilder.
*/
namespace Drupal\entity_test\Entity;
/**
* Test entity class for testing a view builder.
*
* @ContentEntityType(
* id = "entity_test_view_builder",
* label = @Translation("Entity Test view builder"),
* handlers = {
* "access" = "Drupal\entity_test\EntityTestAccessControlHandler",
* "view_builder" = "Drupal\entity_test\EntityTestViewBuilderOverriddenView",
* },
* base_table = "entity_test",
* render_cache = FALSE,
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "bundle" = "type"
* }
* )
*/
class EntityTestViewBuilder extends EntityTest {
}
<?php
/**
* @file
* Contains \Drupal\entity_test\EntityTestViewBuilderOverriddenView.
*/
namespace Drupal\entity_test;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a view builder that overrides ::view() and ::viewMultiple().
*/
class EntityTestViewBuilderOverriddenView extends EntityTestViewBuilder {
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
$build = [];
$build[$entity->id()]['#markup'] = SafeMarkup::checkPlain($entity->label());
return $build;
}
/**
* {@inheritdoc}
*/
public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
$build = [];
foreach ($entities as $key => $entity) {
$build[$key] = $this->view($entity, $view_mode, $langcode);
}
return $build;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment