I'm trying to create a webform on my node programmatically so that I can set it up to be used as for a specific scenario. What I wanted to do is have all of the components my client want to appear automatically, but also default to specific settings for user entry, email specific users and validation rules etc, because they are going to have the same setup for each one, and they aren't going to want to write their setup out every single time.
I was using the Webform Default Fields module, but I know that this only works with the components, and not the setup. So I got rid of it and started writing my own version, with big help from this answer to get me as far as I am right now. But I'm struggling to find the correct format of how to write in a conditional statement, or how you would set the settings for the webform.
My current code is as follows:
function mymodule_node_insert($node) {
if ($node->type == 'event') {
_abstract_webform_components($node);
}
}
function _abstract_webform_components($node) {
$components = array(
0 => array(
'name' => 'First name',
'form_key' => 'first_name',
'type' => 'textfield',
'mandatory' => 1,
'weight' => 1,
'pid' => 0,
'extra' => array(
'private' => 0,
),
),
1 => array(
'name' => 'Last name',
'form_key' => 'name',
'type' => 'textfield',
'mandatory' => 1,
'weight' => 2,
'pid' => 0,
'extra' => array(
'private' => 0,
),
),
);
// etc etc
webform_ensure_record($node);
foreach($components as $component) {
$component['nid'] = $node->nid;
$component['extra']['title_display'] = 'inline';
$component['extra']['locked'] = 1;
$node->webform['components'][] = $component;
webform_component_insert($component);
}
// Retrieves information about the author
$author = user_load($node->uid);
// Adds the author of the node as the default email recipient
$email = array(
'nid' => $node->nid,
'email' => $author->mail,
'subject' => 'default',
'from_name' => 'default',
'from_address' => 'default',
'template' => 'default',
'excluded_components' => array(),
);
webform_email_insert($email);
$validation = array(
'validator' => 'max_words',
'action' => 'add',
'nid' => $node->nid,
'rulename' => 'Maximum Abstract Length',
'rule_components' => array(
1 => '1',
),
'data' => 300,
);
webform_validation_rule_save($validation);
}
Basically I'm missing the correct notation for how you add a condition to the webform but I have the right command to insert it. Also, I have the notation for how I want to setup my webform settings, but I don't know how to apply it
Many Thanks
EDIT
I've got one half of it, I've managed to add the conditional statement into the code, I was actually using the wrong code, but I managed to find a proper example of how the array needs to be written, edited the code above