.../Drupal/custom_block/Tests/CustomBlockFieldTest.php | 7 ++++++- .../modules/custom_block_test/custom_block_test.module | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php index 57f1e99..63f1daa 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php @@ -20,7 +20,7 @@ class CustomBlockFieldTest extends CustomBlockTestBase { * * @var array */ - public static $modules = array('block', 'custom_block', 'link'); + public static $modules = array('block', 'custom_block', 'custom_block_test', 'link'); /** * The created field. @@ -102,6 +102,10 @@ public function testBlockFields() { $this->drupalPostForm(NULL, $edit, t('Save')); $block = entity_load('custom_block', 1); $url = 'admin/structure/block/add/custom_block:' . $block->uuid() . '/' . \Drupal::config('system.theme')->get('default'); + // Enable custom_block_test.module's hook_entity_view_alter(), which will + // set a data- attribute on the custom block entity. We want to ensure it + // shows up on the correct DOM node. + \Drupal::state()->set('custom_block_test_entity_view_alter_enabled', TRUE); // Place the block. $instance = array( 'id' => drupal_strtolower($edit['info']), @@ -113,6 +117,7 @@ public function testBlockFields() { $this->drupalGet(''); $this->assertLinkByHref('http://example.com'); $this->assertText('Example.com'); + $this->assertFieldByXPath('//div[@id="block-' . drupal_strtolower($edit['info']) . '" and @data-custom_block_test-entity-id="custom_block/1"]', NULL, 'data-custom_block_test-entity-id set on the correct DOM node.'); } } diff --git a/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module b/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module index 7186098..c10e795 100644 --- a/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module +++ b/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module @@ -9,6 +9,8 @@ */ use Drupal\custom_block\Entity\CustomBlock; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; /** * Implements hook_custom_block_view(). @@ -80,3 +82,16 @@ function custom_block_test_menu() { ); return $items; } + +/** + * Implements hook_entity_view_alter(). + */ +function custom_block_test_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) { + // Allow tests to enable or disable this alter hook. + if (!\Drupal::state()->get('custom_block_test_entity_view_alter_enabled', FALSE)) { + return; + } + + $build['#attributes']['data-custom_block_test-entity-id'] = $entity->entityType() . '/' . $entity->id(); +} +