Problem/Motivation

Whenever a new field is added to an entity+bundle with LB enabled, a instance of the field is added to the first region of the default LB layout automatically.

This can be quite annoying to say the least, when you have carefully crafted a layout and fields are placed in unexpected positions. Sometimes a field is not mean to be displayed, or perhaps another site builder added a field without remembering to remove the field.

Understandably this is how fields traditionally worked, however visibility of newly added fields is more difficult to detect than simply checking the table within Manage Display tab. For example a field may be invisible, or have no sample value, or the layout/styling may only display the field conditionally.

Steps to reproduce

  1. Install a standard site
  2. Enable LB & add it to the page content type
  3. Add a new field to the page content type and see that it has been added to the layout automatically.

Proposed resolution

Added a new feature flag module to control if new fields are added to the Layout layout_builder_add_new_fields_to_layout
Enable this module for existing sites

Remaining tasks

Review
Commit

User interface changes

New fields are no longer added to the entities LB layout when layout_builder_add_new_fields_to_layout is disabled.

API changes

A new feature flag module has been added (layout_builder_add_new_fields_to_layout)

Data model changes

None

Release notes snippet

N/A

Issue fork drupal-3115759

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

dpi created an issue. See original summary.

tim.plunkett’s picture

Issue tags: +Blocks-Layouts

See \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::setComponent(), that entire method is providing this functionality.
There's a guard condition right at the top that could be expanded to include the check you are proposing.

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.

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.

pameeela’s picture

Adding credit for neclimdul from #3221294: New fields get rendered by LB which I closed as a duplicate.

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.

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.

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.

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.

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

acbramley’s picture

Status: Active » Needs work

Overall looking really good! We have some test failures though.

danielveza’s picture

Status: Needs work » Needs review

Tests are green, I've responded to the feedback. Happy to hear other options if people disagree. Ready for review :)

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Can the issue summary be updated to use the standard issue template.

Left some comments on the MR.

danielveza’s picture

Issue tags: +Needs change record

Flagging that we should add a change record for this too

danielveza’s picture

Issue summary: View changes
danielveza’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update, -Needs change record

IS updated, CR created.

Setting this to needs review, there is a couple of open threads that could use some more discussion moving forward.

damienmckenna’s picture

Issue summary: View changes

Could this be simplified to not add the fields by default at all, and just remove the functionality that does this?

smustgrave’s picture

Think that’s something the sub maintainer could say

smustgrave’s picture

But I do vote for the tests to be fixed here.

Unless there’s history for adding a setting and adding default value to another ticket. That I don’t know

danielveza’s picture

Status: Needs review » Needs work

Pushed a commit that sets the new configuration to FALSE for new sites. Lets see how the tests go

smustgrave’s picture

Not sure if this would fly though. If a bunch of tests do break, maybe in the setup we just set this new configuration to true with a todo to a follow up to cleanup.

If we do go with the config option and not remove the code out right

danielveza’s picture

Not sure if this would fly though. If a bunch of tests do break, maybe in the setup we just set this new configuration to true with a todo to a follow up to cleanup.

I'm a bit lost now. Having it FALSE by default then overriding every LB test to set it to TRUE feels significantly less clean to me then just maintaing the existing behaviour in this issue then changing the default in a follow up. Maybe I'm not understanding something.

smustgrave’s picture

Yea now that I read that out loud you’re right

danielveza’s picture

Status: Needs work » Needs review

Reverted the last commit. Since updating the default value and tests can hopefully be done in a follow up I'm moving this back into needs review

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Spoke with @larowlan and we can push the test fixes to a follow up.

Opened #3423225: Remove layout_builder_add_new_fields_to_layout

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Added some review comments.

danielveza’s picture

Status: Needs work » Needs review
Related issues: +#3423352: Add an API for feature flags

I've changed the approach of this issue to use a feature flag module rather than configuration. The unresolved threads are no longer relevent, but I'm leaving them open for the moment. Adding the feature flag issue as a related issue.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record updates

Can the CR be updated with the new approach same for IS.

danielveza’s picture

Issue summary: View changes
danielveza’s picture

Issue summary: View changes
danielveza’s picture

Issue summary: View changes
danielveza’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record updates

