From 30472b7b1cb8ee8b5d5c645c4d5c1febebe41678 Mon Sep 17 00:00:00 2001 From: aspilicious Date: Wed, 29 Jul 2020 15:26:06 +0000 Subject: [PATCH] Issue #3068078 by aspilicious: Block attributes and contextual links are removed --- src/View/BlockViewBuilder.php | 14 ++++++++++++++ tests/src/Kernel/BlockViewBuilderTest.php | 9 ++++++++- .../twig_tweak_test/src/Plugin/Block/FooBlock.php | 3 +++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/View/BlockViewBuilder.php b/src/View/BlockViewBuilder.php index ef54db7..a612200 100644 --- a/src/View/BlockViewBuilder.php +++ b/src/View/BlockViewBuilder.php @@ -126,6 +126,10 @@ class BlockViewBuilder { $block_plugin->setTitle($title); } + // Place the content returned by the block plugin into a 'content' child + // element, as a way to allow the plugin to have complete control of its + // properties and rendering (for instance, its own #theme) without + // conflicting with the properties used above. $build['content'] = $block_plugin->build(); if ($block_plugin instanceof TitleBlockPluginInterface) { @@ -143,6 +147,16 @@ class BlockViewBuilder { '#base_plugin_id' => $block_plugin->getBaseId(), '#derivative_plugin_id' => $block_plugin->getDerivativeId(), ]; + // Semantically, the content returned by the plugin is the block, and in + // particular, #attributes and #contextual_links is information about + // the *entire* block. Therefore, we must move these properties into the + // top-level element. + foreach (['#attributes', '#contextual_links'] as $property) { + if (isset($build['content'][$property])) { + $build[$property] = $build['content'][$property]; + unset($build['content'][$property]); + } + } } } diff --git a/tests/src/Kernel/BlockViewBuilderTest.php b/tests/src/Kernel/BlockViewBuilderTest.php index 4ed03d2..8f9ad50 100644 --- a/tests/src/Kernel/BlockViewBuilderTest.php +++ b/tests/src/Kernel/BlockViewBuilderTest.php @@ -47,7 +47,9 @@ final class BlockViewBuilderTest extends KernelTestBase { ], '#theme' => 'block', '#id' => 'twig_tweak_test_foo', - '#attributes' => [], + '#attributes' => [ + 'id' => 'foo', + ], '#contextual_links' => [], '#configuration' => [ 'id' => 'twig_tweak_test_foo', @@ -100,6 +102,11 @@ final class BlockViewBuilderTest extends KernelTestBase { $expected_build = [ 'content' => [ '#markup' => 'Foo', + // Since the block is built without wrapper #attributes must remain in + // 'content' element. + '#attributes' => [ + 'id' => 'foo', + ], '#cache' => [ 'contexts' => ['url'], 'tags' => ['tag_from_build'], diff --git a/tests/twig_tweak_test/src/Plugin/Block/FooBlock.php b/tests/twig_tweak_test/src/Plugin/Block/FooBlock.php index 46b1e04..9bbba53 100644 --- a/tests/twig_tweak_test/src/Plugin/Block/FooBlock.php +++ b/tests/twig_tweak_test/src/Plugin/Block/FooBlock.php @@ -41,6 +41,9 @@ final class FooBlock extends BlockBase { public function build(): array { return [ '#markup' => $this->getConfiguration()['content'], + '#attributes' => [ + 'id' => 'foo', + ], '#cache' => [ 'contexts' => ['url'], 'tags' => ['tag_from_' . __FUNCTION__],