0

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.

5
  • 2
    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. Commented Apr 22, 2022 at 16:42
  • 3
    You are misdiagnosing your problem, as this code would not cause the issue you are facing.
    – Jaypan
    Commented Apr 22, 2022 at 23:56
  • 1
    Check the browser's console for JS errors.
    – No Sssweat
    Commented Apr 23, 2022 at 1:39
  • @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.
    – avpaderno
    Commented Apr 24, 2022 at 8:41
  • 1
    Also, before setting $form['my_field']['#value'], you should check $form['my_field'] has been set.
    – avpaderno
    Commented Apr 24, 2022 at 8:42

0

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.