Score:0

Callback Not Firing On Change

gf flag

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.

Thanks!

id flag
What is the intended purpose of the “widget” array key?
Drupal Learner avatar
gf flag
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.
cn flag
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
Drupal Learner avatar
gf flag
@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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.