I'm trying to add contact form with AJAX callback, called from another form. Form is embedding as well, but when I submit it only page is reloading and form doesn't send any data (in my case it's a contact form, and I can't see any created contact message in backoffice after submitting).
function somemodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['somefield']['widget']['#ajax'] = [
'callback' => 'somemodule_form_conditional_ajax',
'method' => 'html',
'wrapper' => 'form-conditional-ajax-wrapper',
'event' => 'change',
];
$form['#suffix'] = '<div id="form-conditional-ajax-wrapper"></div>'
}
function somemodule_form_conditional_ajax($form, FormStateInterface $form_state) {
$output = [];
$values = $form_state->getValues();
$conditional_form_id = $values['somefield'][0]['value'];
$contact_message = \Drupal::entityTypeManager()
->getStorage('contact_message')
->create([
'contact_form' => $conditional_form_id,
]);
if (!is_null($contact_message) && $contact_message instanceof Drupal\Core\Entity\EntityInterface) {
$output = \Drupal::service('entity.form_builder')->getForm($contact_message);
}
return $output;
}