diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php index 691b59a..6f2f3ee 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php @@ -336,7 +336,6 @@ public function getTemplateSuggestions(EntityInterface $entity, $view_mode = 'de $entity_type_id . '__' . $bundle_name . '__' . $sanitized_view_mode, $entity_type_id . '__' . $entity_id, $entity_type_id . '__' . $entity_id . '__' . $sanitized_view_mode, - ); } diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php b/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php index c0bc4f5..d1a7717 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php @@ -36,7 +36,7 @@ public function buildComponents(array &$build, array $entities, array $displays, $view_mode, $langcode = NULL); /** - * Preprocesses variables. + * Returns a list of template suggestions from an entity and the view mode. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to get the template suggestions. diff --git a/core/modules/system/src/Tests/Entity/EntityTemplateSuggestionsTest.php b/core/modules/system/src/Tests/Entity/EntityTemplateSuggestionsTest.php new file mode 100644 index 0000000..d11ec25 --- /dev/null +++ b/core/modules/system/src/Tests/Entity/EntityTemplateSuggestionsTest.php @@ -0,0 +1,98 @@ +entity_name = $this->randomMachineName(); + $this->entity_user = $this->createUser(); + + // Pass in the value of the name field when creating. With the user + // field we test setting a field after creation. + $entity = entity_create($entity_type); + $entity->user_id->target_id = $this->entity_user->id(); + $entity->name->value = $this->entity_name; + + return $entity; + } + + /** + * Tests generation of template suggestions based on entity properties. + */ + public function testReadWrite() { + + // All entity variations have to have the same results. + foreach (entity_test_entity_types() as $entity_type) { + $this->assertTemplateSuggestions($entity_type); + } + } + + /** + * Executes the template suggestions test set for a defined entity type. + * + * @param string $entity_type + * The entity type to run the tests with. + */ + protected function assertTemplateSuggestions($entity_type) { + $entity = $this->createTestEntity($entity_type); + + $entity_definition = \Drupal::entityManager()->getDefinition($entity_type, FALSE); + + if ($entity_definition->hasViewBuilderClass()) { + $view_modes = \Drupal::entityManager()->getFormModes($entity_type); + + foreach ($view_modes as $id => $view_mode) { + + if ($view_builder = \Drupal::entityManager()->getViewBuilder($entity_type)) { + $entity_id = rand(1, 20); + $sanitized_view_mode = strtr($id, '.', '_'); + $bundle_name = $entity->bundle(); + $entity->{$entity_definition->getKey('id')} = $entity_id; + + $suggestions = $view_builder->getTemplateSuggestions($entity, $id); + + $expected = array( + $entity_type . '__' . $sanitized_view_mode, + $entity_type . '__' . $bundle_name, + $entity_type . '__' . $bundle_name . '__' . $sanitized_view_mode, + $entity_type . '__' . $entity_id, + $entity_type . '__' . $entity_id . '__' . $sanitized_view_mode, + ); + + $this->assertEqual($expected, $suggestions, json_encode($suggestions)); + } + } + } + } + +}