At this point I'm doubting if I'm doing it the right way, but I'm struggling with an issue:
I'm adding an Ajax callback to the form alter of a specific form:
function mymodulename_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'webform_submission_matching_node_218_add_form') {
$form['actions']['submit']['#ajax'] = array(
'callback' => 'matching_api_call',
//'event' => 'click',
'effect' => 'fade',
'speed' => 500,
'wrapper' => 'block-matchingblock',
'progress' => [
'type' => 'throbber',
'message' => t('Matching...'),
]);
}
}
The callback works fine but when I call $form_state->getValues(), I get an array with all my fields but they are simply empty or 0.
My callback function:
function matching_api_call(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
// ajax debug
$response = new AjaxResponse();
$selector = '#block-matchingblock';
$content = print_r($form_state->getValues(), true);
$settings = [];
$response->addCommand(new HtmlCommand($selector, $content, $settings));
return $response;
}
My output:
Array(
[field1] =>
[field2] =>
[field3] => 0
[field4] => 0
[submit] => Match
[form_build_id] => {formbuildid}
[form_token] => {formtoken}
[form_id] => {form_id}
[op] => Match
)
The entry is added in the backend so it does get submitted. Should I do something else because I'm using Ajax here?
Thanks in advance.