0

I have created a webform hook (using hook_form_alter) which searches for a particularly named input and appends an ID for JS code to make changes on the front end.

It works fine however I have come across an issue where if that input is made part of a fieldset, it can no longer find it. This is to do with how the $form array is rearranged to include children inputs in the fieldset array.

My question is: how do I find an element by name regardless of whether it is part of a fieldset or not?

This is the code I'm using, which works fine for parent level inputs but not when it is part of a fieldset. How do I get this to work where I don't need to know what the fieldset is called (because I won't always know).

function MODULE_form_alter(&$form, &$form_state, $form_id){
  $form['submitted']["certain_name"]['#default_value'] = '';
  $form['submitted']["certain_name"]['#attributes']['id'] = 'new-id';
  $form['submitted']["certain_name"]['#ajax'] = array(
    'event'    => 'change',
    'callback' => 'module_js_lookup(this.value)'
  );
}

1 Answer 1

0

Unfortunately, to access/modify and form fields in Webform using the form API and hooks, you have to know/include the fieldset hierarchies in the array calls as you observed.

For example:

$form['submitted']["fieldset_machine_name"]["certain_name"]

or

$form['submitted']["outer_fieldset_machine_name"]["inner_fieldset_machine_name"]["certain_name"]

Rather than set a callback function on the form element in your hook_form_alter, you may just need to find the element type with particular machine name using vanilla javascript or jQuery in either:

  • your theme's assorted javascript files as Drupal behavior
  • the custom module as a separate script

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.