Problem/Motivation

Unable to use layout builders "Discard changes" button when the entity 'layout_builder' form mode has validation errors.

The layout builder override form renders the entity form in the 'layout_builder' mode. If a form field is added which has server or client side validation which fails, then the discard button will not work. The discard button should work independently from the rest of the form.

Steps to reproduce

  1. Create an entity type and enable layout builder on it. Allow entities to customize individual layouts.
  2. Add a required text field to the entity.
  3. Add a new form mode to the entity type with machine name 'layout_builder', if it does not exist already.
  4. Edit the layout_builder form mode for the entity type, and configure the previously created required text field so it is visible on this form mode.
  5. Save.
  6. Create and save the entity
  7. Access the layout builder page for the entity, make changes.
  8. Try to use the discard button, without adding a value to the required text field.
  9. You will see: HTML validation errors because the text field is not filled.

Proposed resolution

When using the discard button: You should see: the browser ignores form input and skips to the discard confirmation page.

Remaining tasks

Fix failing tests
Review
Commit

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dpi created an issue. See original summary.

dpi’s picture

Using limit validation errors on discard button may be a path forward.

tim.plunkett’s picture

Issue tags: +Needs tests, +Blocks-Layouts

Good catch, let's get some test coverage first.

dpi’s picture

Assigned: Unassigned » dpi
dpi’s picture

Test + fix.

Used a new file for tests. There are similar test files, but this set of tests don't need all the heavy dependencies of existing tests, including node/views.

Status: Needs review » Needs work

The last submitted patch, 5: 3165010-lb-discard-button-validation.patch, failed testing. View results

dpi’s picture

acbramley’s picture

+++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderFormModeTest.php
@@ -0,0 +1,107 @@
+    $urlParameters = ['entity_test' => $entity->id()];
+    $url = Url::fromRoute('layout_builder.overrides.entity_test.view', $urlParameters);

Big nit: we don't need the $urlParameters var anymore as it's only used once.

Other than that, can't fault it!

acbramley’s picture

Status: Needs review » Reviewed & tested by the community

Nice! Sending to RTBC

larowlan’s picture

Status: Reviewed & tested by the community » Needs review

This looks great, just a couple of observations that may or may not be relevant

  1. +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderFormModeTest.php
    @@ -0,0 +1,106 @@
    +    EntityFormMode::create([
    ...
    +    $displayRepository->getFormDisplay($entityTypeId, $bundle, $formMode)
    

    we create the form mode and have it in context, but we re-load it back from the repository and save it again - can we set the component before the first save call and avoid the second save as well as the repository load?

  2. +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderFormModeTest.php
    @@ -0,0 +1,106 @@
    +    $session->addressEquals('/entity_test/1/layout/discard-changes');
    

    Should we also test that we can submit the discard changes form once we arrive here?

dpi’s picture

Thanks @larowlan,

we create the form mode and have it in context, but we re-load it back from the repository and save it again - can we set the component before the first save call and avoid the second save as well as the repository load?

The mode is created, then the display is created. They are different config types. getFormDisplay will create an entity for us, not fetch the mode from previous line.

Feel free to correct me if there's a better way of doing this.

Should we also test that we can submit the discard changes form once we arrive here?

Updated the test to make sure this form submits.

tim.plunkett’s picture

You are right that those are two objects. But by using the display repository, there's still a double save.

Adjusting the test to fit with other LB test conventions. These are only stylistic changes.

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.

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.

smustgrave’s picture

Assigned: Unassigned » smustgrave

I have time and can test this today.

smustgrave’s picture

Assigned: smustgrave » Unassigned
Status: Needs review » Reviewed & tested by the community

Following the steps in the description I was able to verify the error
Using patch #15 resolved the issue

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work

The patch for 9.1 and 10 are both failing tests, setting back to NW.

acbramley’s picture

The 9.1.x failures seem unrelated (and outdated?), I couldn't reproduce the 10.x failure locally even with assertions turned on but probably something with my setup. Comparing to other similar test setups, I think it may be to do with full vs default mode.

DishaKatariya’s picture

Assigned: Unassigned » DishaKatariya
DishaKatariya’s picture

Assigned: DishaKatariya » Unassigned
acbramley’s picture

Fixed the assertion error by setting a label on the test entity.

darvanen’s picture

Status: Needs review » Reviewed & tested by the community
  • I've read the comments and all feedback appears to be addressed.
  • Test looks good to me and is passing.
  • No material changes to functionality of patch since it was manually tested by @smustgrave
  • IS looks complete (assuming [empty] means "none")
  • Can't see any core gates we've missed

LGTM, kicking off test for 9.4 but not 10.x because HEAD isn't passing right now.

larowlan’s picture

The last submitted patch, 23: 3165010-23.patch, failed testing. View results

alexpott’s picture

Version: 9.5.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed db43eba838 to 10.1.x and 668e3a781a to 10.0.x and b84170c86a to 9.5.x and e5c94094be to 9.4.x. Thanks!

Backported to 9.4.x as a non-disruptive bug-fix.

  • alexpott committed db43eba on 10.1.x
    Issue #3165010 by dpi, acbramley, tim.plunkett, larowlan: Using the...

  • alexpott committed 668e3a7 on 10.0.x
    Issue #3165010 by dpi, acbramley, tim.plunkett, larowlan: Using the...

  • alexpott committed b84170c on 9.5.x
    Issue #3165010 by dpi, acbramley, tim.plunkett, larowlan: Using the...

  • alexpott committed e5c9409 on 9.4.x
    Issue #3165010 by dpi, acbramley, tim.plunkett, larowlan: Using the...

  • alexpott committed e1b4aaf on 10.1.x
    Issue #3165010 follow-up: Using the layout builder discard changes...

  • alexpott committed 0888fc9 on 10.0.x
    Issue #3165010 follow-up: Using the layout builder discard changes...

  • alexpott committed 5da19f9 on 9.5.x
    Issue #3165010 follow-up: Using the layout builder discard changes...

  • alexpott committed 899fec2 on 9.4.x
    Issue #3165010 follow-up: Using the layout builder discard changes...
alexpott’s picture

This caused a regression on D10 because classy is deprecated there. There's no need for the test to use classy so I changed the default theme.

commit e1b4aafae9c2cedd9443ed9ab591e0f5d01b0a75 (HEAD -> 10.1.x, origin/10.1.x)
Author: Alex Pott <alex.a.pott@googlemail.com>
Date:   Thu Oct 6 16:25:32 2022 +0100

    Issue #3165010 follow-up: Using the layout builder discard changes button should ignore any input and skip validation

diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderFormModeTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderFormModeTest.php
index 24d28c3839..8ced12daea 100644
--- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderFormModeTest.php
+++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderFormModeTest.php
@@ -29,7 +29,7 @@ class LayoutBuilderFormModeTest extends BrowserTestBase {
   /**
    * {@inheritdoc}
    */
-  protected $defaultTheme = 'classy';
+  protected $defaultTheme = 'stark';

   /**
    * {@inheritdoc}
alexpott’s picture

I ensured that the test still fails without the fix with stark as the default theme.

Status: Fixed » Closed (fixed)

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