Commit 408f00c3 authored by alexpott's avatar alexpott

Issue #2698909 by Berdir, andypost, jibran, tim.plunkett, lauriii, tstoeckler,...

Issue #2698909 by Berdir, andypost, jibran, tim.plunkett, lauriii, tstoeckler, dawehner: EntityViewBuilder uses non-existing #theme hooks
parent 892272e7
......@@ -9,6 +9,7 @@
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Theme\Registry;
use Drupal\Core\TypedData\TranslatableInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -54,6 +55,13 @@ class EntityViewBuilder extends EntityHandlerBase implements EntityHandlerInterf
*/
protected $languageManager;
/**
* The theme registry.
*
* @var \Drupal\Core\Theme\Registry
*/
protected $themeRegistry;
/**
* The EntityViewDisplay objects created for individual field rendering.
*
......@@ -72,12 +80,15 @@ class EntityViewBuilder extends EntityHandlerBase implements EntityHandlerInterf
* The entity manager service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Theme\Registry $theme_registry
* The theme registry.
*/
public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, Registry $theme_registry = NULL) {
$this->entityTypeId = $entity_type->id();
$this->entityType = $entity_type;
$this->entityManager = $entity_manager;
$this->languageManager = $language_manager;
$this->themeRegistry = $theme_registry ?: \Drupal::service('theme.registry');
}
/**
......@@ -87,7 +98,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
return new static(
$entity_type,
$container->get('entity.manager'),
$container->get('language_manager')
$container->get('language_manager'),
$container->get('theme.registry')
);
}
......@@ -148,7 +160,6 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
$this->moduleHandler()->alter('entity_view_mode', $view_mode, $entity, $context);
$build = array(
'#theme' => $this->entityTypeId,
"#{$this->entityTypeId}" => $entity,
'#view_mode' => $view_mode,
// Collect cache defaults for this entity.
......@@ -159,6 +170,11 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
),
);
// Add the default #theme key if a template exists for it.
if ($this->themeRegistry->getRuntime()->has($this->entityTypeId)) {
$build['#theme'] = $this->entityTypeId;
}
// Cache the rendered output if permitted by the view mode and global entity
// type configuration.
if ($this->isViewModeCacheable($view_mode) && !$entity->isNew() && $entity->isDefaultRevision() && $this->entityType->isRenderCacheable()) {
......
......@@ -2,7 +2,6 @@
namespace Drupal\entity_test;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder;
/**
......@@ -12,15 +11,6 @@
*/
class EntityTestViewBuilder extends EntityViewBuilder {
/**
* {@inheritdoc}
*/
protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
$build = parent::getBuildDefaults($entity, $view_mode);
unset($build['#theme']);
return $build;
}
/**
* {@inheritdoc}
*/
......
......@@ -2,6 +2,7 @@
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
use Drupal\Core\Cache\Cache;
......@@ -210,4 +211,23 @@ protected function createTestEntity($entity_type) {
return $this->container->get('entity.manager')->getStorage($entity_type)->create($data);
}
/**
* Tests that viewing an entity without template does not specify #theme.
*/
public function testNoTemplate() {
// Ensure that an entity type without explicit view builder uses the
// default.
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type = $entity_type_manager->getDefinition('entity_test_base_field_display');
$this->assertTrue($entity_type->hasViewBuilderClass());
$this->assertEquals(EntityViewBuilder::class, $entity_type->getViewBuilderClass());
// Ensure that an entity without matching template does not have a #theme
// key.
$entity = $this->createTestEntity('entity_test');
$build = $entity_type_manager->getViewBuilder('entity_test')->view($entity);
$this->assertEquals($entity, $build['#entity_test']);
$this->assertFalse(array_key_exists('#theme', $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