Problem/Motivation
When you edit a field block on Layout Builder, modify the Title, check Display title, and save the block the field block does not display the specified Title (configuration label) but instead displays the label / admin label.
This is due to the block.html.twig template currently using the label variable.
Proposed resolution
Although this could be handled via a template override or a field block specific template (e.g. block--field-block.html.twig), this could be more cleanly handled by using a hook_preprocess_block that would replace the label with the configuration label, when available.
Draft release note
Layout Builder field blocks will now display the user-specified label from the block configuration. Sites should review their existing blocks as this change may impact workflows that relied on the previous behavior.
| Comment | File | Size | Author |
|---|---|---|---|
| #46 | 3039185-interdiff-46.txt | 833 bytes | swentel |
| #46 | 3039185-46.patch | 3.38 KB | swentel |
| #44 | 3039185-interdiff-44.txt | 1.46 KB | swentel |
| #44 | 3039185-44.patch | 3.36 KB | swentel |
| #41 | field_blocks_title-3039185-41.patch | 2.68 KB | manishsaharan |
Issue fork drupal-3039185
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
nord102I have created a patch that adds a hook_preprocess_block that sets the label to the configuration label for field blocks specifically.
Comment #3
nord102Comment #4
tim.plunkettThanks for the patch!
This will need automated tests
Comment #5
nord102I have attached a test that fails and passes with the patch.
Comment #6
nord102Re-adding files, fixed the test patch to not use the docroot path
Comment #7
nord102Re-adding files again, fixing diff path
Comment #9
phernand42 commented#2 worked for me (still on version 8.6.15).
Comment #10
bkosborneI think the comments in the preprocess block could be improved. You have this:
How about this:
Also, this may be a breaking change at this point since LB is now stable. I think the impact is probably quite low, but it's possible people have previously configured their block titles to something different than field label, and now that alternate title will show.
Comment #11
yogeshmpawarComment #12
yogeshmpawarAgree with #10, so adding updated patch with an interdiff.
Comment #13
bkosborneI think it's missing a closing parenthesis
Comment #14
yogeshmpawarThat's right @bkosborne, Updated patch with an interdiff.
Comment #17
webdrips commented#14 seems to work with 8.8
Comment #18
dhirendra.mishra commentedLe me re-roll the patch.
Comment #19
dhirendra.mishra commentedComment #21
dhirendra.mishra commentedIt shows error: 1) Drupal\Tests\layout_builder\Functional\LayoutBuilderTest::testFieldBlockLabel
The link Add Block was not found on the page.
Failed asserting that an array has the key 0
can someone clarify this?
Comment #22
nkoporecI have fixed the broken tests, test it out manually and it works as expected.
Comment #24
danny englanderI tested the latest patch (#22) on Drupal 8.9 and it worked great. Before the patch, my custom title did not show on the page, only the default. After the patch, my custom title is rendered.
Comment #25
danny englanderComment #26
larowlanQueued a test-run on 9.1
Comment #27
jnettikI've also tested the patch in #22 and can confirm it's working as expected now.
Comment #28
studiozut commentedI've also tested patch #22 in Drupal 9.1 via dreditor/simplytest and it works as intended in multiple block tests.
Comment #29
studiozut commentedAssigning to myself to test patch #22
Comment #30
studiozut commentedTested patch #22 in Drupal 9.1 via dreditor/simplytest and it works as intended in multiple block tests.
Comment #31
tim.plunkettThis is not a bug, it's a feature request. Field UI does not allow you to change the text of the Field label. That textfield shown is the for Block title.
Label != Title.
Yes, that's confusing to have both.
But I think this patch makes them even more confusing by reusing one as the other. This would break the display of existing sites.
Comment #32
tim.plunkettAha, I partially misunderstood the issue, due to the label vs title in discussions.
Comment #33
tim.plunkettThe conditional doesn't match with what's in template_preprocess_block. It's not just fields that are the problem, but anything with a #title that happens to be in the exact magical spot.
Here's an alternate patch. Test is identical, just swapped the preprocess change for one to FieldBlock.
Comment #35
andrewsuth commentedI can confirm patch #33 works for me on 8.9.x as well.
Comment #36
andrewsuth commentedMoved this comment to correct issue.
Comment #38
extect commented#33 works for me as well on both D8 and D9. Thank you!
However, I don't get why the tests are failing.
Comment #39
manishsaharan commentedComment #40
manishsaharan commented#22 worked for me as well in drupal 8
Comment #41
manishsaharan commentedResolved the above custom commands failed issues in #33 Working fine with 9.2.x
Comment #44
swentel commentedThis should fix the remaining failures
Comment #46
swentel commentedFix of first fail, not sure yet about second as that one passes locally :)
Comment #48
webdrips commented#46 worked for me on 9.2.7
Comment #50
xaa commented#46 working on 9.3.6
Comment #51
msypes commentedAlso confirming that #46 works with 9.3.6
Comment #52
kaustubhb commentedConfirming that #46 works with 9.3.7
Comment #54
crutch commented#46 not working for 9.4.5
Comment #55
leisurman commented#46 is working for 9.3.19
Comment #58
aaronpinero commented#46 is working for me on Drupal 9.5.5
Comment #59
pcate commented#46 patch applied cleanly and fixed issue. Patch tests are passing on both 10.0.x and 10.1.x. I don't see any comments with outstanding issues so setting to RTBC.
Comment #60
fskreuz commentedJust adding some context since it's not the first time I've bumped into this issue and tried to recall how this worked:
2158003 added the "the exact magical spot" referred to in #33. It's a way for the rendered block content to override the configured title (e.g. views blocks). It appears that the intent is to have the implementing block place
#titleto override the block title (or at least it's a change to accommodate how Views did block title overrides). Patch fixes the issue by avoiding the field's#titleproperty from being in the magical spot by placing it in an array, nesting it a level down.2942623 exists, but might be closed. Although it brings up an interesting point of why the title override comes after the block's configured label. You would expect that block configuration overrides whatever the block content supplier (be it Views or Field Block) supplies.
Comment #62
lauriiiThis leads into behavior change on existing sites because they might be relying on this behavior. There could be also information in the administrative labels that is not intended to be printed on the public site. Tagging for release manager review to get their feedback on how they'd prefer to handle this.
Comment #63
catch@lauriii asked me about this one in slack.
The issue summary could use an update, the original 'proposed solution' was a bit weird, the new proposed solution just changes how the $build array is structured and seems fine.
On the behaviour change I think this is fine in a minor release with a release note. There seem to be two possibilities where it's a surprise:
1. Someone tried to customize the title, it didn't work, they forgot about it and just dealt with it (or changed the actual field title), suddenly it starts working. This could happen but seems like we just fixed their bug. Worst case they have to update the layout configuration.
2. Someone's relying on having this extra text field to store 'stuff' (metadata or similar) and suddenly their stuff that wasn't intended for public consumption is shown on the site. I think this is extremely unlikely because the UI for field blocks is exactly the same as all other kinds of blocks, and it's just this one that doesn't work.
Remaining question is whether this goes into 10.2.x or 10.1.x, I don't have a strong opinion on that.
Comment #64
catchForgot to untag.
Comment #66
lauriiiCommitted 7598b15 and pushed to 11.x. Thanks!
I don't necessarily see a lot of difference between committing this to 10.2.x and 10.1.x so I went ahead and cherry-picked this to 10.1.x.
Comment #68
neclimdulThis broke my site. No content on pages using field blocks.
A developer had written this:
That little changed test at the end of the patch, it was detecting this was going to break. :(
Its getting late on my Friday so I haven't dug in too deep into why the developer did this but wanted to raise this since the scope of the break might be bigger than captured.
Comment #69
catchLet's add a change record for this. It's tagged for release notes - I didn't check the release notes draft but I also can't see a snippet in the issue summary.
The render array change is allowed in a minor so it's a case where we'd expect custom code to adapt, but this went in to 10.1.x quite late so it's not leaving that much time. Did you already fix this for your site, and if so is it compatible with both the new and old way now? i.e. I wouldn't like to break this twice.
Comment #70
lauriiiI added a paragraph directly to the release notes about the behavior change, copied that here too to reduce confusion. The snipped I added didn't mention the render array change. Should we mention the render array change in both, CR and release notes?
Comment #71
catchI normally wouldn't bother (unless it was a very commonly altered form or similar) but I think since we have an example of someone relying on it, it would be a good idea in this case. Not sure how to word it except something like 'The render array structure for field blocks now has an extra level of nesting.' - although maybe that would be enough?
Comment #72
neclimdulYes and yes but I can watch this space and deal with the fix either way.
I think so. I actually went to the change records but couldn't find any recent layout builder changes which made narrowing down the problem, fix, and this issue a lot harder.
Yeah I think so.
The fix for me was a bit complicated because this seems to have been done to avoid the field wrapping divs. The trivial fix was just to output the individual items using the children filter but that added add the field wrappers which broke layout. To fully work around it afterwards actually required quite a bit of complexity or contrib. I went with contrib and
field_valuebeing provided by twig_field_value.Before:
After:
Comment #73
lauriiiI created a CR but I'm not sure what would be the before and after with just core because we don't have
|childrenfilter in core. Also, shouldn't it take into account that there could be something to render on the first level of the render array?Comment #74
neclimdulWow... that seems like an oversight on core's fault. That's super useful with render arrays.
I'm not sure how you'd do it either without implement Element::children inside the twig template which seems... not great.
That said, this can probably be closed since 10.1 is out and there's a changelog?
Comment #75
smustgrave commentedBrought this up with @lauriii in #needs-review-queue-initative and agreed it could be closed again. Well marked fixed to be auto closed later.
Comment #77
quietone commentedThis has a change record, removing tag