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

查看:121
本文介绍了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.

以下是我到目前为止:

对于第一步: / p>

For step one:

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,因为没有访问控制。在这种情况下,您应该使用访问参数,并输入用户需要具有的perm名称。您可以使用不同模块中已存在的模块,也可以使用 hook_perm 。您将需要确保您的用户具有正确的烫发才能使用。这通常通过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;


  • 创建一个您指定的回调函数,这是一些函数,当一些一个访问网址。

    一个简单的版本看起来像这样。在保存节点之前,您需要验证数据并执行相应的操作。您可能还需要进行权限检查。

  • 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看起来像这样,如果你的fx有一个按钮和一个文本框:

  • 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天全站免登陆