[英]Drupal 6/jQuery Ajax update a field
我在同一個站點上的路徑不同,我需要允許用戶更改他/她在不同位置寫入的節點上的字段內容。 我有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中的功能與在步驟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);
});
});
}
我在網址上看到了一個帶有正確數據的帖子。 這很好。 但沒有回應。 警報為空。
還有一個新的空白...已經創建了一些東西。 它沒有任何內容,但我可以在過濾節點時在視圖中看到它。 它沒有標題,任何字段等。只是一個發布日期。
我想要更新的節點未更新。
所以這讓我認為第二步可能有點不正確。 我有幾個問題。
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。
設置一個你可以使用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;
創建一個您指定的回調函數,這是一個訪問該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); }
添加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'); } }
如果你有一個按鈕和一個文本字段,使用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. } }); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.