我正在创建一个包含 4 个字段的自定义表单,并且我想创建一个具有一些 CCK 字段的特定内容类型的节点。
我打算在提交此表单时以编程方式创建节点。我计划为某些字段推送默认值,并且某些字段将使用此表单小部件映射..
这是我的代码
<?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");
}
}
现在,当我提交此内容时,它正在创建内容类型,其中文本字段文本作为标题和正文,我打算这样做......但我想将 UID(登录用户 ID)存储在表节点的 uid 列中...我已经尝试过,如您所见...但它仍然发送 0...我需要帮助...请帮助