Score:0

Using the off-canvas dialog, how can I submit and close it, without redirect?

cn flag

I use this as link:

<a
  class="use-ajax"           
  data-dialog-options="{&quot;width&quot;:800}" 
  data-dialog-renderer="off_canvas" 
  data-dialog-type="dialog" 
  href="/group/1/content/create/group_node%3Apage"
>
  Create page
</a>

When submitting the page, I like the dialog to close without redirecting the new page created.

How can this be accomplished? If I add ?destination= with the current URL, then the URL of the off-canvas page is used, not the current page.

Score:1
cn flag

Add an Ajax callback to the submit button:

$form['actions']['submit'] = [
  '#type' => 'submit',
  '#value' => $this->t('Submit'),
  '#ajax' => [
    'callback' => '::ajaxCloseForm',
  ],
];

Which sends a CloseDialogCommand:

/**
 * AJAX callback to close the off-canvas dialog
 */
public function ajaxCloseForm(array&$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new CloseDialogCommand('#drupal-off-canvas'));
  return $response;
}

You can also add additional Ajax commands to update the DOM of the page to see the effect of the submit without reloading the page.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.