Browse Source

Issue #3068078 by aspilicious: Block attributes and contextual links are removed

merge-requests/2/head
aspilicious 4 years ago committed by Chi
parent
commit
30472b7b1c
  1. 14
      src/View/BlockViewBuilder.php
  2. 9
      tests/src/Kernel/BlockViewBuilderTest.php
  3. 3
      tests/twig_tweak_test/src/Plugin/Block/FooBlock.php

14
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]);
}
}
}
}

9
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'],

3
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__],

Loading…
Cancel
Save