Drupal 6/jQuery Ajax 更新一个字段 [英] Drupal 6/jQuery Ajax update a field

查看:21
本文介绍了Drupal 6/jQuery Ajax 更新一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm on a different path on the same site, and I need to allow the user to change the contents of a field on a node s/he wrote in a different location. I have the nodeid and the field name, and ids, etc np.

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

I don't believe this is too difficult, but a tutorial or an explanation would be wonderful.

谢谢.

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

Thank you anschauung for asking, so to clarify:

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

It is a CCK textarea. As for why, well there's a central node type, with many linked node reference nodes. From the edit page of any node which references the central node, it needs to be able to edit and save a field of the central node. So that's my usecase.

再次感谢.

非常感谢 googletorp,非常感谢您的帮助.

Thanks you so much googletorp, I really really appreciate your help.

这是我目前所拥有的:

第一步:

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 或其他一些钩子当节点表单为生成.你应该调查哪种方式最适合您的情况.

Either in hook_form_alter, hook_nodeapi or some other hook that is invoked when the node form is generated. You should investigate which is best in your situation.

如何生成节点表单?

第三步:

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 步中获取节点形式之间还有什么?

Also what is in between the function in step 2 and getting the node form in step 3?

第 4 步:我想我大部分都理解了,虽然因为其他事情我还不能测试它.:)

Step 4: I think I understand mostly, though because of other things I can't test it yet. :)

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

I really want to learn how to do this in drupal, but it would be swell if you could increase the dumminess level of your language a bit. :D Thank you so much once again.

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

I actually tried putting access arguments yesterday, but for some reason it did not work. :( But now it does! Yay you have magic.

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

Now, when I trigger the post like this:

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

I see a post to the url with the correct data. Which is good. But no response. The alert is empty.

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

Also a new blank... something has been created. There's nothing in it, but I can see it in views when filtered for nodes. It has no title, any fields, etc. Just a post date.

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

The node that I want to be updated isn't updated.

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

So that leads me to think that the step two is probably somewhat incorrect. I have a few questions about it.

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

再次感谢您.

推荐答案

这很容易完成,但需要几个步骤.

This can be done quite easily, but there are a few steps to it.

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

  1. 使用hook_menu()设置您可以发布的网址.您需要使用 CALLBACK 类型.您需要记住的是向菜单项添加某种访问控制.如果您没有任何人可以访问它,即使是用户 1,因为没有进行访问控制.在这种情况下,您应该使用访问参数,并输入用户需要拥有的权限名称.您可以使用其他模块中已经存在的模块,也可以使用 hook_perm<创建自己的模块/a>.您需要确保您的用户拥有正确的烫发才能使用它.这通常是通过 Drupal AI 完成的.

  1. Setup a url you can post to using hook_menu(). You need to use the CALLBACK type. What you need to remember is to add some sort of access control to your menu item. If you don't no one can visit it, even user 1 since no access control is being made. In this case you should use the access arguments, and put the name of the perm that the user needs to have. You can use one that already exist in a different module, or you can create your own using hook_perm. You will need to make sure your users have the correct perm for them to be able to use this. This is done normally through the 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;

  • 创建一个你指定的回调函数,这个函数会在有人访问url时运行.
    一个简单的版本看起来像这样.在保存节点之前,您需要验证数据并执行类似操作.您可能还想进行权限检查.

  • Create a callback function that you specified, this is the function that will be run when some one visits the url.
    A simplyfied version looks like this. You would need to validate the data and do stuff like that before saving the node. You might also want to do a permission check.

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

  • 添加js文件,这可以通过drupal_add_js(),您可能需要查看 drupal_get_path() 以创建正确的路径为您的 js 文件.有一些不同的方法可以添加 js 文件.在 hook_form_alter、hook_nodeapi 或其他一些在生成节点表单时调用的钩子.您应该调查哪种情况最适合您的情况.如果你使用 hook_form_alter 它看起来像这样:

  • Add the js file, this can be done with drupal_add_js(), you might want to look into drupal_get_path() to create the correct path for your js file. There are some different ways you can add the js file. Either in hook_form_alter, hook_nodeapi or some other hook that is invoked when the node form is generated. You should investigate which is best in your situation. If you go with hook_form_alter it would look something like this:

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

  • 用 jQuery 做你的 javascript 东西,如果你有一个按钮和一个文本字段,它可能看起来像这样:

  • Do your javascript stuff using jQuery this could look something like this, if you fx had a button and a textfield:

    $("#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.
            }
        });
    });
    

  • 在您的回调函数中,您处理将在 $_POST 中的数据,并且您应该通过返回 json 数据来结束您的函数,您的 js 可以根据这些数据采取行动让用户知道发生了什么.drupal_json 可用于此目的.
  • In your callback function you handle the data which will be in $_POST, and you should end your function by return json data, that your js can act upon to let the user know what happened. drupal_json can be used for this.
  • 这篇关于Drupal 6/jQuery Ajax 更新一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