Score:0

hook_form_alter makes js on every other form not work

zw flag

I use hook_form_alter() to alter a field.

function mymodule_form_alter(&$form, &$form_state) {
  if ($form['#id'] == 'my_form') {
    $form['my_field']['#value'] = 'New value';
  }
}

It changes the field value to new value, but the problem is that, as soon as I enable this module, the JavaScript on forms stops working. For example drop-downs, autocomplete, AJAX no longer works.

I cant understand what cause this.

cn flag
Try changing the theme temporarily (to any of the themes included with core that you are not already using) and see if the error still occurs.
Jaypan avatar
de flag
You are misdiagnosing your problem, as this code would not cause the issue you are facing.
No Sssweat avatar
ua flag
Check the browser's console for JS errors.
apaderno avatar
us flag
@Jaypan is correct: That code could not cause issues with JavaScript code. You don't get the form ID from `$form['#id']`, but that is another matter.
apaderno avatar
us flag
Also, before setting `$form['my_field']['#value']`, you should check `$form['my_field']` has been set.
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.