Created
November 2, 2014 15:12
-
-
Save eiriksm/c9e779b999b3da82158e to your computer and use it in GitHub Desktop.
Programatically creating webforms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_node_insert(). | |
*/ | |
function mymodule_node_insert($node) { | |
// The node type I am using is called 'order'. | |
if ($node->type == 'order') { | |
// I am using the body field for values. | |
$body = field_get_items('node', $node, 'body'); | |
$comp = explode("\n", $body[0]['value']); | |
// $comp now holds an array with all lines in the body field. | |
$i = 1; | |
$components = array(); | |
foreach ($comp as $c) { | |
if ($c == '') { | |
// Skip empty lines. Maybe the non-tech admin pressed an extra enter? | |
continue; | |
} | |
// Create the webform components array. Not sure if we need all these | |
// values, but let's be sure. | |
$components[$i] = array( | |
'cid' => $i, | |
'pid' => '0', | |
// I don't trust the admin to make a key based on input :) | |
'form_key' => 'kitten_' . $i, | |
'name' => $c, | |
// I want all lines to be numeric type component. | |
'type' => 'number', | |
'value' => '', | |
'extra' => array( | |
'title_display' => 'before', | |
'private' => 0, | |
'type' => 'textfield', | |
'decimals' => '', | |
'separator' => ',', | |
'point' => '.', | |
'unique' => 0, | |
'integer' => 0, | |
'conditional_operator' => '=', | |
'excludezero' => 0, | |
'field_prefix' => '', | |
'field_suffix' => '', | |
'description' => '', | |
'attributes' => array(), | |
// The number has to be positive. | |
'min' => '0', | |
'max' => '', | |
'step' => '', | |
'conditional_component' => '', | |
'conditional_values' => '', | |
), | |
'mandatory' => '0', | |
'weight' => $i, | |
'page_num' => 1, | |
); | |
$i++; | |
} | |
// Alright, let's create the node object. | |
$n = new stdClass(); | |
$n->type = 'webform'; | |
// Let the user be the author. | |
$n->uid = $node->uid; | |
// Let the 'order' title be the webform title. | |
$n->title = $node->title; | |
// I put them all on the frontpage. | |
$n->promote = 1; | |
// And we are allowed to comment. | |
$n->comment = 2; | |
// Enter webform array. | |
$n->webform = array( | |
'confirmation' => '', | |
'confirmation_format' => NULL, | |
'redirect_url' => '<confirmation>', | |
'status' => '1', | |
'block' => '0', | |
'teaser' => '0', | |
'allow_draft' => '0', | |
'auto_save' => '0', | |
'submit_notice' => '1', | |
'submit_text' => '', | |
// Users can only submit once how many kittens they want. | |
'submit_limit' => '1', | |
'submit_interval' => '-1', | |
'total_submit_limit' => '-1', | |
'total_submit_interval' => '-1', | |
'record_exists' => TRUE, | |
'roles' => array( | |
// Only authenticated users can submit this webform. | |
0 => '2', | |
), | |
'emails' => array(), | |
// Here comes our $components array. | |
'components' => $components, | |
); | |
// Here i also add the description field to the webform. | |
$description = field_get_items('node', $node, 'field_description'); | |
if (!empty($description[0]['value'])) { | |
// Blah blah. | |
} | |
// Save the node. | |
node_save($n); | |
// You could also put something like drupal_goto('node/' . $n->nid) here if | |
// you want. My use case is different. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment