Since the webform module checks to prevent the use of duplicate component fieldnames, it would be nice if there were a built-in lookup function as part of the API

// **BEFORE**
function hook_webform_submission_presave($node, &$submission) {
  // Update some component's value before it is saved.
  $component_id = 4;
  $submission->data[$component_id]['value'][0] = 'foo';
}
// **AFTER**
function hook_webform_submission_presave($node, &$submission) {
  // Update some component's value before it is saved.
  $component_id = webform_get_cid_from_fieldname($nid, 'customer_email');
  $submission->data[$component_id]['value'][0] = 'foo@drupal.org';
}

The benefit here is that it is easy to remember the fieldnames you used when you created the webform,
but it is not always easy to remember or keep track of cid values.

Comments

quicksketch’s picture

Form keys are not actually unique in Webform. They are merely uniquer *per-fieldset*. This makes it so you can have a fieldset for "billing" and a fieldset for "shipping" for example, with "address", "street", etc. underneath each one.

So the proposed method probably wouldn't work, it would need to be a look-up based on the parent-tree, such as webform_get_cid_by_parents($node, array('billing', 'address'));. That said, I'm not sure a function is really necessary, it could also just be an array map, such as $node->webform['map']['billing']['address'] to get you the CID.

stephen.moz’s picture

Thanks for the reply.

stephen.moz’s picture

Status: Active » Closed (fixed)

close