I need a text field populated automatically with a random number that can not be modified by the user. It is created after the form was submitted the first time. I can use the computed field for nodes or profiles but not in the web form. I have used a custom webform module to populate select lists but not text fields. How can I achieve this?

Comments

luthien’s picture

How to set up the value for a non-editable Text field, using a custom module, but do it only after the form was submitted the first time? Using form_alter only works for setting up the value before the user submits the form.

mymodule.info

name = Webform Custom Code
description = Customizations for the Webform module.
core = 7.x
package = Webform
dependencies[] = webform

mymodule.module

/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  // set field only for my form
  if ($form_id == 'webform_client_form_id') {
    // edit component values here
   $form['submitted']['mytextfield']['#value'] = '12345';
  }
}

Using hook_webform_submission_presave() was the solution.

mymodule.module

<?php
/**
* Implements  hook_webform_submission_presave().
*/
function mymodule_webform_submission_presave($node, &$submission) {
  // process only for a specific web form, you need the webform node id
  if ($node->nid == your-webform-node-id-here) {
    // find component id
    $component_id = 4;
   // only add new value if field is not empty 
   if (empty($submission->data[$component_id]['value'][0])) {
    $submission->data[$component_id]['value'][0]= "foo";
   }
  }
}
?>
luthien’s picture

Status: Active » Closed (fixed)