Commit 7c0a466b authored by alexpott's avatar alexpott

Issue #2754783 by danmuzyka, jhedstrom, alexpott:...

Issue #2754783 by danmuzyka, jhedstrom, alexpott: hook_entity_display_build_alter() only affects final entity in $build_list array
parent f95fc93c
......@@ -277,7 +277,7 @@ public function buildMultiple(array $entities) {
'view_mode' => $this->originalMode,
'display' => $this,
);
\Drupal::moduleHandler()->alter('entity_display_build', $build_list[$key], $context);
\Drupal::moduleHandler()->alter('entity_display_build', $build_list[$id], $context);
}
return $build_list;
......
......@@ -681,6 +681,17 @@ function entity_test_entity_prepare_view($entity_type, array $entities, array $d
}
}
/**
* Implements hook_entity_display_build_alter().
*/
function entity_test_entity_display_build_alter(&$build, $context) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $context['entity'];
if ($entity->getEntityTypeId() == 'entity_test' && $entity->bundle() == 'display_build_alter_bundle') {
$build['entity_display_build_alter']['#markup'] = 'Content added in hook_entity_display_build_alter for entity id ' . $entity->id();
}
}
/**
* Implements hook_entity_access().
*/
......
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\entity_test\Entity\EntityTest;
/**
* Test view/render hooks for entities.
*
* @todo Add tests for the following hooks. https://www.drupal.org/node/2755353
* hook_entity_view_display_alter()
* hook_entity_prepare_view()
* hook_ENTITY_TYPE_view()
* hook_entity_view()
* hook_ENTITY_TYPE_view_alter()
* hook_entity_view_alter()
*
* @group Entity
*/
class EntityViewHookTest extends EntityKernelTestBase {
/**
* Test hook_entity_display_build_alter().
*/
public function testHookEntityDisplayBuildAlter() {
entity_test_create_bundle('display_build_alter_bundle');
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
$entity_ids = [];
// Create some entities to test.
for ($i = 0; $i < 5; $i++) {
$entity = EntityTest::create([
'name' => $this->randomMachineName(),
'type' => 'display_build_alter_bundle',
]);
$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);
$output = $renderer->renderRoot($build);
$this->setRawContent($output->__toString());
// Confirm that the content added in
// entity_test_entity_display_build_alter() appears multiple times, not
// just for the final entity.
foreach ($entity_ids as $id) {
$this->assertText('Content added in hook_entity_display_build_alter for entity id ' . $id);
}
}
}
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