18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10<br />

Once the response is set up it is inserted into a renderable array. The type is<br />

ajax_commands, which will know how to render the AJAX command that was created<br />

in the callback. To send the AJAX response properly ajax_deliver() is used. This<br />

function properly formats the response for the JavaScript on the receiving side.<br />

Additionally, <strong>Drupal</strong> tracks the JavaScript and CSS files within a page. If a new<br />

file is added within an AJAX request that is not already loaded in the page the new<br />

file is sent as part of the response and added to the page along with the rest of<br />

the response.<br />

For cases when JavaScript is not available in the initial page view and the link is<br />

followed, it is treated as a full page request and the user is sent to a new page. This<br />

page lives at the same callback that built the AJAX response. The difference is nojs is<br />

passed into the callback so it knows the response is not in AJAX. In this case the else<br />

is executed generating a different message for the new page.<br />

AJAX commands<br />

<strong>Drupal</strong> provides several AJAX commands that can add or alter content in a page<br />

using jQuery methods. In the previous section we covered ajax_command_append().<br />

Here are all the possible commands that can be used.<br />

ajax_command_after<br />

When ajax_command_after() is used, jQuery.after() is the method used to<br />

add content to the page. The arguments are $selector, $content, and $settings.<br />

$selector is the jQuery selector on the page and $content is the content to add<br />

after the selector. $settings, the third argument, is a set of settings used by the<br />

behaviors for this single command.<br />

ajax_command_alert<br />

The alert command is a core JavaScript command. ajax_command_alert($text) is<br />

used to alert returned text. The text is presented in an alert box.<br />

ajax_command_append<br />

This command is similar to ajax_command_after. Instead of adding the content<br />

after the selector it appends it to the end of the selector. The interface to the function<br />

is the same as ajax_command_after() with the $selector, $content, and<br />

$settings arguments. $selector is the jQuery selector on the page, $content is<br />

the content to append to the selector, and $settings is used by behavior for just<br />

this one command.<br />

[ 309 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!