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
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