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, i think).

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?

Comments

Rob_Feature’s picture

Title: Programmatically Add Conditional Element » Programmatically Add Conditionals
DanChadwick’s picture

Status: Active » Fixed

We don't provide programming support in the webform issue queue, and writing directly to webform's tables may cause compatibility problems (or worse) in the future.

I do realize there is a performance issue with creating many conditionals. I would suggest that someone devote some attention to fixing the root issue.

Rob_Feature’s picture

Status: Fixed » Closed (won't fix)

That's unfortunate as this kind of info would probably be helpful to a bunch of devs here on drupal.org. Marking as 'won't fix' since nothing was actually answered/fixed.

I've posted the question over at StackExchange for anyone looking for an answer: http://drupal.stackexchange.com/questions/145079/when-programmatically-a...