Problem/Motivation
There is currently no way to add or alter components in a layout via a recipe. We should have a config action that allows recipe to add or edit components in a layout.
Proposed resolution
Created new ConfigSectionListTrait with a config action for adding a new component.
An example provided here:
config:
actions:
dashboard.dashboard.welcome:
addComponentToLayout:
section: 0
position: 4
component:
region:
layout_twocol_section: 'second'
default_region: content
configuration:
id: dashboard_text_block
label: 'My new dashboard with weight from a plugin'
label_display: 'visible'
provider: 'dashboard'
context_mapping: { }
text:
value: '<p>My new dashboard with weight from plugin</p>'
format: 'basic_html'
The config action would get a position, and will calculate the weight based on that, and recalculate the weights of the components after this one.
The region is a map of layout => region in case we don't know if the given section has a concrete layout.
A default_region can be given in case the section layout is not on that map.
Remaining tasks
Review.
User interface changes
None.
Introduced terminology
None.
API changes
API addition.
Data model changes
None.
Release notes snippet
TBD.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3475115
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:
- 11.x
compare
- 3475115-configaction-layout-component
changes, plain diff MR !9801
Comments
Comment #2
quietone commentedComment #4
penyaskitoThought that this could fit in SectionListTrait, but timplunkett pointed out that this would only make sense in config based section lists, so will be its own trait.
Comment #6
plopescGreat step forward @penyaskito. Really looking forward to have this in core!
Tried to test it locally to integrate it with navigation module with no luck. Probably I missed one or more steps.
Steps I followed:
use ConfigSectionListTrait;to NavigationSectionListStorageuuidGenerator()methodddev . php core/scripts/drupal recipe modules/my_recipeThe "addComponent" plugin does not exist. Valid plugin IDs for Drupal\Core\Config\Action\ConfigActionManager are: ....The recipe.yml used for testing was like this:
Could you please indicate the steps I missed?
Thank you!
Comment #7
penyaskitoApparently I was wrong and looking at
\Drupal\Core\Config\Action\Plugin\ConfigAction\Deriver\EntityMethodDeriver::getDerivativeDefinitions, ActionMethods are only valid forConfigEntityTypeInterfaceimplementations, so Navigation will need to declare its ownConfigActionplugin.So this will only be useful as is for
LayoutBuilderEntityViewDisplayin core, and of course contrib.Comment #8
penyaskitoMR is ready for review. We might want more test coverage for the weight recalculation, but I'd prefer to get feedback on the direction.
I'm not sure how are we testing config actions. Ideally we would have test recipes instead of just testing the method.
We might want to implement this in LayoutBuilderEntityViewDisplay, but not sure if part of the same MR.
Comment #9
phenaproximaDrupal CMS is going to need this, tagging accordingly.
Comment #10
phenaproximaKicking back for addressing my remaining feedback. :) But I think this is a great start on a helpful action, and I'd really like to see it in core.
Comment #11
penyaskitoDidn't remove the changed I had before yet.
This means that setSection needs to change its visibility from protected to public in SectionListTrait.
Not sure if I implemented the deriver right, but this worked for me with a slightly different version of the previous recipe:
Comment #12
penyaskitoThis needs better test coverage.
We only cover adding new components. I'd expect we want a separate action for adding multiple components, and a separate one of editing existing components, as @phenaproxima said it would be better to have them separated.
Not sure if we should cover these here or in follow-ups.
Comment #13
gábor hojtsyThe MR only seems to add the "add layout component" action, so I think its far that it only has tests for that. @phenaproxima indicated that these improvements to the tests would be good, that still looks like needs work:
Comment #14
gábor hojtsyReached out to phpstan folks. Learned that #3484349: Add missing @return types for StringTranslationTrait::formatPlural() and ::getNumberOfPlurals() is in the way, until that lands, the phpstan baseline needs updating in the MR.
Comment #15
pameeela commentedComment #16
phenaproximaDiscussed with @penyaskito and we agreed that this is nice to have, and would accelerate Drupal CMS's dashboard work track, but is not a stable blocker.
Comment #17
pameeela commentedTagging as a target so it's on the radar somewhere, it's intended to be used for nice-to-haves.
Comment #18
penyaskitoAdded moar test coverage as requested.
The only remaining comments are about reusing the existing array, but don't think creating a new one and copying every key value (as we don't know the specific block schema) would make it more clear.
Comment #19
gábor hojtsyThe added test coverage looks good IMHO. This part needs a change record I think?
Comment #20
thejimbirch commentedIs it possible for 2 change records? We have been adding them for new and altered config actions. Something like:
Comment #21
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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 #23
thejimbirch commentedComment #24
penyaskitoThis should be ready, we still need the change record for making public setSection.
Comment #25
penyaskitoNW per https://git.drupalcode.org/project/drupal/-/merge_requests/9801#note_486598
Comment #26
penyaskitoComment #27
penyaskitoBack to NR.
Comment #28
penyaskitoComment #29
phenaproximaLooks great. I have basically no complaints about the action itself. My only outstanding issues:
setSection.I had a couple of minor nitpicks as well. But this doesn't feel far off to me.
Comment #30
thejimbirch commentedDraft change record for the action exists: https://www.drupal.org/node/3519491
Can you add a little more detail with what your want with "the other to mention the publicizing of setSection."? I can try to add it.
Comment #31
thejimbirch commentedComment #32
phenaproximaPretty simple - I think we just want to mention that
\Drupal\layout_builder\SectionListTrait::setSection()is now a public method and will be available on every class that uses SectionListTrait.Comment #33
penyaskitoFixed change record and IS with a (valid and tested) example.
Comment #34
penyaskitoAnd added missing change record for SectionListTrait::setSection() is now a public method
Comment #35
penyaskitoI'd hope to have this on 11.2 for any further development around providing/adjusting dashboards in Drupal CMS for the next months.
Comment #36
phenaproximaThis looks great to me. There are a couple of minor gaps here, such as:
Since I agree that we'd really like this in 11.2, I think it is okay to add that coverage in a follow-up rather than hold this issue up any longer. Tagging for that, but otherwise...ship it.
Comment #37
larowlanLooking good, asked a question of the maintainers about the API addition.
It would be good to add some docs to the action. We have some example YML in comments above - something like that would be really valuable.
Comment #38
penyaskito#36 Created the test for the first point, added follow-up #3522807: Add unit test for Drupal\layout_builder\Plugin\ConfigAction\Deriver\AddComponentDeriver for the second one.
Added phpdoc with example and docs for the config action.
Comment #39
penyaskitoComment #40
danielvezaMR is looking good. I've done a couple of code reviews and left a few minor comments. re: #37 I'm ok with the API change as outlined in the MR, but I'll leave the tag for now just in the case the other LB maintainers want to weigh in.
Comment #41
phenaproximaCouple minor points but I'm happy with this. If the subsystem maintainer signs off, ship it. Call this a soft RTBC.
Comment #42
penyaskitoAll feedback resolved. Only detail is confirming that we're fine leaving #3523019: `\Drupal\layout_builder\Section::setComponent` should verify the region in that layout exists as a follow-up, as I don't think we should do that in the action itself.
Comment #43
larowlanCredits
Comment #44
larowlanDidn't mean to change status
Comment #47
larowlanCommitted to 11.x
Published both of the change records
Thanks all