I recently needed to update a panel pane via ajax after a user made changes in a modal form.
After figuring out how to programmactically update a panel pane, I needed to replace a pane's HTML with the results from a AJAX call.
Outside of Drupal, making AJAX calls to return a small set of HTML is pretty simple. I wanted to avoid side-stepping Drupal's AJAX Framework.
The major part here is the use of ajax_deliver for the "delivery callback".
This will return the AJAX command results instead of a rendered page.
/** * Implements hook_menu(). */ function trigger_ajax_menu() { 'page callback' => 'trigger_ajax_update', 'access callback' => TRUE, 'delivery callback' => 'ajax_deliver', 'theme callback' => 'ajax_base_page_theme', 'type' => MENU_CALLBACK, ), ); } function trigger_ajax_update($uid) { $account = user_load($uid); $content = '<div id="target-element-update">Hello ' . $account->name . '!</div>'; '#type' => 'ajax', ajax_command_replace('#target-element-update', $content) ), ); }
Drupal's AJAX requires binding to page element so I'm just using "body".
(function ($) { $(function () { /** * Create a placeholder function that we can execute to trigger an AJAX call. */ Drupal.ajax.prototype.triggerAjax = function (uid) { // Leave if we're already executing. if (this.ajaxing) { return false; } // Append the User ID to the end of the URL to complete our menu entry. this.options.url += uid; $.ajax(this.options); }; Drupal.ajax['triggerObjectPlaceholder'] = new Drupal.ajax( null, $(document.body), { url: '/custom-ajax/', event: 'onload', keypress: false, prevent: false }); // Some event // ... // Trigger the manual ajax trigger var userId = 1; Drupal.ajax['triggerObjectPlaceholder'].triggerAjax(userId); }); })(jQuery);
Software engineer by profession, embedded systems tinkerer, husband, father, fantasy novel devourer, wine lush, beer and cheese connoisseur