Drupal 7以编程方式将数据提交给webform

时间:2017-02-26 02:57:51

标签: webforms drupal-7 submission programmatically-created

我在Drupal网站上有一个webform,我需要使用以下代码以编程方式发布数据:

// Get the node ID of the created Webform and load the node.
$node = node_load($nid);

global $user;
$uid  = $user->uid;

// Include files.
module_load_include('inc', 'webform', 'webform.module');  
module_load_include('inc', 'webform', 'includes/webform.submissions');

// Prepare the data for submission.
$data = array(
    2 => array('value' => array('question_id')),
    1 => array('value' => array('quiz_name')),
    3 => array('value' => array('your_feedback')),
);

$submission = (object) array(
    'nid' => $nid,
    'uid' => $user->uid,
    'submitted' => REQUEST_TIME,
    'remote_addr' => ip_address(),
    'is_draft' => FALSE,
    'data' => $data,
);

print_r($submission);

$sid = webform_submission_insert($node, $submission);

return "Submission {$sid} received!";

问题是提交已创建,但它完全是空的i:e $data数组未在提交中表示。

1 个答案:

答案 0 :(得分:0)

global $user;
$nid = 4; //nid is the node id of your webform.

$node = node_load($nid); 

// The values to save. Take case about array indexes! (see below)
$data = array(
    '1' => array('0' => $type),
    '2' => array('0' => $method),
    '5' => array('0' => $volume),
    '6' => array('0' => $comment),
    '7' => array('0' => $phone),
    '8' => array('0' => $length)
);

$submission = (object) array(
    'nid' => $nid,
    'uid' => $user->uid,
    'submitted' => REQUEST_TIME,
    'remote_addr' => ip_address(),
    'is_draft' => FALSE,
    'data' => $data,
);

// Include the necessary files.
module_load_include('inc', 'webform', 'webform.module');
module_load_include('inc', 'webform', 'includes/webform.submissions');
webform_submission_insert($node, $submission); // Submit a submission.
webform_submission_send_mail($node, $submission); // Send webform's e-mails

$type$method$volume等只是必须写入Webform字段的变量。数组索引是Webform的字段索引。

如何获取索引并找出哪个在哪里?

  1. 转到您的Webform组件页面(例如/node/4/webform where'4'是一个Webform nid)。
  2. 将鼠标悬停在“编辑”字段上,然后查看链接源(href)。应该是这样的:http://[site-name]/node/4/webform/components/2?destination=node/4/webform,其中“ 2 ”是该字段的索引。
  3. 对所有必填字段执行相同操作。
  4. 万一遇到麻烦,请查看webform_submitted_data DB表。它具有以下字段:
    • nid(网络表单ID),
    • sid(提交ID)
    • cid(字段索引!),
    • 否(不知道,也许是多值字段的增量),
    • 数据(存储的字段数据)。