Drupal 6 / jQuery Ajax更新字段

我在同一个站点上的路径不同,我需要允许用户更改他/她在不同位置写入的节点上的字段内容。 我有nodeid和字段名称,以及ids等np。

我不相信这太难了,但教程或解释会很精彩。

谢谢。

编辑:谢谢anschauung的询问,所以澄清一下:

这是CCK textarea。 至于为什么,有一个中心节点类型,有许多链接节点参考节点。 从引用中心节点的任何节点的编辑页面,它需要能够编辑和保存中心节点的字段。 这就是我的用例。

再次感谢。

非常感谢googletorp,我非常感谢你的帮助。

这是我到目前为止所拥有的:

对于第一步:

function update_main_field_menu() { $items = array(); $items['update_main_field/%'] = array( 'title' => 'Update Main Field', 'page callback' => 'post_to_main_node', 'page arguments' => 1, 'type' => MENU_CALLBACK ); return $items; } 

第二步:

 function post_to_main_node(){ // Sorry, I'm totally lost. What do I put here? } 

你也提到了这个:

在hook_form_alter,hook_nodeapi或生成节点表单时调用的其他一些钩子中。 您应该调查哪种情况最适合您的情况。

如何生成节点表单?

第三步:

 function modulename_form_mainct???_node_form_alter (&$form, &$form_state) { // I'm not sure about which form I'm doing node form alter on. If I do it to the mainct, wouldn't that alter the regular edit page the user is viewing? I only want to load the js for the ajax submission. Is there a update_main_field node form? drupal_add_js(drupal_get_path('module', 'modulename') ."/updateField.js"); } 

此外,步骤2中的function与步骤3中的节点forms之间的区别是什么?

第4步:我认为我主要理解,但由于其他事情,我还无法测试它。 🙂

我真的想学习如何在drupal中做到这一点,但如果你可以稍微增加你的语言的笨拙程度,它会膨胀。 :D再次感谢你。


再次编辑:

我实际上昨天试过把访问参数,但由于某种原因它没有用。 :(但现在确实如此!你有魔法吗?

现在,当我触发这样的post时:

 Drupal.behaviors.ajax_update_field = function (context) { $("#button").click(function(){ var url = $("#edit-field-reference-0-nid-nid").val().replace(/.*?\[nid:(\d+)?]/ig, "$1"); url = "/update_main_field/"+url; // The data is just some silly test thing $.post(url, {data: $("#edit-field-reference-0-nid-nid-wrapper label").text()}, function(value) { // Here you can write your js to handle a response to the user, // or if something went wrong an error message. // value = response data from drupal alert(value); }); }); } 

我在url上看到了一个带有正确数据的post。 这很好。 但没有回应。 警报为空。

还有一个新的空白…已经创建了一些东西。 它没有任何内容,但我可以在为节点过滤时在视图中看到它。 它没有标题,任何字段等。只是一个发布日期。

我想要更新的节点未更新。

所以这让我认为第二步可能有点不正确。 我有几个问题。

 function post_to_main_node(){ // Is this sufficient to load the node? nid doesn't have to be set as an arg for the function? $node = node_load($_POST['nid']); // Is the field set like this? 'field_library' is the 'machine name' of the field. This is what's needed right? $node->field_library = $_POST['data']; node_save($node); } 

再次感谢你。

这可以很容易地完成,但它有几个步骤。

更新了代码以显示我将如何执行此操作。 这个代码几乎可以复制到你的drupal项目中,但是我还没有对它进行过测试,所以这里可能会有拼写错误或者有bug。

  1. 设置一个你可以发布到使用hook_menu()的url。 您需要使用CALLBACK类型。 您需要记住的是为菜单项添加某种访问控制。 如果你不这样做,那么即使用户1也没有人可以访问它,因为没有进行访问控制。 在这种情况下,您应该使用访问参数,并输入用户需要具有的perm的名称。 您可以使用已存在于不同模块中的模块,也可以使用hook_perm创建自己的模块 。 您需要确保您的用户拥有正确的烫发权,以便他们能够使用此权限。 这通常是通过Drupal AI完成的。

     function modulename_menu() { $items = array(); $items['update_main_field'] = array( 'page callback' => 'call_back', 'type' => MENU_CALLBACK, 'access arguments' => array('name of perm'), ); return $items; 
  2. 创建一个您指定的回调函数,这是一个访问该URL时将运行的函数。
    一个简单的版本看起来像这样。 在保存节点之前,您需要validation数据并执行类似的操作。 您可能还想进行权限检查。

     function call_back() { $result = array(); // Here we check the date get the node, update the cck field and save it. $node = isset($_POST['nid']) ? node_load($_POST['nid']) : FALSE; // $node will be false if nid wasn't set or nid was invalid. if (!$node || !isset($_POST['text']); { $result['status'] = 'error'; $result['message'] = t('error message'); } // Check if the loaded node have the correct type so it will have the field we want. else if ($node->type != 'node_type') { $result['status'] = 'error'; $result['message'] = t('error message'); } else { $node->field = $_POST['text']; node_save($node); $result['status'] = 'success'; $result['message'] = t('success message'); } return drupal_json($result); } 
  3. 添加js文件,可以使用drupal_add_js()来完成,您可能需要查看drupal_get_path()来为js文件创建正确的路径。 您可以通过一些不同的方式添加js文件。 在hook_form_alter,hook_nodeapi或生成节点表单时调用的其他一些钩子中。 您应该调查哪种情况最适合您的情况。 如果你使用hook_form_alter,它看起来像这样:

     modulename_form_alter(&$form, &$form_state, $form_id){ // Add js to the desired node form. if ($form_id == 'content_type_name_node_form') { drupal_add_js(drupal_get_path('module', 'modulename') . '/script.js'); } } 
  4. 如果你有一个按钮和一个文本字段,使用jQuery做你的javascript东西看起来像这样:

     $("#button_id#").click(function(){ var nid = $("#edit-field-reference-0-nid-nid").val().replace(/.*?\[nid:(\d+)?]/ig, "$1"); var text = $("#edit-field-reference-0-nid-nid-wrapper label").text(); $.post("/update_main_field", {"nid": nid, "text", text}, function(data) { // This is basically how jQuery converts response data to json var json = eval("(" + data + ")"); if (json['status'] == "error") { // Handle error cases. } else if (json['status'] == "success") { // Handle the success case. } }); }); 
  5. 在你的回调函数中,你处理将在$ _POST中的数据,你应该通过返回json数据结束你的函数,你的js可以采取行动让用户知道发生了什么。 drupal_json可以用于此。