【问题标题】:Drupal 6/jQuery Ajax update a fieldDrupal 6/jQuery Ajax 更新字段
【发布时间】:2010-12-12 20:29:44
【问题描述】:

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

我不认为这太难,但是有教程或解释会很棒。

谢谢。

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

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

再次感谢。

非常感谢 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 步中的函数和第 3 步中获取节点形式之间还有什么关系?

第 4 步: 我想我主要了解,但由于其他事情我还不能测试它。 :)

我真的很想学习如何在 drupal 中做到这一点,但是如果你能稍微提高一下你的语言的愚蠢程度,那就太好了。 :D 再次感谢你。


再次编辑:

我昨天实际上尝试放置访问参数,但由于某种原因它不起作用。 :( 但现在确实如此!是的,你有魔法。

现在,当我像这样触发帖子时:

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 帖子。哪个好。但没有回应。警报为空。

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

我要更新的节点没有更新。

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

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);
}

再次感谢您。

【问题讨论】:

  • 能否提供更多细节?例如,您能否描述您正在更改的字段、在什么情况下以及为什么?解释可能会非常非常不同,具体取决于您是否正在更改 CCK 字段、核心字段等。
  • 感谢 anschauung,我编辑了问题。
  • 我想我理解了这个问题,但是 jQuery 是从哪里来的呢?
  • 我以为你会使用 jQuery 来进行 ajax 提交,因为 Drupal 打包了它?
  • 它不起作用,因为您只是复制了我的插图代码,而不是将其用作指南。通读我的答案并查找您使用的功能,您拥有所需的一切,只需付出一些努力,您就会弄清楚。

标签: jquery ajax drupal field


【解决方案1】:

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

更新了代码以显示我将如何执行此操作。这段代码几乎可以复制到你的drupal项目中,但我还没有经过实战测试,所以这里可能有错字或那里的错误。

  1. 设置一个您可以使用hook_menu() 发布到的网址。您需要使用 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 时将运行的函数。
    一个简单的版本看起来像这样。在保存节点之前,您需要验证数据并执行类似的操作。您可能还想进行权限检查。

    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. 如果你的 fx 有一个按钮和一个文本字段,那么使用 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 可以用于此。

【讨论】:

  • 非常感谢。我在问题中回答了。
  • 感谢更新说明!我创建了一个模块并使用一些示例数据进行了尝试。我在萤火虫中看到了一个帖子,但是 403 被禁止。当我尝试手动导航到 url 时,我也被拒绝访问。 (全部与 user1)。你知道有什么问题吗?谢谢。
  • 再次感谢您。我用另一个编辑更新了我的问题。非常感谢您的帮助。
  • 谢谢,但我真的很困惑。为什么我需要检查日期?如何获取节点?与 hook_perm 页面或类似页面不同,node_load api 页面没有示例。搜索 $_post 时,我也没有看到任何结果。 api.drupal.org/api/search/6/%24_post 。是否有一个链接可以解释这意味着什么以及正确的语法是什么?
  • $_POST 是一个 php 超级全局,所以你不会在 drupal api 文档中找到它,这里是它的链接 php.net/manual/en/reserved.variables.post.php 。前几行很好地解释了如何使用 node_load: 从数据库中加载一个节点对象。参数 $param 节点的 nid 或数据库查询中要匹配的条件数组。如果您没有获得预期的数据,您需要检查您的数据,以确保事情不会出错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多