发布到节点 drupal 7
Posted
技术标签:
【中文标题】发布到节点 drupal 7【英文标题】:Post to a node drupal 7 【发布时间】:2017-09-14 04:08:52 【问题描述】:我是 drupal 的新手,正在尝试将数据从表单发布到我的 druple 安装。
我已经设置了我的服务模块并建立了一个基本节点
我的节点
sites/all/modules/custom/bd_profile/bd_profile.module
bd_profile.module
<?php
function bd_profile_services_resources()
return array(
'bd_profile' => array( // My new resource
'create' => array(
'callback' => '_bd_profile_create_node',
'access callback' => '_bd_profile_create_access',
'args' => array(
array(
'name' => 'node',
'optional' => FALSE,
'source' => 'data', // Setting the source to 'data' in your args means that any data in the POST will be passed to the callback function
'description' => 'The node data to create',
'type' => 'array',
),
),
),
),
);
/**
* Access callback
*/
function _bd_profile_create_access()
return TRUE;
/**
* Callback function that creates the node
*/
function _bd_profile_create_node($arg)
// Minimally there needs to be something submitted
if($arg)
// Create the node
$node = new stdClass();
$node->type = 'bio';
$node->title = $arg['name'];
$node->language = LANGUAGE_NONE;
$node->uid = 0;
// $node->uid = $user->uid;
// $node->status = 1; //(1 or 0): published or not
// $node->promote = 0; //(1 or 0): promoted to front page
// $node->comment = 1; // 0 = comments disabled, 1 = read only, 2 = read/write
node_object_prepare($node);
// Create a map of predefined POST args to Drupal fields
$map = array(
'job_title' => 'field_title',
'message' => 'body',
);
// Array to store both mapped and unmapped fields
$node_fields = array();
// What predefined args have been passed?
$arr1 = array_intersect_key($arg, $map);
// Build an array associating Drupal fieldnames to arg values
foreach($arr1 as $key => $value)
$field = $map[$key]; // Get the drupal field that matches the form field
$node_fields[$field] = $value;
// What undefined (ie. unknown) args have been passed?
$arr2 = array_diff_key($arg, $map);
// Associate unknown arg values with the 'general info' field on our bio/profile pages
foreach($arr2 as $key => $value)
if(isset($node_fields['field_general_info']))
$node_fields['field_general_info'] .= $key . " | " . $value . "\n";
else
$node_fields['field_general_info'] = $key . " | " . $value . "\n";
// Save all arg values as Drupal fields
foreach($node_fields AS $key => $value)
$node->$key[$node->language][0]['value'] = $value;
// Save the node
$node = node_submit($node);
node_save($node);
else
// Error if no args were passed
return services_error(t('No data submitted!'), 406, t('No data submitted!'));
这现在在我的服务资源中作为 bd_profile 处于活动状态并创建检查
我到端点的路径:bensapi
接下来我有一个简单的 POST 表单
myform.html
<form action="http://localhost/myapp/bensapi/node" method="post" enctype="multipart/form-data">
<table >
<tr>
<td>Your name:</td><td><input name="name" type="text" /></td>
</tr>
<tr>
<td>Job title:</td><td><input name="job_title" type="text" /></td>
</tr>
<tr>
<td>Age:</td><td><input name="age" type="text" /></td>
</tr>
<tr>
<td>Hometown:</td><td><input name="hometown" type="text" /></td>
</tr>
<tr>
<td>Hometown state:</td><td><input name="state" type="text" /></td>
</tr>
<tr>
<td>Bio/message:</td><td><textarea name="message"></textarea></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Save">
<input type="reset" value="Clear"></td>
</tr>
</table>
</form>
最后我的新内容类型是这样设置的
一旦我发布,我就会得到
<result>Node bd_profile could not be found</result>
我是drupal的新手,不知道我哪里出了问题,任何帮助让它工作都会很好。
谢谢。
【问题讨论】:
【参考方案1】:我没有使用服务,但要创建端点页面,您可以使用 hook_menu:
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7.x
这应该可行:
function bd_profile_menu()
$items['add_node'] = array(
'title' => 'Adding a node',
'page callback' => '_bd_profile_create_node',
'access arguments' => array('access content'),
);
.. 其中“add_node”是您的终点的路径。当然,您可以根据需要设置其他值。
【讨论】:
以上是关于发布到节点 drupal 7的主要内容,如果未能解决你的问题,请参考以下文章
Drupal 6 - “将此字段链接到节点”第一个列表项没有斜体