Use AJAX callbacks in formAlter

Created on 19 September 2023, about 1 year ago

Problem/Motivation

Is it possible to use AJAX callbacks in the formAlter method?

Steps to reproduce

I tried implementing an AJAX callback in a node edit form which has a select field "field_provinces".

The throbber does seem to trigger on change, however my AJAX callback function is not triggering if I change the value. I set a breakpoint in $form_state->getValue('field_provinces') but it doesn't break.

I also tried doing my callback without ::

Am I doing something wrong here?

/**
   * {@inheritdoc}
   */
  public function formAlter(array &$form, FormStateInterface &$form_state, $form_id) {
    $form['field_provinces']['widget']['#ajax'] = [
      'callback' => '::retrieveContactLists',
      'event' => 'change',
      'wrapper' => 'contact-lists-wrapper',
      'progress' => [
        'type' => 'throbber'
      ]
    ];

    $form['field_sendtex_list']['#attributes'] = [
      'id' => 'contact-lists-wrapper'
    ];

  }

  public function retrieveContactLists(array &$form, FormStateInterface $form_state) {
    $form_state->getValue('field_provinces');
    $form_state->getValues();
  }
💬 Support request
Status

Closed: works as designed

Version

1.5

Component

Code

Created by

🇧🇪Belgium Breasy

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024