Problem/Motivation
Normally, when creating block plugins, one can return an #attributes
from the build()
method. If that happens, the attributes are being added to the block's wrapper.
It looks like this logic is located here: https://git.drupalcode.org/project/drupal/blob/8.8.x/core/modules/block/...
It doesn't work if the block has been put inside the layout, through the layout_builder. I consider it a bug, as anyone would expect the same behavior here, and it will surely break some custom plugin blocks relying on this feature.
Proposed resolution
Apply the same logic inside the BlockComponentRenderArray
as it is in the BlockViewBuilder
, to make #attributes
work with the layout_builder.
Remaining tasks
Review the patch.
API changes
If anyone used #attributes
to put attributes in the content of their block, instead of the block's wrapper, this change will probably break something for him. The weight of this issue is reduced by the fact, that this person would have to create this block plugin only to work in the layout builder, as outside of it, he would quickly notice that it doesn't work as expected.
Comment | File | Size | Author |
---|---|---|---|
#34 | 3077734-34.patch | 2.67 KB | nginex |
|
Issue fork drupal-3077734
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
Luke_Nuke CreditAttribution: Luke_Nuke commentedI attached two patches. One with test, testing the buggy scenario, which should fail. And the fix, also containing the same test, but it should pass it.
Comment #4
Luke_Nuke CreditAttribution: Luke_Nuke commentedImproved patch. I added the same checks that can be find in BlockViewBuilder. BTW There is something very wrong, that this logic is basically copied from another class. Some DRY issue is here.
Comment #5
tim.plunkettAgreed with the concerns of #4
Comment #7
Petr IllekI've recreated/rerolled the patch against 8.9.x as it wasn't working for 8.8.x-rc1.
Comment #9
kybermanI tested the latest patch and it solves my problem. It would be great to have that merged.
We are facing missing attributes placed by entity_class_formatter module implementation (using hook_entity_view_alter() to set attributes), when custom blocks are inserted into Layout Builder. As a workaround, I created a patch to transfer missing attributes, but it's not really a general solution: https://www.drupal.org/project/entity_class_formatter/issues/3108054
Comment #10
hash6 CreditAttribution: hash6 at QED42 for Drupal India Association commentedComment #11
kybermanPatch inside comment #7 failed only because the test block plugin was in the wrong folder.
Here is a fixed patch against 8.9.x.
Comment #12
tim.plunkettThanks for the patch!
The result of BlockPluginInterface::build() is always an array, and isEmpty() is already called above, this whole line can be
This comment needs to be rewrapped. References to specific contrib modules should be removed. Mixing styles like
"is the"
and*entire*
is odd, neither are found in core AFAIK.Why the foreach loop for a single property? Is this expected to be expanded later?
Shorten to 80 char. Remove everything after the , and clarify that it is testing a block plugin?
Comment #13
kybermanThank you Tim for your guidance! Firstly it was only a quick fix.
I'm also thinking about merging the existing values there (if available), rather than destroying them. What do you think?
Comment #15
tim.plunkettPlease include an interdiff when making changes to an existing patch, to help others review your changes.
This issue seems similar to #3135899: Layout Builder does not seem to work with Entity Class Formatter and #3072231: Custom blocks break layout builder module - Quick Edit could not associate the rendered entity field markup, perhaps they can be combined?
Comment #16
Grayle CreditAttribution: Grayle commentedJust a combo patch of this issue and this one: https://www.drupal.org/project/drupal/issues/3020876
Both change the same lines, so uploaded combo patch here for composer. Please ignore, has no bearing on the issue at hand. Just need it now.
Comment #17
kkalashnikov CreditAttribution: kkalashnikov at Srijan | A Material+ Company for Drupal India Association commentedPatch #13 is successfully applied for Drupal 9.1.
@kyberman, It would be helpful for reviewing if you will update interdiff.
Comment #19
mohit_aghera CreditAttribution: mohit_aghera at QED42 commentedCombining both the patches here in case someone needs it.
https://www.drupal.org/project/drupal/issues/3020876#comment-14051592
I think we can take forward the other issue and may be close to this one.
Comment #20
volkswagenchickAdding
NorthAmerica2021
andEasy Out of the Box
tags for visibility.DrupalCon NA is April 12-16 with a focus on EOOTB on Wednesday, April 14.
Thanks
Comment #21
Suresh Prabhu Parkala CreditAttribution: Suresh Prabhu Parkala at Specbee commentedJust a re-roll against the latest 9.2.x.
Comment #24
brentgQuickly rerolled the patch for 9.3, tests probably needs adapting since a lot on this has changed in the other issue
Comment #27
BramDriesenThe merge request applies cleanly on 9.3.0. Not sure what is needed to get this to RTBC.
Comment #28
mohit_aghera CreditAttribution: mohit_aghera commentedComment #29
tim.plunkettOh, this is confusing. The MR is missing some of the code from the patches. Please update the MR to contain everything needed, and stop posting new patches. Thanks!
Comment #32
smustgrave CreditAttribution: smustgrave at Mobomo commentedAs mentioned in #29 the MR seems to be missing some files. And there is an open thread on the 1 file added.
Comment #34
nginex CreditAttribution: nginex as a volunteer and at Dropsolid for Dropsolid commentedI've removed duplciate for this line below.
Besides, the tests from #13 are already in drupal core 9.3.x, so that's why you don't see new files in the MR.
I've also created a static patch for those who does not like when patch from MR does not apply anymore due to new commits.
Comment #35
smustgrave CreditAttribution: smustgrave at Mobomo commentedSo if the tests were already added and they didn't catch this bug then I think the tests may not have been covering this bug. So we will need a failing test case to show this is still an issue I believe.
Comment #36
nginex CreditAttribution: nginex as a volunteer and at Dropsolid for Dropsolid commented@smustgrave, you can check #3077734-2: Plugin blocks cannot set their own attributes when put in the layout comment, there are two patches, first patch contains only tests without the fix and it failed, second patch contains the fix + tests and it successed.
Would you expect to have one more test scenario that will fail, something like this?
So if the fix is not working then test will be passed.
I'm not sure what else we can add here
Comment #37
BramDriesenI think #2 was just overlooked. Setting it back to NR for that matter.
Comment #38
smustgrave CreditAttribution: smustgrave at Mobomo commentedThe test-only patch from #2
One file is already in drupal core
The other is not but also contains deprecated function calls.
The MR and patch #34 do not contain any tests. Would expect a full patch to contain the tests also.
Comment #39
nginex CreditAttribution: nginex as a volunteer and at Dropsolid for Dropsolid commented@smustgrave, you can find that the tests exist and already commited in 9.2.x, 9.3.x almost 2 years ago.
#3020876-68: Contextual links of reusable content blocks are not displayed when rendering entities built via Layout Builder,
that's why you can't see tests in the MR.
Comment #40
smustgrave CreditAttribution: smustgrave at Mobomo commentedThen the tests weren't covering this bug or they would be failing on without this fix. So either this is a non issue now or we need new tests.
Comment #41
nginex CreditAttribution: nginex as a volunteer and at Dropsolid for Dropsolid commentedIndeed, it's very confusing issue due to mix from #3020876: Contextual links of reusable content blocks are not displayed when rendering entities built via Layout Builder.
See #3020876-29: Contextual links of reusable content blocks are not displayed when rendering entities built via Layout Builder, now I'm surprised that this issue even exists, seems like it should be fixed in #3020876.
Thoughts?
Comment #42
smustgrave CreditAttribution: smustgrave at Mobomo commentedCan you see if the fix from that one solves the problem here? Could be the same issue and just 2 different approaches being taken.
Comment #43
smustgrave CreditAttribution: smustgrave at Mobomo commentedPosted in the #bugsmash channel on slack too
Comment #44
nginex CreditAttribution: nginex as a volunteer and at Dropsolid for Dropsolid commentedIt's indeed seems like a duplicate of #3020876: Contextual links of reusable content blocks are not displayed when rendering entities built via Layout Builder, the only difference is that here we have an extra condition
Honestly, that extra condition does not affect on functionality, at least the tests that cover block attributes change from #3020876: Contextual links of reusable content blocks are not displayed when rendering entities built via Layout Builder are passed.
I would closed this as duplicate, but I need one more confirmation of this theory
Comment #45
smustgrave CreditAttribution: smustgrave at Mobomo commentedLooking at this again I do think it's a duplicate. Closing this out for the other and will move over credit.
Thanks!