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
- Install a standard site
- Enable LB & add it to the page content type
- 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
| Comment | File | Size | Author |
|---|
Issue fork drupal-3115759
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
tim.plunkettSee
\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.
Comment #6
pameeela commentedAdding credit for neclimdul from #3221294: New fields get rendered by LB which I closed as a duplicate.
Comment #13
acbramley commentedOverall looking really good! We have some test failures though.
Comment #14
danielvezaTests are green, I've responded to the feedback. Happy to hear other options if people disagree. Ready for review :)
Comment #15
smustgrave commentedCan the issue summary be updated to use the standard issue template.
Left some comments on the MR.
Comment #16
danielvezaFlagging that we should add a change record for this too
Comment #17
danielvezaComment #18
danielvezaIS updated, CR created.
Setting this to needs review, there is a couple of open threads that could use some more discussion moving forward.
Comment #19
damienmckennaCould this be simplified to not add the fields by default at all, and just remove the functionality that does this?
Comment #20
smustgrave commentedThink that’s something the sub maintainer could say
Comment #21
smustgrave commentedBut 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
Comment #22
danielvezaPushed a commit that sets the new configuration to FALSE for new sites. Lets see how the tests go
Comment #23
smustgrave commentedNot 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
Comment #24
danielvezaI'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.
Comment #25
smustgrave commentedYea now that I read that out loud you’re right
Comment #26
danielvezaReverted 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
Comment #27
smustgrave commentedSpoke with @larowlan and we can push the test fixes to a follow up.
Opened #3423225: Remove layout_builder_add_new_fields_to_layout
Comment #28
alexpottAdded some review comments.
Comment #29
danielvezaI'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.
Comment #30
smustgrave commentedCan the CR be updated with the new approach same for IS.
Comment #31
danielvezaComment #32
danielvezaComment #33
danielvezaComment #34
danielvezaCR & IS updated
Comment #35
smustgrave commentedThanks! That clears things up.
Comment #36
alexpottShould 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.
Comment #37
smustgrave commentedMy understanding was using a sub module flag was a temporary solution till that was worked out
Comment #38
alexpott@smustgrave looking at the most recent comments on the issue from @catch and @larolwan is looks like each flag is going to be module...
Comment #39
longwaveA 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?
Comment #40
damienmckenna+1 for longwave's suggestion.
Comment #41
danielvezaYeah good call, I agree with this.
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.
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
Comment #42
acbramley commentedBut 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.
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?)
Comment #44
mstrelan commentedWe 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.
Comment #45
codebymikey commentedAdded a static copy of the latest MR patch.
Comment #46
chrisolof+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).
Comment #47
acbramley commentedComment #48
acbramley commentedWhen 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".inLayoutEntityHelperTraitTest- see https://git.drupalcode.org/issue/drupal-3115759/-/jobs/5455575This is because on HEAD, that test contains no sections which means we don't call
$section->getLayout()incalculateDependencies.Section::getLayoutcallsSection::layoutPluginManager.Obviously the easy fix here is to enable
layout_discoveryin 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.Comment #49
acbramley commentedRe #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.
Comment #50
danielvezaChanges 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
Comment #51
smustgrave commentedLeft 1 small comment on the MR.
Comment #52
acbramley commentedComment #53
smustgrave commentedAll feedback appears to be addressed for this and functionality working as expected. Wish I had this the other day actually!
Comment #54
danielvezaHad 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.
Comment #55
needs-review-queue-bot commentedThe 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.
Comment #56
smustgrave commentedWas a clean rebase
Comment #58
catchThis 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!
Comment #61
quietone commentedThis 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?
Comment #62
mondrakeAlso, this has broken HEAD testing because
Comment #63
mondrakeBumping to critical while HEAD testing is broken.
Comment #65
catchReverted for now - let's add the deprecated module documentation and update the test.
Comment #66
smustgrave commentedAdded to the module page https://www.drupal.org/docs/core-modules-and-themes/deprecated-and-obsol...
Comment #67
catchDiscussed 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.
Comment #68
smustgrave commentedShould I delete the documentation entry then?
Comment #74
acbramley commentedI'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.
Comment #76
acbramley commentedComment #77
smustgrave commentedStill huge +1 for this feature.
Seems to have broken a few tests