Drupal Experts As we have covered wide range of projects for various industries our professionals are experienced and keep good knowledge of online environment. We are always open to handle challenging projects and to provide ultimate solution to our customers. Open source web development projects are one of our specialties in the range of customized services we provide to our clients.
Friday, 25 May 2012
Ajax without Javascript in drupal
Ajax is nothing new. And especially since Drupal’s adoption of jQuery, ajax has certainly become much easier. Generally, ajax requests in Drupal involve
PHP code (usually in a module) to generate the necessary markup for the originating page.
Javascript code to handle the ajax response.
Some form of initialization code, usually in a Drupal behavior, to connect the markup to the behaviors.
PHP to generate the response, both for the ajax request and for the
non-javascript page. (Oh, and it’s up to you to decide how to tell the
difference!)
Let the Chaos begin
There’s nothing inherently wrong with the above method, but there’s definitely room for improvement. With the introduction of CTools (a.k.a Chaos tool suite), ajax development has become much simpler. From the CTools project page on Drupal.org,
“AJAX responder — tools to make it easier for the server to handle AJAX requests and tell the client what to do with them.”
CTools introduces the concept of an ajax command. A command is a
javascript function within the Drupal.CTools.AJAX.commands namespace,
which can be invoked as an ajax response. The server-side callback
returns an object representation of a command, and this object contains
everything necessary to run the command on the client-side. The easiest
way to explain this is with an example.
We will be building an example module, which I will call “example”.
This module will display a page with an ajax link that will reveal more
content when clicked.
The first thing that is needed is a hook_menu() implementation to
define two new paths. The first is the page that will hold the link, and
the second defines the ajax callback. Take note of the %ctools_js in
the second entry. This is how we will determine if the call is being
made from an ajax call or not. More on that when we get to the callback
code.
No comments:
Post a Comment