In the function webform_component_clone, the cid variable is unset, but never actually reset. This causes Undefined index: cid errors when attempting to clone components using other modules (such as web form add more (uses hook_webform_component_presave)).

To circumvent this error, I set the component['cid'] index to be blank and then modified webform_component_insert to use

<?php
 
if ($component['cid']=="") {
?>

instead of

<?php
 
if (!isset($component['cid'])) {
?>

I then reassigned the cid value to the $new_cid variable.

I chose to empty the value of the index instead of unsetting it because I still want the cid index to be available within the array. Unsetting appears to cause the index to be destroyed, thereby making it unavailable unless it's pushed into the array again.

<?php
function webform_component_clone(&$node, &$component) {
 
$original_cid = $component['cid'];
 
//unset($component['cid']);
 
$component['cid'] = "";
 
$new_cid = webform_component_insert($component);
 
$component['cid'] = $new_cid;
  if (
webform_component_feature($component['type'], 'group')) {
    foreach (
$node->webform['components'] as $cid => $child_component) {
      if (
$child_component['pid'] == $original_cid) {
       
$child_component['pid'] = $new_cid;
       
webform_component_clone($node, $child_component);
      }
    }
  }
  return
$new_cid;
}
?>

Comments

quicksketch’s picture

Could you provide your changes as a patch instead of code samples? That makes the changes easier to review for me.

I chose to empty the value of the index instead of unsetting it because I still want the cid index to be available within the array. Unsetting appears to cause the index to be destroyed, thereby making it unavailable unless it's pushed into the array again.

I'd suggest setting $component['cid'] = NULL, then the index "cid" will exist but isset($component['cid']) will still be FALSE.

cbrasfield’s picture

StatusFileSize
new1.55 KB

First patch, let's see how it goes...

quicksketch’s picture

Status:Active» Fixed
StatusFileSize
new929 bytes

Thanks that patch helped quite a bit. There were some unrelated changes (commenting out lines of code that needed to be there, or debugging code) that I omitted from the patch, but that definitely helped clarify things for me. I committed this patch to both 3.x branches which I think does just what you're looking for.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.