0

I have two different kinds of webforms, one I'll call "type 1" and one I'll call "type 2". Depending on the type of webform, I want to conditionally add either type1.js or type2.js. The type of webform is has a hidden field called formtype that an admin would set either to "type1" or "type2," and then, theoretically, I would pull in the proper Javascript based on that value when the form loads.

My hook_form_alter() function looks like this:

<?php
/**
 * Implements hook_form_alter().
 */
function module_name_form_alter(&$form, $form_state, $form_id) {
  if($form['submitted']['formtype']['#default_value'] == 'type1'){
    drupal_add_js($js_path . 'type1.js');
  }
  elseif($form['submitted']['formtype']['#default_value'] == 'type2'){
    drupal_add_js($js_path . 'type2.js');
  }
}
?>

This does actually cause the proper javascript to load, but I get a Drupal message saying:

Undefined index: submitted (etc.)

My question is: why would I be receiving this error message even though the logic appears to work? I'm also open to other ways of handling this use-case.

FYI, I'm using Drupal 7.

5
  • Generally I think it's bad idea in the first place. Why don't you load your JS always, but only make it do something if there is a value you need?
    – Mołot
    Aug 12 '15 at 14:42
  • I was mostly thinking of it in terms of performance and to save some development time. I've inherited both these javascript files and they're hundreds of lines each, so I'm trying to just incorporate them in a simple way rather than having to gloss thousands of lines to make sure the two are compatible with one another. Aug 12 '15 at 15:31
  • simplicity of devel I'm not going to argue. But in terms of performance unconditional set of scripts is way better for aggregation and caching.
    – Mołot
    Aug 12 '15 at 15:34
  • Thanks for the suggestion. I'll definitely look into what it would take to get them to play nicely together because, as you say, it would be ideal for performance. Aug 12 '15 at 15:54
  • 1
    Ideal is a big word... Test, don't simply believe me :)
    – Mołot
    Aug 12 '15 at 15:59
2

You applied this code for all forms on your site, but only webform has $form['submitted'] element. To fix this issue you may use next code snippet

<?php
/**
 * Implements hook_form_alter().
 */
 function module_name_form_alter(&$form, $form_state, $form_id) {
   if (!empty($form['submitted'])) {
     if ($form['submitted']['formtype']['#default_value'] == 'type1'){
       drupal_add_js($js_path . 'type1.js');
     }
     else if ($form['submitted']['formtype']['#default_value'] == 'type2'){
       drupal_add_js($js_path . 'type2.js');
     }
  }
}
?>
2
  • Aha! So it wasn't the actual webform that was throwing this error, it was the hook_form_alter function running on some other form on the page. Thank you for the information! Aug 12 '15 at 14:15
  • You might want to use hook_form_FORM_ID_alter() instead of the global hook_form_alter()
    – Watergate
    Aug 12 '15 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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