CR & IS updated

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! That clears things up.

alexpott’s picture

Should this be postponed on #3423352: Add an API for feature flags then as it is using the feature flag module approach but that approach is not yet 100% agreed.

smustgrave’s picture

My understanding was using a sub module flag was a temporary solution till that was worked out

alexpott’s picture

@smustgrave looking at the most recent comments on the issue from @catch and @larolwan is looks like each flag is going to be module...

longwave’s picture

A feature flag that disables something feels the wrong way round to me. I think we should install it on existing installs, where users can disable it again if they wish, but not on new installs.

The code that is behind the flag should also be moved to a hook in the feature flag module, if possible?

damienmckenna’s picture

+1 for longwave's suggestion.

danielveza’s picture

Status: Reviewed & tested by the community » Postponed

A feature flag that disables something feels the wrong way round to me.

Yeah good call, I agree with this.

I think we should install it on existing installs, where users can disable it again if they wish, but not on new installs.

For the sake of getting this one in quickly we were going to change the default behaviour in a follow up issue. #3423225: Remove layout_builder_add_new_fields_to_layout However this one is blocked now so maybe we can fit in in here.

The code that is behind the flag should also be moved to a hook in the feature flag module, if possible?

As far as I'm aware, the current goal of the feature flag modules is for them to contain no code.

Postponed on getting agreement on #3423352: Add an API for feature flags

acbramley’s picture

A feature flag that disables something feels the wrong way round to me

But this is a "feature" for developers/site builders to stop this annoying behaviour of things being added automatically.

Honestly I think this behaviour is so disruptive it could almost be considered a bug and just switched off altogether, but that's probably going to delay this even longer.

I think we should install it on existing installs, where users can disable it again if they wish, but not on new installs.

Then why not just blanket remove the functionality as per above...?

When is adding a new field automatically to a bundle's view display actually a good action for the site builder?

This seems to happen sporadically when view displays are saved via update hooks as well, fields will just get blanket added to the layout for what seems like no reason at all (perhaps they aren't already in the hidden key?)

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

mstrelan’s picture

Status: Postponed » Needs work

We already have an existing feature flag module, already in layout builder, so IMHO we don't need to postpone this on #3423352: Add an API for feature flags. Worst case we just remove these modules, which we were going to do anyway.

I've reversed the feature flag module so it provides the old behavior, and new sites get the new behavior by default.

Setting NW for a update post_update hook to enable the module on existing sites. CR also needs updating.

codebymikey’s picture

Added a static copy of the latest MR patch.

chrisolof’s picture

+1 for this change. This auto-field-adding (bug?) just bit me in a deployment.

The problematic deployment upgrades a content type's teaser display from the basic/regular type to a LB layout. When I deploy that change without this patch I get the new LB layout in place, as configured, but with all of the old fields from the old basic/regular display unexpectedly appended into the first section (result not matching config). When I apply this patch and repeat the same deployment test I get the new LB layout, as configured, with no surprise fields appended (result matching config).

acbramley’s picture

Issue summary: View changes
acbramley’s picture

When investigating some test failures I noticed this commit https://git.drupalcode.org/project/drupal/-/merge_requests/6609/diffs?co... was resulting in You have requested a non-existent service "plugin.manager.core.layout". in LayoutEntityHelperTraitTest - see https://git.drupalcode.org/issue/drupal-3115759/-/jobs/5455575

This is because on HEAD, that test contains no sections which means we don't call $section->getLayout() in calculateDependencies. Section::getLayout calls Section::layoutPluginManager.

Obviously the easy fix here is to enable layout_discovery in that test (which is a dependency of layout_builder anyway) however it seems like this may have a side effect of adding a default section when we shouldn't be.

However, reverting that commit then makes a huge number of tests fail (30 at the time of writing this) because now when we enable layout builder for a bundle, the layout is completely empty, so tests that click things like Add block fail because there is no section to add a block to.

We have several options for fixing this:
1. Individually fix each test to add a section (a lot of work)
2. Enable the feature flag module on all of tests (easiest)
3. Figure out if we should populate a layout with the existing defaults (section + fields) when it's first enabled (might not even be possible, still investigating).
4. Go back to always adding a default section even when not adding fields.

I was hoping with option 3 we could just use $this->isNew() but since LB takes over existing view displays that won't work.

acbramley’s picture

Status: Needs work » Needs review

Re #48 I've gone with 4 for now.

This is green, CR and IS updated. Would be good to get a fresh review on it.

danielveza’s picture

Changes look great. I'm happy with #49, it feels like the lowest impact solution for a situation that should be rare outside of tests.

I've popped one thought in about a hook_requirements implementation, so leaving in needs review for people to have a think about it. I don't feel strongly about this, if its decided we don't want it then this is ready to be RTBC

smustgrave’s picture

Status: Needs review » Needs work

Left 1 small comment on the MR.

acbramley’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

All feedback appears to be addressed for this and functionality working as expected. Wish I had this the other day actually!

danielveza’s picture

Had one last look through the code, looks great! It would be fantastic to get this in. I constantly wish I had this when I add new fields to content types.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Was a clean rebase

catch’s picture

Status: Reviewed & tested by the community » Fixed

This looks good. I was going to ask about where we're going to remove it from all the tests it's added to, but then I saw #3423225: Remove layout_builder_add_new_fields_to_layout, we might want to split that issue into two, but can discuss on that issue.

Committed/pushed to 11.x, thanks!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

  • catch committed 6937eea4 on 11.x
    Issue #3115759 by dpi, neclimdul, danielveza, acbramley, smustgrave,...
quietone’s picture

Status: Fixed » Needs work
Issue tags: +Needs documentation

This adds a deprecated module and it should be added to https://www.drupal.org/docs/core-modules-and-themes/deprecated-and-obsolete. There is a similar module listed on that page and the documentation style may be of some use, https://www.drupal.org/node/3223395#s-layout-builder-expose-all-field-bl....

I don't see a review of the change record. Can someone check that?

mondrake’s picture

Also, this has broken HEAD testing because

    There was 1 PHPUnit test runner deprecation:
    
    1) Metadata found in doc-comment for class Drupal\Tests\layout_builder\Functional\LayoutBuilderNewFieldsTest. Metadata in doc-comments is deprecated and will no longer be supported in PHPUnit 12. Update your test code to use attributes instead.

