In a few cases, I found that the function webform_conditional_add_js(&$form, $form_state) was run twice for a node. Most surprisingly, I found that the settings were added twice and created an array of objects in the Drupal.settings.webform_conditional.fields JS object.

So,
Drupal.settings.webform_conditional.fields = {
field_a : {...}
field_b : {...}
};

Turned into
Drupal.settings.webform_conditional.fields = {
[0: field_a : {...}, 1: field_a : {...}]
[0: field_b : {...}, 1: field_b : {...}]
};

The settings are added via this line of code:
drupal_add_js(array('webform_conditional' => $form['#webform_conditional_js']), "setting");

And it is possible for that line of code to be run twice for the same node, resulting in the mess we see above.

Although the cases may be rare (in my case I am rendering a teaser and a full node on the same page, and the teaser is set to display the webform fields) it could save someone a lot of grief if a check were added at the beginning of that function.

I propose introducing a static $nids_processed variable within the function to ensure the same node is not processed twice.


/**
 * Function run #after_build on webform.  This ensures that Javascript is added even if form is built from cache.
 */
function webform_conditional_add_js(&$form, $form_state) {
  static $nids_processed = array();
  $current_nid = $form['#webform_conditional_js']['nid'];
  //If this node has already been processed, abort.
  if ($nids_processed[$current_nid]) {
    return $form;
  }
  //Otherwise, add the current node ID to the list of $nids_processed
  $nids_processed[$current_nid] = TRUE;
  
  //And continue
  drupal_add_js(array('webform_conditional' => $form['#webform_conditional_js']), "setting");
  drupal_add_js(drupal_get_path('module', 'webform_conditional') . '/webform_conditional.js');
  return $form;
}

Patch attached. Hope this helps!

CommentFileSizeAuthor
webform_conditional.module.patch826 bytesMac Clemmens

Comments

tedbow’s picture

Status: Active » Postponed (maintainer needs more info)

@Mac Clemmens, thanks for the patch. I see how the patch would fix the described problem but it seems like when you have the same form twice on a page the javascript won't actually work to show and hide components in both instances form. Do both forms work in your case with the patch?