Problem/Motivation

Settings Tray and the Workflow initiative are essentially solving the same common problems with using forms in AJAX dialogs (#2785047: In Outside In mode, form validation messages should appear in the off-canvas tray, not the main page and #2830584: Use modals for creating, updating, and deleting workflows, with a new DialogFormTrait, respectively).

The problems were:

  1. Showing validation messages on form submit error while remaining in the dialog
  2. Refreshing the main page when needed
  3. Redirect to a path on successful commit

Proposed resolution

Provide a common trait that can be used simply by including the trait and $this->buildFormDialog($form, $form_state, FALSE, '#drupal-modal'); (with the last 2 arguments being optional).

Remaining tasks

?

User interface changes

None.

API changes

API addition.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tedbow created an issue. See original summary.

tedbow’s picture

Status: Needs review » Needs work

The last submitted patch, 2: 2896535-2.patch, failed testing. View results

Wim Leers’s picture

Issue summary: View changes
Issue tags: +Needs change record

Clarified IS.

Wim Leers’s picture

Status: Needs work » Postponed

I'd be forced to repeat all of the review points that @effulgentsia raised at #2896535: Create a helper trait for Forms in ajax dialogs, especially #2896535: Create a helper trait for Forms in ajax dialogs. Which means we're repeating the same discussion we already had.

Either this should be postponed on #2785047: In Outside In mode, form validation messages should appear in the off-canvas tray, not the main page and #2830584: Use modals for creating, updating, and deleting workflows, with a new DialogFormTrait, or those two issues must be closed in favor of this. I'm assuming the former. If it's the latter, then I'll leave it to @tedbow to mark those two issues as duplicates and reopen this.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tim.plunkett’s picture

This soft-blocks Layout Builder.

xjm’s picture

#2830584: Use modals for creating, updating, and deleting workflows, with a new DialogFormTrait is just adding a trait to its own namespace currently. What is the plan WRT the layout builder?

almaudoh’s picture

Title: Create a helper trait to for Forms in ajax dialogs » Create a helper trait for Forms in ajax dialogs

Just passing thru, but I couldn't resist...:)

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

timmillwood’s picture

Status: Postponed » Needs work

Maybe as a first step we could move \Drupal\layout_builder\Controller\AjaxHelperTrait to core.

Then look at adding items from \Drupal\layout_builder\Form\AjaxFormHelperTrait and #2830584: Use modals for creating, updating, and deleting workflows, with a new DialogFormTrait in a follow up?

timmillwood’s picture

Status: Needs work » Needs review
FileSize
2.94 KB

Here's a start for that.

I updated the patch in #2830584: Use modals for creating, updating, and deleting workflows, with a new DialogFormTrait so that the trait added there is now just a copy of the two LB traits, which will make the transition easier.

tim.plunkett’s picture

index 072eccab35..aacc75d6b8 100644
--- a/core/modules/layout_builder/src/Controller/AjaxHelperTrait.php

--- a/core/modules/layout_builder/src/Controller/AjaxHelperTrait.php
+++ b/core/lib/Drupal/Core/Ajax/AjaxHelperTrait.php

Please also remove the @todo in this file that points to this issue :)

timmillwood’s picture

FileSize
6.27 KB
8.58 KB

Lets add AjaxFormHelperTrait to the patch, and update settings tray to use it.

Still todo:
- Make AjaxHelperTrait::isAjax less brittle.

@tedbow suggested

Look at services and figure out all possible drupal_*.* values actually it would anything that uses drupal_dialog.\* or drupal_modal.\* because we can’t add a new data-dialog-type just data-dialog-renderer could vary

timmillwood’s picture

Status: Needs review » Needs work

Needs work based on the todo in #14 and the change record.

timmillwood’s picture

Issue tags: +Workflow Initiative

Yesterday I started work on #2949991: Add workspace UI in top dialog and quickly found I needed an isAjax() method, yet again copying the trait from LB. Although this time fully understanding how brittle it is, because I needed to add drupal_dialog.off_canvas_top to the array of wrapper formats.

timmillwood’s picture

Issue tags: -Needs change record

I've created a change record for this https://www.drupal.org/node/2950404

timmillwood’s picture

Status: Needs work » Needs review
FileSize
767 bytes
9.08 KB

Here's an update to make isAjax() less brittle.

tim.plunkett’s picture

+++ b/core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php
@@ -1,18 +1,13 @@
  * @internal
...
 trait AjaxFormHelperTrait {

+++ b/core/lib/Drupal/Core/Ajax/AjaxHelperTrait.php
@@ -8,8 +8,6 @@
  * @internal
...
 trait AjaxHelperTrait {

Should these still be marked @internal?

The changes to isAjax look good!

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

@timmillwood points out that while we can always remove the @internal later, if we remove it now we're stuck. Better safe than sorry, and revisit that after issues like #2830584: Use modals for creating, updating, and deleting workflows, with a new DialogFormTrait and #2949991: Add workspace UI in top dialog land.

alexpott’s picture

Committed 9f61972 and pushed to 8.6.x. Thanks!

+1 for leaving internal for now as that does give us a chance to explore what's necessary to make this even more generic in #2830584: Use modals for creating, updating, and deleting workflows, with a new DialogFormTrait. Considering they are internal I think we should hold of publishing the CR until they are not.

diff --git a/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php b/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
index c55cc4d6a0..243e907ccd 100644
--- a/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
+++ b/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
@@ -8,7 +8,6 @@
 use Drupal\Core\Ajax\AjaxFormHelperTrait;
 use Drupal\Core\Ajax\AjaxResponse;
 use Drupal\Core\Ajax\RedirectCommand;
-use Drupal\Core\Ajax\ReplaceCommand;
 use Drupal\Core\Block\BlockPluginInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Plugin\PluginWithFormsInterface;
@@ -130,12 +129,12 @@ public function buildForm(array $form, FormStateInterface $form_state) {
     $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
 
     // static::ajaxSubmit() requires data-drupal-selector to be the same between
-    // the various Ajax requests. A bug in
-    // \Drupal\Core\Form\FormBuilder prevents that from happening unless
-    // $form['#id'] is also the same. Normally, #id is set to a unique HTML ID
-    // via Html::getUniqueId(), but here we bypass that in order to work around
-    // the data-drupal-selector bug. This is okay so long as we assume that this
-    // form only ever occurs once on a page.
+    // the various Ajax requests. A bug in \Drupal\Core\Form\FormBuilder
+    // prevents that from happening unless $form['#id'] is also the same.
+    // Normally, #id is set to a unique HTML ID via Html::getUniqueId(), but
+    // here we bypass that in order to work around the data-drupal-selector bug.
+    // This is okay so long as we assume that this form only ever occurs once on
+    // a page.
     // @todo Remove this workaround once https://www.drupal.org/node/2897377 is
     //   fixed.
     $form['#id'] = Html::getId($form_state->getBuildInfo()['form_id']);

There was an used use and a comment that needed properly re-flowing.

  • alexpott committed 9f61972 on 8.6.x
    Issue #2896535 by timmillwood, tedbow: Create a helper trait for Forms...
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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

quietone’s picture

publish the change record