Drupal的6 / jQuery的阿贾克斯更新场新场、Drupal、jQuery、阿贾克斯更

2023-09-10 13:22:02 作者:丢了薇笑ヽ拿什么伪装

我对在同一地点的不同路径,我需要允许用户更改节点个字段的内容,他/她在不同的位置中写道。我有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 textarea的。至于为什么,以及有一个中心节点类型,有很多链接的节点参考节点。从它引用该中央节点的任何节点的编辑页,它需要能够编辑并保存中央节点的字段。所以这是我的用例。

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,我真的真的AP preciate你的帮助。

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?
}

您也提到了这一点:

Also you mentioned this:

无论是在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?

第四步: 我想我明白居多,因为其他的事情,虽然我无法测试它。 :)

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

我真的想学习如何做到这一点在Drupal,但它会膨胀,如果你能提高你的语言有点dumminess水平。 :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);

    	});
    });
}

我看到一个贴子的网址与正确的数据。这是很好的。但没有任何反应。警报是空的。

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.

更新了code来说明如何我会做到这一点。这code几乎可以被复制到您的Drupal项目,但我没有实战考验,所以有可能是这里一个错字或出现的错误。

设置一个网址,你可以张贴到使用 hook_menu()。您需要使用回调类型。什么,你需要记住的是某种形式的访问控制添加到您的菜单项。如果你没有任何人可以访问它,即使用户1,因为没有访问控制正在取得。在这种情况下,你应该使用的接入参数,并把用户需要有烫发的名称。您可以使用一个已经在不同的模块存在,或者您也可以创建自己的使用 hook_perm 。你需要确保你的用户有正确的烫发让他们能够利用这一点。这是通过Drupal的人工智能正常完成。

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;

创建你所指定的回调函数,这是当有人访问该网址将要运行的功能。 一个simplyfied版本看起来是这样的。您需要验证数据和存储节点之前做的东西一样。您可能还希望做一个权限检查。

从Web技术看开源是否有利可图

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有一个按钮和一个文本框做你的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.