我的网站(D6)有"快速查询表格"(使用网络表格)&当用户点击webform中的提交按钮时,我需要同时创建一个新节点。
当用户点击提交按钮时,如何从网络表单获取值并将值插入节点。
请建议我怎么做!!
答案 0 :(得分:3)
您可以通过创建自定义模块来实现此目的。该模块将具有两个功能:
在创建模块之前,您应该使用核心节点模块和CCK来创建包含与webform相同的所有字段的内容类型。
在下面的示例中,在开关案例中替换模块名称fir MODULENAME和XXX的webform的ID。此函数将MODULENAME_create_node添加到webform的提交函数数组中。我们将在下面定义MODULENAME_create_node。
<?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;
}
}
这是创建节点的主要功能。
<?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;
}
您还需要为模块创建信息文件。如果您不熟悉,请参阅Drupal在writing info files上的文档。
答案 1 :(得分:2)
Bala - 您需要将表单和form_state传递给自定义函数,如下所示:
<?php
function MYMODULENAME_create_node($form, $form_state) {
// ... above code
}
否则$ form_state变量为空,将抛出您遇到的错误。