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:

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

  • It's unclear what you are trying to achieve, but you shouldn't place any form logic in Ajax callbacks. The easiest way to add custom code to webform submitting would be a webform handler plugin.
    – 4uk4
    Nov 27, 2020 at 12:22
  • Thanks for your input 4k4. Sorry for the confusion, I'm not trying to achieve anything specific in my code above, I'm just debugging the getValues() output. In the end when the form is submitted, some field values have to be sent to an API but they are just empty. Thanks for the tip, I will try it with a custom handler.
    – peird
    Nov 27, 2020 at 13:13

1 Answer 1


Someone guided me in the right direction. My Webform Confirmation setting was set to 'None'. After setting it to 'Inline', calling $form_state->getValues() finally returns the form values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.