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:
<?php
$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
)
);
webform_ensure_record($form['#node']);
foreach ($components as $component) {
$component['nid'] = $form['#node']->nid;
$form['#node']->webform['components'][] = $component;
webform_component_insert($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:
<?php
$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";