Problem/Motivation

Modules that want to modify the section configuration form in layout builder may have a hard time, because there's no way to retrieve the layout plugin object. Contrib needs access to the plugin object so it can save configuration for it.

Proposed resolution

Add public getters for retrieving the layout and section, like we already did for the block configuration form in #3003666: Provide access to a Section or SectionComponent from within a $form_state

Remaining tasks

Per #3044117-49: Add getter for layout object in Layout Builder's ConfigureSectionForm:

  • This is a feature request and should be testing 9.5.x instead of 9.3.x.
  • And let's add a test on D10 as well.
  • Once completed and passed, return to RTBC?

User interface changes

N/A

API changes

No

Data model changes

No

Release notes snippet

Issue fork drupal-3044117

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

bkosborne created an issue. See original summary.

bkosborne’s picture

Title: Add getters for section and layout in Layout Builder's ConfigureSectionForm » Add getter for layout object in Layout Builder's ConfigureSectionForm
Status: Active » Needs review
StatusFileSize
new694 bytes
new2.59 KB

Here's two versions of the patch.

One simply adds a getter for the layout object, which I think would be enough for contrib.

To also provide access to the section object, more changes are needed, since the section object is either retrieve or created as needed. We'd need to create the object in the form builder and save it as a property, instead of re-creating it in the submit function. That way contrib could mess with it if they wanted to.

tim.plunkett’s picture