mondrake’s picture

Priority: Normal » Critical

Bumping to critical while HEAD testing is broken.

  • catch committed af30df5a on 11.x
    Revert "Issue #3115759 by dpi, neclimdul, danielveza, acbramley,...
catch’s picture

Priority: Critical » Normal

Reverted for now - let's add the deprecated module documentation and update the test.

smustgrave’s picture

catch’s picture

Discussed this briefly with @lauriii, he asked why we're adding a feature flag for this at all - it only affects existing sites when they add a field in the field UI, not the actual runtime of the site - so we should be fine to just change the behaviour with no bc layer.

Thinking about it more this makes sense to me too, I tried to trace back where this was discussed and I see Damien's comment asking whether it was really necessary, but otherwise we only discussed how to do it, not whether to do it or not.

Given that would allow us to remove a fair bit of code + and upgrade path + the follow-up issue, I think we should just do that here. It will mean updating some tests but that would need to happen anyway to remove the feature module.

smustgrave’s picture

Should I delete the documentation entry then?

acbramley changed the visibility of the branch 3115759-prevent-auto-adding-new to active.

acbramley changed the visibility of the branch 3115759-prevent-auto-adding-new to hidden.

acbramley’s picture

Issue tags: -Needs documentation

I've reverted the documentation page.

The tricky part here is that setComponent is used both for adding fields to the layout when Layout builder is initially enabled, and when adding new fields.

Both in the case of the old feature flag and removing the code outright, we get an empty layout when enabling layout builder. IMO this is a good thing, but just wanted to point that out if it wasn't obvious before.

We could retain that functionality and just move the code out of setComponent and into a new method called by the preSave which is where the initial addition of the fields when layout builder is enabled happens.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

acbramley’s picture

Status: Needs work » Needs review
Related issues: -#3423352: Add an API for feature flags
smustgrave’s picture

Status: Needs review » Needs work

Still huge +1 for this feature.

Seems to have broken a few tests