This Drupal example is working with the 'virtual' paths that are not actual nodes,
and when I add a node/page with the same fake path 'vms-custom-path/trigger-page
but with an existing named node/page like my_drupal_site/test-ajax-page and adding $items['my-ajax-page'] = array()
never seems to get called.
//PHP Module
/// path to our REAL page with a menu block on it in Drupal site
// first to load the js file only on this page
$items['test-ajax-page'] = array(
'title' => 'My custom ajax callback',
'page callback' => 'addCustomJS',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
// then to register the actual ajax call back
$items['test-ajax-page/ajax'] = array(
'title' => 'My custom ajax callback',
'page callback' => 'My_ajax_callback',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
I need to use AJAX to update a Drupal page when it loads with data look ups from the server. Most examples use a click or other form event, this for a menu page without any forms and has to happen when the page loads without any user events.
I found a deeson-online's example using Drupal.ajax.prototype.specifiedResponse but it is throwing AJAX errors.
How can I run Drupal AJAX with jQuery when the page finishes loading?
Here's the basic version that did work with the Drupal AJAX Object approach - when I have the jQuery version, as Austin suggested, working for our situation I'll add that.
//PHP Module
// Returns AJAX commands if the URL has 'ajax' in it
$items['My-custom-path/ajax'] = array(
'title' => 'My custom ajax callback',
'page callback' => 'My_ajax_callback',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
/**
* AJAX callback that prints text and link ID. ID will be used for looking up additional data.
*/
function My_ajax_callback() {
// Define a new array to hold our Drupal AJAX commands.
$ajax_commands = array();
// Create a new AJAX commands that will be run in the Browser
//In this case arg(3) is the Node ID in the URL passed back
if(is_numeric(arg(3)) ){
$ajax_commands[] = ajax_command_after('#'.arg(3), 'Hello '. '#'.arg(3) . '!!!');
$ajax_commands[] = ajax_command_append('#'.arg(3), 'Updated');
$ajax_commands[] = ajax_command_append('#'.arg(3), arg(3));
}
else {
//return;
$ajax_commands[] = ajax_command_after('#'.arg(3), 'No data available.');
}
// Return our commands in JSON.
return drupal_json_output($ajax_commands);
}
// JavaScript
(function ($) {
Drupal.behaviors.My_ajax = {
attach: function(context, settings) {
var $MyAjaxLink = $('.My-ajax-link');
// Add IDs to the Menu links to query them on the server
$('div.block div.content div.menu-block-wrapper ul.menu li.leaf a').each(function(index, element) {
var url = $(this).attr('href');
var urlArray = url.split("/");
var lastPath = urlArray.length -1;
$(this).attr('id', urlArray[lastPath]);
});
/**
* Add an extra function to the Drupal ajax object
* which allows us to trigger an ajax response without
* an element that triggers it.
* From post on http://deeson-online.co.uk/labs/trigger-drupal-managed-ajax-calls-any-time-drupal-7
*/
Drupal.ajax.prototype.specifiedResponse = function() {
var ajax = this;
// Do not perform another ajax command if one is already in progress.
if (ajax.ajaxing) {
return false;
}
try {
$.ajax(ajax.options);
}
catch (err) {
alert('An error occurred while attempting to process ' + ajax.options.url);
return false;
}
return false;
};
$('div.block div.content div.menu-block-wrapper ul.menu li.leaf a:not(.ajax-processed)').each(function(index, element) {
var $MyAjaxLink = $(this);
// make sure it is a numeric ID
if( !isNaN(this.id) ) {
DrupalAjax = new Drupal.ajax('.My-ajax-link', $MyAjaxLink, {
url: $MyAjaxLink.attr('href'),
settings: {},
event: 'onload'
//event: 'click tap'
});
$(this).addClass('ajax-processed');
// not working yet did loop and throw errors
DrupalAjax.specifiedResponse()
}
});
}
}
})(jQuery);