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.

Issue fork drupal-3039185

Command icon 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

nord102 created an issue. See original summary.

nord102’s picture

I have created a patch that adds a hook_preprocess_block that sets the label to the configuration label for field blocks specifically.

nord102’s picture

Status: Active » Needs review
tim.plunkett’s picture

Version: 8.6.x-dev » 8.8.x-dev
Issue tags: +Blocks-Layouts, +Needs tests

Thanks for the patch!
This will need automated tests

nord102’s picture

I have attached a test that fails and passes with the patch.

nord102’s picture

Re-adding files, fixed the test patch to not use the docroot path

nord102’s picture

phernand42’s picture

#2 worked for me (still on version 8.6.15).

bkosborne’s picture

Status: Needs review » Needs work

I think the comments in the preprocess block could be improved. You have this:

/**
 * Implements hook_preprocess_block().
 */
function layout_builder_preprocess_block(&$variables) {
  // Only override the label with the configuration label for field blocks.
  if ($variables['elements']['#base_plugin_id'] === 'field_block') {
    if (!empty($variables['configuration']['label']) && !empty($variables['configuration']['label_display'])) {
      $variables['label'] = $variables['configuration']['label'];
    }
  }
}

How about this:

/**
 * Implements hook_preprocess_block().
 */
function layout_builder_preprocess_block(&$variables) {
  // Prevent field label from overriding the configured block title for field blocks.
  // See template_preprocess_block from block module for why this is necessary.
  if ($variables['elements']['#base_plugin_id'] === 'field_block' && !empty($variables['configuration']['label']) && !empty($variables['configuration']['label_display']) {
    $variables['label'] = $variables['configuration']['label'];
  }
}

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.

yogeshmpawar’s picture

Assigned: Unassigned » yogeshmpawar
yogeshmpawar’s picture

Assigned: yogeshmpawar » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.56 KB
new1.19 KB

Agree with #10, so adding updated patch with an interdiff.

bkosborne’s picture

Status: Needs review » Needs work

I think it's missing a closing parenthesis

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new2.56 KB
new933 bytes

That's right @bkosborne, Updated patch with an interdiff.

Status: Needs review » Needs work

The last submitted patch, 14: 3039185-14.patch, failed testing. View results

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

webdrips’s picture

#14 seems to work with 8.8

dhirendra.mishra’s picture

Assigned: Unassigned » dhirendra.mishra

Le me re-roll the patch.

dhirendra.mishra’s picture

Assigned: dhirendra.mishra » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.62 KB

Status: Needs review » Needs work

The last submitted patch, 19: 3039185-19.patch, failed testing. View results

dhirendra.mishra’s picture

It 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?

nkoporec’s picture

Status: Needs work » Needs review
StatusFileSize
new3.26 KB

I have fixed the broken tests, test it out manually and it works as expected.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

danny englander’s picture

StatusFileSize
new130.07 KB

I 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.

before and after

danny englander’s picture

Status: Needs review » Reviewed & tested by the community
larowlan’s picture

Queued a test-run on 9.1

jnettik’s picture

I've also tested the patch in #22 and can confirm it's working as expected now.

studiozut’s picture

I've also tested patch #22 in Drupal 9.1 via dreditor/simplytest and it works as intended in multiple block tests.

studiozut’s picture

Assigned: Unassigned » studiozut

Assigning to myself to test patch #22

studiozut’s picture

Assigned: studiozut » Unassigned

Tested patch #22 in Drupal 9.1 via dreditor/simplytest and it works as intended in multiple block tests.

tim.plunkett’s picture

Category: Bug report » Feature request
Status: Reviewed & tested by the community » Needs work

This 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.

tim.plunkett’s picture

Category: Feature request » Bug report
Status: Needs work » Needs review

Aha, I partially misunderstood the issue, due to the label vs title in discussions.

tim.plunkett’s picture

StatusFileSize
new2.68 KB
+++ b/core/modules/layout_builder/layout_builder.module
@@ -406,6 +406,18 @@ function layout_builder_preprocess_language_content_settings_table(&$variables)
+  // Prevent field label from overriding the configured block title for field
+  // blocks. See template_preprocess_block from block module for why this is
+  // necessary.
+  if ($variables['elements']['#base_plugin_id'] === 'field_block' && !empty($variables['configuration']['label']) && !empty($variables['configuration']['label_display'])) {
+    $variables['label'] = $variables['configuration']['label'];
+  }

The 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.

Status: Needs review » Needs work

The last submitted patch, 33: 3039185-block_title-33.patch, failed testing. View results

andrewsuth’s picture

I can confirm patch #33 works for me on 8.9.x as well.

andrewsuth’s picture

Moved this comment to correct issue.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

extect’s picture

#33 works for me as well on both D8 and D9. Thank you!
However, I don't get why the tests are failing.

manishsaharan’s picture

Assigned: Unassigned » manishsaharan
manishsaharan’s picture

Assigned: manishsaharan » Unassigned

#22 worked for me as well in drupal 8

manishsaharan’s picture

Priority: Normal » Major
Status: Needs work » Needs review
StatusFileSize
new2.68 KB

Resolved the above custom commands failed issues in #33 Working fine with 9.2.x

Status: Needs review » Needs work

The last submitted patch, 41: field_blocks_title-3039185-41.patch, failed testing. View results

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

swentel’s picture

Status: Needs work » Needs review
StatusFileSize
new3.36 KB
new1.46 KB

This should fix the remaining failures

Status: Needs review » Needs work

The last submitted patch, 44: 3039185-44.patch, failed testing. View results

swentel’s picture

Status: Needs work » Needs review
StatusFileSize
new3.38 KB
new833 bytes

Fix of first fail, not sure yet about second as that one passes locally :)

