1
votes

I have a few jQueryUI draggable objects that represent nodes generated on my front page in 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.

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.

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.

Can someone help me understand how to attach to Drupal so I can save the coordinates dynamically?

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. Really if it wasn't being done in Drupal this would be a fairly simple setup.

2

2 Answers

3
votes

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.

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

?>
1
votes

When you say that you are familiar with ajax, you mean jquery's ajax or Drupal 7 ajax framework. You can read more about the Drupal 7 Ajax here http://drupal.org/node/752056

I suppose homex is a hidden form element. 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