Problem/Motivation

\Drupal\Core\Ajax\RedirectCommand allows redirecting users to a different route after processing form. However, there isn't currently a way to render a different dialog after processing a form. This is needed in some multi-step forms that are using dialogs where the system needs to take the users to a controller or a form behind another route. This is critical in particular when working with forms because form API always depends on the current route match to be up to date to know which route to submit the form.

One existing use-case for this is #3386762: Use modals in field creation and field edit flow.

Steps to reproduce

Proposed resolution

Create a command that replicates the functionality of the \Drupal\Core\Ajax\RedirectCommand but operates within dialogs.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#7 url-opened-in-dialog.png123.83 KBarisen
#7 custom-form-with-button.png106.18 KBarisen

Issue fork drupal-3408738

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

srishtiiee created an issue. See original summary.

srishtiiee’s picture

Issue summary: View changes
lauriii’s picture

Issue summary: View changes

srishtiiee’s picture

Status: Active » Needs review
arisen’s picture

StatusFileSize
new106.18 KB
new123.83 KB

Thanks for the feature suggestion @srishtiiee.

Reviewed the Merge Request. The changes look good and working as expected.
Tested the same on a Drupal 11 installation.
Testing steps:

  • Setup a Drupal 11 site and applied the patch from the MR
  • Created a Custom Ajax form with a button and a custom submit handler.
  • Inside the handler added the code snippet to open a node in a dialog.
    $response = new AjaxResponse();
      $url = Url::fromRoute('entity.node.canonical', ['node' => 1]);
      $command = new OpenModalDialogWithUrl($url->toString(),
      [
        'url' => 'example',
        'width' => 500,
        'title' => 'Title',
        'modal' => TRUE,
      ]);
      $response->addCommand($command);
      return $response;
  • The node gets displayed in a dialog as expected.

Attaching the screenshots.

This looks RTBC for me.

smustgrave’s picture

Issue tags: +Needs change record

Feels like something that will need a CR

smustgrave’s picture

Status: Needs review » Needs work
benjifisher’s picture

I am adding #1886616: Multistep Form Wizard as a related issue. I have not looked at it closely, but I think that is more specialized than this issue, trying to make it easier to create multi-step entity forms.

kunal.sachdev made their first commit to this issue’s fork.

kunal.sachdev’s picture

Issue tags: -Needs change record

Updated the already existing CR for this.

kunal.sachdev’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
1) Drupal\Tests\Core\Ajax\AjaxCommandsTest::testOpenModalDialogWithUrl
Error: Class "Drupal\Core\Ajax\OpenModalDialogWithUrl" not found
/builds/issue/drupal-3408738/core/tests/Drupal/Tests/Core/Ajax/AjaxCommandsTest.php:509
/builds/issue/drupal-3408738/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
/builds/issue/drupal-3408738/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
/builds/issue/drupal-3408738/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:651
/builds/issue/drupal-3408738/vendor/phpunit/phpunit/src/TextUI/Command.php:144
/builds/issue/drupal-3408738/vendor/phpunit/phpunit/src/TextUI/Command.php:97
ERRORS!
Tests: 32, Assertions: 37, Errors: 1.

CR reads well.

Applied the MR and used the snippet in the CR to open a dialog. I used a node add form, as that's something I'm currently working on in my project.

Reviewing the code and believe this is good

tim.plunkett’s picture

+1 to RTBC, the Drupal.ajax vs ajax discussion in the MR can be resolved

wim leers’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs documentation, +Needs change record updates

There are a bunch of small inconsistencies in the code. Some parameters/decisions should be documented. And most importantly: neither this MR nor the CR document how this relates to the multiple existing dialog-related AJAX commands. "When to use which?" is a critical portion of landing this.

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

arisen’s picture

Status: Needs work » Needs review

And most importantly: neither this MR nor the CR document how this relates to the multiple existing dialog-related AJAX commands. "When to use which?" is a critical portion of landing this.

Updated the CR to address this.

smustgrave’s picture

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

CR appears to have been updated with a usage.

Not sure where documentation should go though. Assuming here https://www.drupal.org/docs/drupal-apis/ajax-api/core-ajax-callback-comm... but imagine the update should be after the code is committed right?

wim leers’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs security review

This is still missing crucial documentation, so it can't be RTBC.

Plus, based on the test coverage I just realized that this appears to be introducing a new potential attack surface 😰 So tagging Needs security review as well…

omkar.podey made their first commit to this issue’s fork.

omkar.podey’s picture

Status: Needs work » Needs review
wim leers’s picture

Status: Needs review » Needs work
omkar.podey’s picture

Status: Needs work » Needs review
omkar.podey’s picture

Status: Needs review » Needs work
omkar.podey’s picture

Set to needs work for writing a better test.

omkar.podey’s picture

Status: Needs work » Needs review
catch’s picture

The hardening and test coverage looks good to me.

smustgrave’s picture

Have posted to #security-discussion in slack so fingers crossed someone can pick up.

lauriii’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs documentation, -Needs security review

The aspect that required security review has been addressed. I think a regular committer review is sufficient now.

wim leers’s picture

Status: Reviewed & tested by the community » Needs work

Yes, agreed, the security concerns I raised are now addressed 👍

Just a few remarks to ensure A) docs are discoverable by developers, B) this stays aligned/in sync with the other place in core where this pattern is used.

omkar.podey’s picture

Status: Needs work » Needs review
lauriii’s picture

Status: Needs review » Reviewed & tested by the community

Feedback from @Wim Leers has been addressed 👍

  • catch committed 6068cc4f on 10.3.x
    Issue #3408738 by omkar.podey, srishtiiee, kunal.sachdev, lauriii,...

  • catch committed 6fca4655 on 11.x
    Issue #3408738 by omkar.podey, srishtiiee, kunal.sachdev, lauriii,...
catch’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed
Issue tags: +Needs followup

One more thought - we could add an extra method on UrlHelper for the local check, combining those two method calls. I was thinking about a trait, but why not just an extra helper method directly on that class.

Not for here though, so committed/pushed to 11.x and cherry-picked to 10.3.x, thanks!

Status: Fixed » Closed (fixed)

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

quietone’s picture