Drupal & AJAX, the new way
In Finnish Drupalcamp 2010, I gave a session about the CTool's AJAX Framework. The slides and a couple of demo modules are attached, and I explain more details in a short article, since the slides are summarized.
AJAX (Asynchronous JavaScript & XML) is nothing new nowadays. Several Javascript frameworks like jQuery have made it easier to make AJAX calls. You don't have to worry about handling the low level XMLHttpRequest object (XHR) anymore.
Drupal and AJAX
Drupal and AJAX jQuery got into Drupal 6 core in 2007, so you can use its AJAX API. But still, you have to make some repetitive stuff like taking care of errors, provide an accessible alternative for browsers without Javascript enabled, tell the user that the AJAX request is processing, etc. Here enters CTools.
In 2008 Earl Miles, author of Views and Panels, introduced the CTools module. This contrib module consists of a set of developers’ tools and APIs that you can use in your own modules. One of these tools is an AJAX Framework that recently got into Drupal 7 core and provides some basic AJAX functionality to your modules without writing a single line of Javascript.
It's as easy as writing into your module an AJAX commands in PHP. CTools will take care of communicating to the browser what needs to be done (inserting that content, replacing it, changing CSS, etc). The default commands should cover the basic needs, but if you need some extra functionality, you can create your own commands. In the demo attached, I created an example of a new command "drupro_demo2_effect_command" that will take 2 extra arguments, 'effect' and 'speed' to make insertions.
Benefits
Summarizing, the main benefits of using the CTools approach instead of writing your own jQuery's AJAX code are:
- It's part of Drupal 7 core, so expect higher security, support and documentation. That presumably will be back ported to CTools for Drupal 6.
- Less code to write and maintain.
- The AJAX framework takes care of:
- Informing user that AJAX call is being requested (throbber icon).
- Attaching/detaching Drupal.behaviors.
- Errors handling.
- And it provides an easy and standard way to implement graceful degradation based on URL parameters.
- Basic commands are provided and they are are extensible.
Main drawback of this approach is the lack of control on the Javascript side and the AJAX call itself, but definitely is the way to go if you need something fast and simple.
| Attachment | Size |
|---|---|
| DCFI_2010_code.zip | 162.74 KB |
Feed yourself
Propaganda on your favourite news reader
See us on facebook
Big up to all our fans!
Spread the love
- Drupal & AJAX, the new way, June 29, 2010
- Drupro develops a web store for Ekahau, March 23, 2010
- We Are Hiring: PHP/Drupal Developers-To-Be, April 20, 2010

Thanks for posting the samples as I am really struggling to understand the javascript APIs of CTools. However, can you please tell me how do I accomplish the following? I would like to show a dynamic tooltip whenenver somebody hovers over a link with a user name. It should grab the content from a url and show in the tooltip. The content will have some text and actionable buttons. Also, I should be able to position and skin the tooltip and control the nature of the tooltip like whether it goes away when I click outside, how long it shows, whats the delay of showing it etc.
Can you please help me? Kindly mail me a copy at chandrabhan.dutta (at) gmal.com
thanks in advance.
Hi chandrabhan,
The CTools framework is limited to make AJAX calls when they are triggered by clicks on links (a elements with ctools css class). By your description it seems you will need to trigger AJAX on mouseover and mouseout events.
So if you need greater control in the Javascript side and manipulating the DOM, I recommend you to go one step down and make AJAX calls based on JQuery API.
I would use user_load function inside the AJAX callback to get whatever information you need from the user http://api.drupal.org/api/function/user_load
And this is not Drupal-specific but it can help you with the Javascript coding http://rndnext.blogspot.com/2009/02/jquery-ajax-tooltip.html
Hope it helps you
Thanks for the links, David. I will give it a shot.
Post new comment