I have a custom page (controller) which embeds a node form. I want to submit this form via ajax but also run another submit handler which opens a modal with some of the values from that form.
My controller function is simple:
public function createQuote() {
$node = \Drupal\node\Entity\Node::create(['type' => 'mpep_quote']);
$form = \Drupal::service('entity.form_builder')->getForm($node);
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['open_modal'] = [
'#type' => 'submit',
'#value' => t('Open modal'),
];
$form['actions']['#access'] = FALSE;
return $form;
}
This works fine to show the form and clicking my new submit button submits the form (non-ajax).
I have tried numerous (recommended and unsuccessful) methods of trying to get the form to submit using ajax, such as adding this to $form['open_modal']:
'#ajax' => [
'callback' => '::openQuoteModal',
'event' => 'click',
],
where openQuoteModal is my additional submit function. This does not submit the form via ajax nor does it run my added submit handler.
The only way I have been able to get the form to submit via ajax is to add this to the $form['open-modal']:
'#attributes' => ['class' => ['use-ajax-submit']],
This does work to submit the form via ajax, but nothing I have done will run my added submit handler. I have tried the following:
- #ajax callback as shown above
- adding a #submit to the form submit button
- adding a #submit to the form itself
Is there any way to get both the from to submit via ajax as well as run a secondary form submit handler?