I am somewhat new to Drupal development. Please bear with me on this.
I am trying to create a popup after a user enters a value in a date field on a node/add form. This is on a Drupal 9 site. To do this I am trying to add a callback function to my field.

I have my form_alter hook that I know is working because I have other changes that already work as well I have a logger to tell me that code is running in this block.

This is the code I have added to invoke the callback.

 $form['field_birthdate']['widget']['#prefix'] = '<div id="field-birthdate-custom-wrapper">';
  $form['field_birthdate']['widget']['#suffix'] = '</div>';
  $form['field_birthdate']['widget']['#ajax'] = [
          'callback' => '_custom_age_check_pop_up',
          'disable-refocus' => FALSE,
          'event' => 'change',
          'wrapper' => 'field-birthdate-custom-wrapper',

I also have my callback function in my module.

function _custom_age_check_pop_up(&$form, FormStateInterface $form_state) {
    \Drupal::logger('custom')->error('Age check pop_up');

The Drupal logger in the _custom_age_check_pop_up function does not fire.

If I do a var_dump of $form['field_birthdate'] before I modify 'field_birthdate' and then again after I can see that my changes to the field is modifying the field. The AJAX key shows the call back and the other items added in the code above.

There are no error messages in the JS Console in Chrome. I have tried the basic things like clearing the cache.

I am using an example of code that I know worked where it was used before. I am unsure why this is not working for me.

I appreciate any help with this.


  • What is the intended purpose of the “widget” array key?
    – mona lisa
    Commented Oct 31, 2022 at 23:25
  • I used the "widget" array key because that is what I had see done. I tried taking it out and it does not the function of the form. Commented Oct 31, 2022 at 23:36
  • Have a look further down $form['field_birthdate']['widget'], there's probably another render array inside it (maybe with key value), which contains the form element. That's the array you need to add #ajax to
    – Clive
    Commented Nov 1, 2022 at 0:00
  • @Clive it works when I use $form['field_birthdate']['widget'][0]['value']['#prefix'] (where prefix can be suffix and AJAX as well). Do you want to add answer so I can upvote it for you as well as have it here if other users have this problem. Commented Nov 1, 2022 at 15:26


Your Answer

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

Browse other questions tagged or ask your own question.