4

I have already created a webform and I need to fill it programatically.

how can I create webform ? Can i create it like node creation:

$node = new stdClass()
$node->type = 'webform'
$node->webform['components'][1]['value'] =  'sss'

3 Answers 3

7

See code below:

function _add_webform_components($node) {
  $components = array(
    0 => array(
      'name' => 'First name',
      'form_key' => 'first_name',
      'type' => 'textfield',
      'mandatory' => 1,
      'weight' => 1,
      'pid' => 0
    ),
    1 => array(
      'name' => 'Name',
      'form_key' => 'name',
      'type' => 'textfield',
      'mandatory' => 1,
      'weight' => 2,
      'pid' => 0
    ),
    ... and so on

  );

  webform_ensure_record($node);
  foreach($components as $component) {
    $component['nid'] = $node->nid;
    $component['extra']['title_display'] = 'inline';
    $node->webform['components'][] = $component;
    webform_component_insert($component);
  }
  $email = array(
    'nid' => $node->nid,
    'email' => '[email protected]',
    'subject' => 'default',
    'from_name' => 'default',
    'from_address' => 'default',
    'template' => 'default',
    'excluded_components' => array(),
  );
  webform_email_insert($email);
}
5
  • thax very much mr.lenni, my question now is how i can migrate data was interred in drupal 6 webform form to drupal 7 webform? i can access the drupal 6 database and get all the data, but my problem is i cant add the old data to drupal 7 webform and submit it programatically? i hope that my question is clear!
    – genaro
    Commented Aug 14, 2012 at 12:05
  • @genaro, if this is your requirement, edit the question accordingly.
    – niksmac
    Commented Aug 14, 2012 at 14:25
  • This is working splendidly for me in Drupal 7. Since I am using it within a script that generates new nodes I had to move the call to _add_webform_components() after node_save($node) to avoid errors. Commented Feb 7, 2013 at 15:41
  • This looks like it will come in very handy for me, is there a specific function that will allow me to change the settings of the webform? ie, redirect, confirmation message etc? Commented Jul 9, 2013 at 16:03
  • Getting issue "Integrity constraint violation: 1048 Column 'machine_name' cannot be null" and I found one more key need to add in components array 'machine_name' => $node->title . '__first_name' Commented Jul 19, 2019 at 14:34
0

Maybe it is easier to understand with this example:

function hook_node_insert($node) {
  if ($node->type == 'your_content_type') {
    webform_ensure_record($node);
    $components = _my_function_webform_components();
    foreach($components as $component) {
      $component['nid'] = $node->nid;
      $component['extra']['title_display'] = 'inline';
      $node->webform['components'][] = $component;
      webform_component_insert($component);
    }
    $email = array(
      'nid' => $node->nid,
      'email' => '[email protected]',
      'subject' => 'default',
      'from_name' => 'default',
      'from_address' => 'default',
      'template' => 'default',
      'excluded_components' => array(),
    );
    webform_email_insert($email);
  }
}

function _my_function_webform_components() {
  $components = array(
    0 => array(
      'name' => 'Name',
      'form_key' => 'name',
      'type' => 'textfield',
      'mandatory' => 1,
      'width' => '40',
      'maxlength' => '75',
      'weight' => 1,
      'pid' => 0
    ),
    1 => array(
      'name' => 'Age',
      'form_key' => 'age',
      'type' => 'number',
      'mandatory' => 1,
      'max' => '99',
      'excludezero' => 1,
      'weight' => 2,
      'pid' => 0
    ),
    ...
  );

  return $components;

}
-3

You can make use of drupal_form_submit I didn't tried it.

Anyway this may can help you.

This function allows you to supply values for form elements and submit a form for processing.

The idea is to find the form_id check for the form fields and

// register a new user
$form_state = array();
$form_state['values']['xxx'] = 'yyyyy';
drupal_form_submit('xxx', $form_state);

Give a try and please paste the code here if you succeed.

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.