Drupal 6: Creating form to create node..!

856 views Asked by At

I am creating a custom form with 4 fields & I want to create a node of a particular content type, which is having some CCK fields.

I am intended to create node pro-grammatically on submission of this form. I have plan to push default values for some fields and some fields will be mapped with this form widgets..

Here is my code

 <?php

require 'modules/node/node.pages.inc';

/**
 * Implements hook_menu().
 */

function taskform_menu() {
  $items = array();
  $items['admin/content/taskform'] = array(
    'title' => 'Add Task',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('taskform_form'),
    'access arguments' => array('create create_task content'),
  ); 
  return $items;
}
 global $user;

function taskform_perm() {
  return array('Submit daily task');
}

function taskform_form(&$node)
{


  $form['date'] = array(
    '#type' => 'date', 
    '#title' => t('Date'),        
  );

  $form['edproject'] = array(
    '#type' => 'select', 
    '#title' => t('Project'), 

    '#options' => array(
      1 => 'Konnected', 
      2 => 'eLearning', 
      3 => 'Others',
    ),
    '#description' => t('Choose a project'),
  );

  $form['task'] = array(
    '#type' => 'textfield', 
    '#title' => t('Task'), 
    '#size' => 30,
    '#required' => TRUE,
    '#maxlength' => 30,
    '#description' => t('Enter the task'),
  );

  $form['remarks'] = array(
    '#type' => 'textfield', 
    '#title' => t('Remarks'), 
    '#size' => 30,
    '#description' => t('Enter remarks (If any).'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add Task'),
    '#submit' => array('taskform_form_submit'),
  );
  return $form;

}

function taskform_form_submit($form, &$form_state) {

  $node = new stdClass();
  $node->type = 'create_task';
  $node->uid = $user->uid;
  $node->title = $form_state['values']['task'];
  $node->body = $form_state['values']['task'];
  $node->status = 1;
  $node->promote = 0;
  $node->field_assigned_uid[]['uid'] = $user->uid;
  node_object_prepare($node);
    $node = node_submit($node);
    if ($node->validated) {
        node_save($node);
    }
    else{
        t("Node not created");
    }


}

Now when I am submitting this, It's creating the content type with the text-field text as title & body which I am intended to do...but I want to store UID(logged in user ID) in uid column of the table node...and I have tried doing it as you can see...but still it's sending 0... I need help on this...please help

2

There are 2 answers

1
AKS On BEST ANSWER

IMO, it's not a too good idea to make a node for each webform submission. Lame. You can always tailor node form to do what webform does so it's a straight forward setup. Also, you can access each submission's data easily, and webform already has Views integration so I don't know why you need to create a node.

However, if you still need to go ahead, the best way I'd suggest is using webform's new hooks (since Webform 3). See hook_webform_submission_insert

<?php
function MYMODULE_webform_submission_insert($node, $submission) {
// print the submitted values object's information as a message.
// Once you have grabbed the necessary data, remove this line. 
drupal_set_message('<pre>'.print_r($submission, TRUE).'</pre>');

// Now, grab the fields you want and map them to the $node object below.
$node = new stdClass();
$node->title   = 'Webform submission: '$submission->sid;
$node->body    = 'test body';
$node->type    = 'story';
$node->created = time();
$node->status  = 1; //published.
$node->promote = 1; 
$node->sticky  = 0;
$node->format  = 1;
$node->uid     = 1; // author!

if ($node = node_submit($node)) {
  node_save($node);
}
else {
  // error creating node. 
}
}
2
TelFiRE On

Is there a particular reason you need to use webform? It seems like you'd have more control and less issues by creating a specific content type for this and giving permission to anonymous users to submit it. Then, you can get a module that provides field-level permissions and deny access to the fields you don't want them touching (making sure they aren't required).

Am I missing something from what you need?