简体   繁体   English

Drupal 7 - 通过ajax / frontpage更新节点字段

[英]Drupal 7 - Update node fields via ajax/frontpage

I have a few jQueryUI draggable objects that represent nodes generated on my front page in Drupal. 我有一些jQueryUI可拖动对象,表示在我的头版在Drupal中生成的节点。

I want to grab when a user drops an element and save the x/y coordinates to the server so when the next user opens the page it will still be where it was last left at. 我想在用户删除元素并将x / y坐标保存到服务器时抓取,这样当下一个用户打开页面时,它仍然是最后一次离开的位置。

I created two integer fields, homex and homey, but I can't seem to figure out or find enough documentation to learn how to tell Drupal to update the values for a given node. 我创建了两个整数字段,homex和homey,但我似乎无法弄清楚或找到足够的文档来学习如何告诉Drupal更新给定节点的值。

I'm fairly familiar with how to create modules in Drupal, and ajax in general - but combining the two in this case is perplexing me. 我对如何在Drupal和ajax中创建模块非常熟悉 - 但在这种情况下将两者结合起来让我感到困惑。

Can someone help me understand how to attach to Drupal so I can save the coordinates dynamically? 有人可以帮助我了解如何附加到Drupal,以便我可以动态保存坐标吗?

What would be preferable is if I could just write a simple handler module for Drupal that takes the x/y pair in a get/post request then updates them in the database and responds with a success/json. 更可取的是,如果我能为Drupal编写一个简单的处理程序模块,它在get / post请求中获取x / y对,然后在数据库中更新它们并以success / json响应。 Really if it wasn't being done in Drupal this would be a fairly simple setup. 真的,如果没有在Drupal中完成,这将是一个相当简单的设置。

It seems all I had to do was create a hook_menu() and a hook_ajax_callback() (sorry couldn't find a link) in a module. 我似乎只需要在模块中创建一个hook_menu()和一个hook_ajax_callback()(抱歉找不到链接)。

Here's what I ended up with (more less, leaving in three different return methods I was playing with): 这是我最终得到的结果(更不用说,留下我正在玩的三种不同的回归方法):

<?php

function homepage_coords_menu(){
    return array(//$items
        'homepage_coords/%node/%/%' => array(
            'page callback' => 'homepage_coords_ajax_callback',
            'page arguments' => array(1,2,3),
            'access callback' => TRUE,
            'type' => MENU_CALLBACK,
        )
    );
}

function homepage_coords_ajax_callback($node,$x=0,$y=0){    
    if(!is_numeric($x) || !is_numeric($y)){
        ajax_deliver(json_encode(array(
            'status'=>'fail'
        )));            
    }

    $node->field_homepagex = array('und'=>array(array('value'=>$x)));
    $node->field_homepagey = array('und'=>array(array('value'=>$y)));

    node_save($node);

    ajax_deliver(json_encode(array(
        'status'=>'win'
    )));
}

?>

When you say that you are familiar with ajax, you mean jquery's ajax or Drupal 7 ajax framework. 当你说你熟悉ajax时,你的意思是jquery的ajax或Drupal 7 ajax框架。 You can read more about the Drupal 7 Ajax here http://drupal.org/node/752056 您可以在http://drupal.org/node/752056上阅读有关Drupal 7 Ajax的更多信息

I suppose homex is a hidden form element. 我想homex是一个隐藏的表单元素。 Maybe you could hook_form_alter it, adding a #ajax attribute with an ajax callback triggered by a "change" event for example or any other Jquery event , and in that ajax callback, execute node_form_submit_build_node 也许你可以hook_form_alter它,添加#ajax属性,例如由“change”事件或任何其他Jquery事件触发的ajax回调,并在该ajax回调中,执行node_form_submit_build_node

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM