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'
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);
}
_add_webform_components()
after node_save($node)
to avoid errors.
Commented
Feb 7, 2013 at 15:41
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;
}
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.