4

I am working on a project and I think I am stuck on this. I have a form that I want to add points to a map as users edit it.

Here's a rundown on how the form works:

The user chooses a number (1-10) for how many stops they'd like to make from a select list. The select list defaults to 1 and below it there is a select list that lists all of the points of interest they can choose from. If they change the first select to 2 the ajax callback updates the form and adds an additional select box of POIs. That's working great.

What I'd like to be able to do is add the POI to a map on the page whenever they select it (with driving directions between them). I have an ajax callback on the POI select list in order to do this but I am having trouble figuring out where to go from here.

I have the following in my POI callback (for testing) and it works, but I am having trouble figuring out what ajax_command_ function to use to call a js function to add the point to the map.

$commands = array();
$commands[] = ajax_command_alert("Here!");
return array('#type' => 'ajax', '#commands' => $commands);

Basically I just need to pass the latitude and longitude of the POI (which I am gathering in the callback) and send it to a js function to then update the map. My first thought was using drupal_add_js but I couldn't get it working and discovered later that it was not the right way to go.

Any thoughts?

1
  • custom ajax commands. let me search on google and i'll post a real answer Commented Aug 31, 2012 at 21:29

1 Answer 1

5

PHP:

$commands[] = ajax_command_alert("Here!");
$commands[] = array(
  "command" => 'your_custom_command',
  "latitude" => "45.23",
  "longitude" => "23.25"
);

Javascript:

Drupal.ajax.prototype.commands.your_custom_command = function(ajax, response, status) {
  if( response.latitude != "" ) {
    // do something
  }
};
2
  • 2
    How about some explanation? Context? References for further documentation?
    – Caleb
    Commented Sep 1, 2012 at 8:46
  • This is actually the correct answer. The PHP above is what goes in the function referred to by #ajax['callback']. The javascript is extending Drupal's built in commands to add a new one. The response parameter holds the data passed in the php array above.
    – gregghz
    Commented Nov 17, 2012 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.