0

I'm trying to add a new conditional attribute to a bunch of webforms using a script. There are already 12 conditions, and I need to add a 13th one.

My code looks like this in a basic script module which has $node available:

if(stuff_is_true){
 $new_component = array(
  'nid' => $node->nid,
  'rgid' => '13',
  'andor' => NULL,
  'action' => 'hide',
  'target_type' => 'component',
  'target' => '22',
  'weight' => '13',
  'rules' => array(
    '0' => array(
      'nid' => $node->nid,
      'rgid' => '13',
      'rid' => '0',
      'source_type' => 'component',
      'source' => '21',
      'operator' => 'equal',
      'value' => '0',
    ),
  ),
);

$node->webform['conditionals'][] = $new_component;

// Save the changes
node_save($node);

All of this works if put into a hook_node_view() (without doing a node_save obviously), so I know it's structured properly.

The problem comes during node_save($node) which throws an error like:

Duplicate entry '3033-0' for key 'PRIMARY': INSERT INTO {webform_conditional}...

(3033 is the nid, and the 0 is the rgid key).

Basically, what it's doing (I think) is trying to re-submit the whole $node->webform['conditionals'] array as NEW data, so existing components hit this "duplicate primary key" error (because they already exist).

How can I tell node_save to only ADD my new conditional item to the end of the existing array instead of trying to re-insert the whole thing?

1 Answer 1

0

It seems as if there's really no clean way to do this. What I realized is that it was easier to just remove all existing conditions, and then re-add them all with the additional condition included. So I started by setting:

$node->webform['conditionals'] = array(); //remove all conditions
node_save($node); //save the node, removing all old conditions
$node->webform['conditionals'] = $new_array_of_conditions; //add entire new array of condtions
node_save($node); //save the node with new conditions

This doesn't break anything on old nodes because no data or anything else is reliant upon conditions...so removing them doesn't hurt anything or lose any crucial data.

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.