My website(D6) have "Quick Enquiry Form"(using webform) & i need to create a new node simultaneously when user click the submit button in webform.
How do i get values from the webform and insert the values to the node when user click the submit button.
Please suggest me how to do this!!
You can accomplish this by creating a custom module. The module will have two functions:
Before you create the module, you should use the core node module and CCK to create a content type that contains all of the same fields as the webform.
In the example below, substitue the name of your module fir MODULENAME and the ID of your webform for XXX in the switch case. This function adds MODULENAME_create_node to your webform's submit function array. We will define MODULENAME_create_node below.
<?php
function MODULENAME_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'webform_client_form_XXX' :
$first = array_shift($form['#submit']);
array_unshift($form['#submit'], $first, 'MODULENAME_create_node');
break;
}
}
This is the main function that will create the node.
<?php
function MODULENAME_create_node() {
// Load all of the data submitted via the webform into a keyed array ($data)
$data = array();
foreach ($form_state['values']['submitted_tree'] as $key => $value) {
$data[$key] = $value;
}
// The node_save() function (called later in this function) call
// node_access_check, which means that anonymous calls to this function will
// not be successful. Top get around this, we load user1 while executing this
// function, then restore the user back to the original state at the end of
// the function.
global $user;
$original_user = $user;
$user = user_load(1);
// Initialize the new node with default stuff
$node = new stdClass();
$node->type = 'YOUR_CONTENT_TYPE';
$node->created = time();
$node->changed = $node->created;
$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->format = 1;
$node->uid = $user->uid;
// You'll need to customize this based on what you named your webform and CCK fields.
// Remember that all of the webform data is available and stored in the $data array.
$node->title = $data['title'];
$node->field_myfield1[0]['value'] = $data['myfield1'];
$node->field_myfield2[0]['value'] = $data['myfield2'];
//Save the node
node_save($node);
//Set the user state back to the original
$user = $original_user;
}
You'll also need to create an info file for your module. See Drupal's documentation on writing info files if you're not familiar.
Bala - You need to pass the form and form_state to the custom function like so:
<?php
function MYMODULENAME_create_node($form, $form_state) {
// ... above code
}
Otherwise the $form_state variable is empty and will throw the error you are experiencing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With