function EntityViewBuilderTest::testHookEntityTypeViewModeAlter
Same name in other branches
- 10 core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest::testHookEntityTypeViewModeAlter()
Tests a view mode alter on an entity.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityViewBuilderTest.php, line 316
Class
- EntityViewBuilderTest
- Tests the entity view builder.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testHookEntityTypeViewModeAlter() : void {
$entity_ids = [];
// Create some entities to test.
for ($i = 0; $i < 5; $i++) {
$entity = $this->createTestEntity('entity_test');
$entity->save();
$entity_ids[] = $entity->id();
}
/** @var \Drupal\entity_test\EntityTestViewBuilder $view_builder */
$view_builder = $this->container
->get('entity_type.manager')
->getViewBuilder('entity_test');
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test');
$storage->resetCache();
$entities = $storage->loadMultiple($entity_ids);
$build = $view_builder->viewMultiple($entities, 'entity_test.vm_alter_test');
foreach ($build as $key => $entity_build) {
if (!is_numeric($key)) {
continue;
}
$this->assertArrayHasKey('#view_mode', $entity_build);
$this->assertEquals('entity_test.vm_alter_full', $entity_build['#view_mode']);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.