Issue tags: +Blocks-Layouts
tim.plunkett’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests
+++ b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php
@@ -203,4 +210,24 @@ public function getSectionStorage() {
+   * Retrieve the layout being modified by the form.
...
+   * Retrieve the section being modified by the form.

Nit: Retrieves

Needs tests, see #3003666: Provide access to a Section or SectionComponent from within a $form_state for inspiration

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.

eescribanoc’s picture

I just added the tests in this patch.

eescribanoc’s picture

Status: Needs work » Needs review
StatusFileSize
new5.95 KB

While checking the latest patch to add the tests it didn't apply so I had to repatch it to fit the core version 8.9

Status: Needs review » Needs work
eescribanoc’s picture

StatusFileSize
new784 bytes

I added 2 lines to the test so hopefully it does not fail.

eescribanoc’s picture

Status: Needs work » Needs review
eescribanoc’s picture

StatusFileSize
new784 bytes

Sorry, wrong naming in #10, re-uploading the interdiff

eescribanoc’s picture

And the missing patch that works (assumptions on how interdiff worked, sorry for spamming)
The interdiff is "interdiff_7-12.txt"

emerham’s picture

Status: Needs review » Reviewed & tested by the community

Ran into an issue in contrib space where lb_ux and layout_builder_styles no longer played nicely together. This patch allows layout_builder_styles to get the current layout for the section forms.

bkosborne’s picture

As #14 said, this is blocking two contrib modules from being enabled together. LB Styles needs access to the layout object, and to do so it's overriding the section configure form object with a version that exposes the object. The LB UX module assumes the original form object is being used... and things break.

tim.plunkett’s picture

Issue tags: -Needs tests, -Amsterdam2019

+1 to RTBC.

Re: #15, LB Styles is overriding the form class of the layout_builder.configure_section route, and adding a new method. LB UX doesn't touch that route, that part is fine. But on another route, LB UX uses the original form class provided by Layout Builder. And since Layout Builder Styles' form_alter implementation assumes that that its version of that form class will always be used, it breaks.

Status: Reviewed & tested by the community » Needs work

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.

neograph734’s picture

Status: Needs work » Reviewed & tested by the community

This was changed to needs work because of a failed test in the media library.

Since the test just passed against 9.1 and there are already 2 RTBC's, I am setting this back to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs change record

This needs a change record before it can be committed.

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.

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

tim.plunkett’s picture

@iStryker we already have a patch. Making an empty branch is not very helpful.

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.

zterry95’s picture

Upgrade the patch, so that it applible on 9.2.x

segovia94’s picture

Looks like #25 lost the tests, so this is a re-roll for 9.3.x from #13.

segovia94’s picture

Not sure if I'm able to RTBC when I've done the latest reroll, but this is working for me.

segovia94’s picture

Issue tags: -Needs change record

Per #20, I've added a change record that can be reviewed here: https://www.drupal.org/node/3243396

tim.plunkett’s picture

  1. +++ b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php
    @@ -114,18 +121,18 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt
         if ($this->isUpdate) {
    ...
    +      $this->section = $this->sectionStorage->getSection($this->delta);
    ...
         else {
    ...
    +      $this->section = new Section($plugin_id);
    
    @@ -231,4 +240,24 @@ public function getSectionStorage() {
    +  public function getCurrentSection() {
    +    return $this->section;
    

    One thought. What about having getCurrentSection() contain this logic, wrapped in a !isset($this->section)? Not that there's any chance of this being called before it's set currently, but just for consistency.

  2. +++ b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php
    @@ -231,4 +240,24 @@ public function getSectionStorage() {
    +  public function getCurrentLayout() {
    ...
    +  public function getCurrentSection() {
    

    These should have return types in PHP now, in addition to the @return docs

segovia94’s picture

I've added in the recommendations from #29 (assuming I correctly understood the request).

tim.plunkett’s picture

That's exactly what I meant, thanks

  1. +++ b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php
    @@ -77,4 +77,11 @@
    +   * The plugin id.
    

    s/id/ID

  2. +++ b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php
    @@ -119,20 +126,19 @@
    -    }
    
    @@ -246,7 +252,7 @@
    +  public function getCurrentLayout() : LayoutInterface {
    
    @@ -256,7 +262,16 @@
    +  public function getCurrentSection() : Section {
    

    One nit: no space before the colon, only after.

tim.plunkett’s picture

+++ b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php
@@ -179,14 +192,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
     if ($this->isUpdate) {
       $this->sectionStorage->getSection($this->delta)->setLayoutSettings($configuration);
     }
     else {
-      $this->sectionStorage->insertSection($this->delta, new Section($plugin_id, $configuration));
+      $this->section->setLayoutSettings($configuration);
+      $this->sectionStorage->insertSection($this->delta, $this->section);

The else case has a new call to setLayoutSettings() (needed since the new Section() call is done earlier now).
This is also in the if, and that one repeats the contents of $this->getCurrentSection().

In fact, this now introduces the only references to $this->section (outside the getter).

I think this could be refactored as follows:

$section = $this->getCurrentSection();
$section->setLayoutSettings($configuration);
if (!$this->isUpdate) {
  $this->sectionStorage->insertSection($this->delta, $section);
}

Sorry for the piecemeal reviews! I really think this is the last one 😅

segovia94’s picture

Refactored with suggestion from #33.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

I think this is good to go! Thanks

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.

Status: Reviewed & tested by the community » Needs work
spokje’s picture

Status: Needs work » Reviewed & tested by the community

Back to RTBC per #35 after #3255836: Test fails due to Composer 2.2 solved the unrelated test failure.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I think we should be setting $this->section in the constructor and then

public function getCurrentSection(): Section {
  return $this->section;
}

The reason I'd do this is that $this->delta, $this->plugin and $this->isUpdate are all set in the constructor as well so setting this in constructor is consistent. I realise this is a revert from what was asked in #29. I'm concerned that the method is responsible for instantiation then these things could get out of sync.

Additionally it's inconsistent with $this->layout and the new getCurrentLayout(). this->layout is set in the constructor and retrieved from the section object.

tim.plunkett’s picture

Those are all passed into public function buildForm(), not the constructor.

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.

Webbeh’s picture

Given #40, is this back to Needs Review, or is the feedback/NW in #39 still active/applicable?

ravi.shankar’s picture

Status: Needs work » Needs review

I think this needs to be reviewed once after comments #39 and #40. So changing status to needs review.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

This appears to look good to me. Excited for such a feature!

If there is more to it please move back.

Status: Reviewed & tested by the community » Needs work
Webbeh’s picture

Status: Needs work » Reviewed & tested by the community

The last patch says 'Build Successful', so manually returning it to RTBC?

smustgrave’s picture

So I learned if it says build successfully it means there were so many failures it couldn’t output. I just triggered a 9.5 test to see what happens

smustgrave’s picture

9.5 passed so don't see any issue

quietone’s picture

Status: Reviewed & tested by the community » Needs work

This is a feature request and should be testing 9.5.x instead of 9.3.x. And let's add a test on D10 as well.

Webbeh’s picture

Issue summary: View changes
neograph734’s picture

@quietone I am not sure what you mean. I have spun up a Drupal 10 test, which passes. But that is so easy that I suppose you could have done that yourself.
So, then what did you mean?

jayhuskins’s picture

Status: Needs work » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 29849ead9e to 10.1.x and 05233f3d00 to 10.0.x and 0660f7a20c to 9.5.x. Thanks!

  • alexpott committed 29849ea on 10.1.x
    Issue #3044117 by e.escribano, segovia94, bkosborne, Webbeh, zterry95,...

  • alexpott committed 05233f3 on 10.0.x
    Issue #3044117 by e.escribano, segovia94, bkosborne, Webbeh, zterry95,...

  • alexpott committed 0660f7a on 9.5.x
    Issue #3044117 by e.escribano, segovia94, bkosborne, Webbeh, zterry95,...

Status: Fixed » Closed (fixed)

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