Problem/Motivation

When there is a controller, that responds to an ajax request, wants to open a dialog (modal, non-modal, or off-canvas) without the requestor knowing about it upfront, i.e. the request doesn't contain the _wrapper_format=drupal_... query argument, then the following code does work, but the options like "resizable" or "fraggable" don't:

    $response = new AjaxResponse();
    $form = $this->formBuilder()->getForm(Wrapper::class);
    $title = $form['#title'] ?? $this->t('Configuration');
    $response->addCommand(new OpenOffCanvasDialogCommand($title, $form));
    return $response;

This is because the library core/drupal.dialog.off_canvas (or equivalent) is not attached. It only gets attached when the _wrapper_format is present with the request.

This could be resolved by explicitly attaching the library:

    $response = new AjaxResponse();
    $form = $this->formBuilder()->getForm(Wrapper::class);
    <strong>$form['#attached']['library'][] = 'core/drupal.dialog.off_canvas';</strong>
    $title = $form['#title'] ?? $this->t('Configuration');
    $response->addCommand(new OpenOffCanvasDialogCommand($title, $form));
    return $response;

The DX could be improved if those commands were to always add the required libraries themselves. Otherwise I wonder why they're even be around.

Issue fork drupal-3532803

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

jurgenhaas created an issue. See original summary.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

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