Status: Needs review » Needs work

The last submitted patch, 46: 3039185-46.patch, failed testing. View results

webdrips’s picture

#46 worked for me on 9.2.7

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

xaa’s picture

#46 working on 9.3.6

msypes’s picture

Also confirming that #46 works with 9.3.6

kaustubhb’s picture

Confirming that #46 works with 9.3.7

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

crutch’s picture

#46 not working for 9.4.5

leisurman’s picture

#46 is working for 9.3.19

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

MarcellinoStroosnijder made their first commit to this issue’s fork.

aaronpinero’s picture

#46 is working for me on Drupal 9.5.5

pcate’s picture

Status: Needs work » Reviewed & tested by the community

#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.

fskreuz’s picture

Just 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 #title to 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 #title property 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lauriii’s picture

This 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.

catch’s picture

@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.

catch’s picture

Forgot to untag.

  • lauriii committed fbec11ff on 10.1.x
    Issue #3039185 by nord102, yogeshmpawar, swentel, tim.plunkett,...
lauriii’s picture

Version: 11.x-dev » 10.1.x-dev
Issue summary: View changes
Status: Reviewed & tested by the community » Fixed
Issue tags: +10.1.0 release notes

Committed 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.

  • lauriii committed 7598b15a on 11.x
    Issue #3039185 by nord102, yogeshmpawar, swentel, tim.plunkett,...
neclimdul’s picture

Status: Fixed » Active
Issue tags: +Regression

This broke my site. No content on pages using field blocks.

A developer had written this:

    {% for item in content['#items'] | keys %}
      {{ content[item] }}
    {% endfor %}

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.

catch’s picture

Issue tags: +Needs change record

Let'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.

lauriii’s picture

Issue summary: View changes

I 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?

catch’s picture

I 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?

neclimdul’s picture

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.

Yes and yes but I can watch this space and deal with the fix either way.

Should we mention the render array change in both, CR and release notes?

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.

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?

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_value being provided by twig_field_value.

Before:

    {% for item in content['#items'] | keys %}
      {{ content[item] }}
    {% endfor %}

After:

    {% for item in content|children %}
      {{ item|field_value }}
    {% endfor %}
lauriii’s picture

I created a CR but I'm not sure what would be the before and after with just core because we don't have |children filter in core. Also, shouldn't it take into account that there could be something to render on the first level of the render array?

neclimdul’s picture

Wow... 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?

smustgrave’s picture

Status: Active » Fixed

Brought this up with @lauriii in #needs-review-queue-initative and agreed it could be closed again. Well marked fixed to be auto closed later.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

quietone’s picture

Issue tags: -Needs change record

This has a change record, removing tag