Tried this on a clean 7.21 install with webform 7.x-4.0-alpha6 and 7.x-3.18
When I write a function to create a node from a webform-enabled node type ( say, 'page' ), the webform components are not initialized properly: they are not displayed when viewing the node.
The component editor is also empty, but if I add a component, it yields this error:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3-1' for key 'PRIMARY': INSERT INTO {webform_roles} (nid, rid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1); Array ( [:db_insert_placeholder_0] => 3 [:db_insert_placeholder_1] => 1 ) in webform_node_insert() (line 1135 of /usr/www/users/name/sites/all/modules/webform/webform.module).
However, if I change the node type to 'webform' in my function, everything works.
Below is a sample implementation that creates a node of type 'page' and attaches a webform to it.
//assemble basic webform node
$node = new stdClass();
$node->type = 'page'; //change to webform to work
node_object_prepare($node);
$node->title = 'Example webform';
$node->language = 'en';
node_save($node);
//if node creation was successful, create webform
if ($nid = $node->nid) {
//define webform settings
$node->webform = array();
$node->webform['nid'] = $nid;
$node->webform['confirmation'] = '';
$node->webform['confirmation_format'] = 'full_html';
$node->webform['redirect_url'] = '<none>';
$node->webform['status'] = '1';
$node->webform['block'] = '0';
$node->webform['teaser'] = '0';
$node->webform['allow_draft'] = '0';
$node->webform['auto_save'] = '0';
$node->webform['submit_notice'] = '0';
$node->webform['submit_text'] = 'Test';
$node->webform['submit_limit'] = '-1';
$node->webform['submit_interval'] = '-1';
$node->webform['total_submit_limit'] = '-1';
$node->webform['total_submit_interval'] = '-1';
$node->webform['record_exists'] = true;
$node->webform['roles'] = array('1');
//define webform components
$node->webform['components'] = array();
$node->webform['emails'] = array();
$node->webform['conditionals'] = array();
$cid = 1;
//define example textfield
$node->webform['components'][$cid] = array();
$node->webform['components'][$cid]['nid'] = $nid;
$node->webform['components'][$cid]['cid'] = '' . $cid;
$node->webform['components'][$cid]['pid'] = '0';
$node->webform['components'][$cid]['form_key'] = 'example_field';
$node->webform['components'][$cid]['name'] = 'Example field';
$node->webform['components'][$cid]['type'] = 'textfield';
$node->webform['components'][$cid]['value'] = '';
$node->webform['components'][$cid]['mandatory'] = '0';
$node->webform['components'][$cid]['weight'] = '' . $cid;
$node->webform['components'][$cid]['page_num'] = 1;
$node->webform['components'][$cid]['extra'] = array();
$node->webform['components'][$cid]['extra']['title_display'] = 'before';
$node->webform['components'][$cid]['extra']['private'] = 0;
$node->webform['components'][$cid]['extra']['disabled'] = 0;
$node->webform['components'][$cid]['extra']['unique'] = 0;
$node->webform['components'][$cid]['extra']['width'] = '';
$node->webform['components'][$cid]['extra']['maxlength'] = '';
$node->webform['components'][$cid]['extra']['field_prefix'] = '';
$node->webform['components'][$cid]['extra']['field_suffix'] = '';
$node->webform['components'][$cid]['extra']['description'] = '';
$node->webform['components'][$cid]['extra']['attributes'] = array();
}
node_save($node);
Comments
Comment #1
Roensby commentednevermind, I overlooked a lot of initialization functions. Here is a better ( although probably incomplete ) snippet