I have a Webform in drupal 7. I want to add a hidden field porgramatically to it, and I would like to set the value of this field per form. I want to achieve all these in a custom submit function, as I want to use the sid (serial ID) of the Submission, to generate a unique ID for each Submission. At the moment everything is working, except to set the value. I googled it a lot, tried out hundreds of codes but non of them worked in my case.

Here is my code:

$form['#submit'][] = 'webform_custom_submit';

function webform_custom_submit($form, &$form_state) {
  if ( !array_key_exists('my_uniq_id', $form['#node']->webform['cids'] ) ){
    $components = array(
      0 => array(
        'name' => 'My Unique ID',
        'form_key' => 'my_unique_id',
        'type' => 'hidden',
        'weight' => 1,
        'pid' => 0
    foreach ($components as $component) {
      $component['nid'] = $form['#node']->nid;
      $form['#node']->webform['components'][] = $component;
  $my_uniqe_key = generate_some_unique_key_function();
  //Here I wan't to set the value of the component 'my unique ID', but nothing worked yet
  $form_state['my_unique_id']['#value'] = $my_uniqe_key;

Thanks for the help, I'm sure I could find the answer somewhere, but I tried it 3hours long...

Here is the $form and $form_state written out with dpm from my webform_custom_submit function.

And here are some codes, I tried already:

$form['values']['my_unique_id'] = $my_uniqe_key;
$form['values']['my_unique_id']['#value'] = $my_uniqe_key;
$form['values']['my_unique_id']['value'] = $my_uniqe_key;
$form['submitted']['my_unique_id']['#value'] = $my_uniqe_key;
$form['my_unique_id']['und']['0']['value']['#value'] = $my_uniqe_key;
$form_state['values']['my_unique_id'] = $my_uniqe_key;
$form_state['values']['my_unique_id']['#value'] = $my_uniqe_key;
$form_state['values']['my_unique_id']['value'] = $my_uniqe_key;
$form_state['my_unique_id']['und']['0']['value']['#value'] = $my_uniqe_key;
$form_state['values']['my_unique_id'][LANGUAGE_NONE][0]['value'] = $my_uniqe_key;
drupal_array_set_nested_value($form, array('submitted', 'my_unique_id', '#value'), $my_uniqe_key;);
form_set_value( array('submitted','my_unique_id'), array(0 => array('value' => $my_uniqe_key;)), $form_state) ;

$cid = $form['#node']->webform['cids']['my_unique_id'];
$form_state['values']['submitted'][$cid] = "static text";
  • Hi there, try to set the value using $form_state['values'] rather than $form itself Oct 1, 2017 at 13:41
  • Hi @AlirezaTabatabaeian, I tried it...
    – Koli
    Oct 1, 2017 at 17:15
  • I would believe Alireza's suggestion above about using $form_state['values'] is correct. What happens when you try setting the form state value to something static? Oct 1, 2017 at 18:19
  • @TreyeDesigns: If I try e.g.: $form_state['values']['submitted'][$cid] = "static", nothing happens.
    – Koli
    Oct 6, 2017 at 8:33

3 Answers 3


You should use the presave hook provided by webform instead of adding additional submit handlers to the form.

 * Modify a Webform submission, prior to saving it in the database.
 * @param $node
 *   The Webform node on which this submission was made.
 * @param $submission
 *   The Webform submission that is about to be saved to the database.
function hook_webform_submission_presave($node, &$submission) {
  // Update some component's value before it is saved.
  $component_id = 4;
  $submission->data[$component_id][0] = 'foo';

This is a lot cleaner than adding custom submit handlers to the form (your custom handler is called after the webform submission handler, which can be a big part of the problem).

You have access to the node object in the presave hook so additional checks can be added beforre altering the component.

  • I use the custom submit handler, as I want also to redirect my clients to an other website, where they can pay.
    – Koli
    Oct 2, 2017 at 17:08

I think you should set your value in $form_state['values']['submitted']:

$form_state['values']['submitted'][$cid] = $my_uniqe_key;

Here $cid is the component ID, which is the key of the component info in $form['#node']->webform['components'].

  • I also tried that one, but did not worked. Is it because I try to set a hidden field? Or because, I do it in a custom submit function?
    – Koli
    Oct 1, 2017 at 19:55
  • It worked for me in a custom validate function. I just realized you wanted to set a value per form. Then why not use the default value? When you add the field set the default value. Oct 2, 2017 at 10:20
  • But I add the field only by the first filling of the form. Later it's already added to the form, so the code will not run. Then by every form the same value would be.
    – Koli
    Oct 2, 2017 at 17:10

Finally I was able to solve my problem with the help of this answer to an other question. Here is the part of my code from my_custom_submit function, which updates hidden field:

module_load_include('inc', 'webform', 'includes/webform.submissions');
$node = node_load($form_state['storage']['details']['nid']);
$submission = webform_get_submission($node->nid, $form_state['storage']['details']['sid']);
foreach ($form['#node']->webform['components'] as $key => $component) {
  if ( $component['form_key'] == 'my_unique_id' ){
    $submission->data[$key][0] = $my_uniqe_key;
webform_submission_update($node, $submission);

I tried all your suggestions, but non of them worked for me. Thanks for all your efforts!

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.