16 - June - 2009

Preventing Admin Footer Output in Drupal AJAX Calls

Post by Mike C
Ixis Service

Some administration and debugging modules for Drupal attach their statistics to the end of Drupal rendered pages using PHP's register_shutdown_function function facility. This is great to ensure the output is always available, no matter how badly built the theme being used is.

Examples of modules adding their stats to the footer include the superb Devel package which allows outputting statistics on your database queries (or "Gunk" as its called in the source code), and the Memcache Admin UI module which provides information about the memcache bin usage.

The problem arises when you're using Drupal to output AJAX responses which contain plain text. If your Drupal generated response doesn't have a suitable HTTP header defined for it, then both Devel and Memcache tag on their output to your AJAX response and cause the client-side JavaScript to be very confused, or just fail.

The simple solution is to set a HTTP Header in your custom module before the output. Headers which are recognised by Devel and Memcache vary. Devel recognises
'xml', 'javascript', 'json', 'plain', 'image', 'application', 'csv', and 'x-comma-separated-values', whilst Memcache picks up on 'xml', 'javascript', and 'plain'.

Using the Drupal API add the following before you print your output:
drupal_set_header('Content-Type: text/plain; charset=utf-8');

The Devel module also has an extra switch you can set using $GLOBALS['devel_shutdown'] = FALSE to suppress the output, however that doesn't cover the Memcache case.

Mike C

Managing Director

12 years of Drupal development wrangling and a background in digital project architecture.

Comments

Spot on! Saved my headache getting any worse with my custom AJAX callbacks being meddled with by the devel module. Thanks for the explanation just at the right time!

Ian M

Worked for me. Thanx

Janar

Even better use the "delivery callback" attribute in your menu_hook() items to change the standard delivery callback to ajax_deliver().

Bernhard Fürst

In reply to by Bernhard Fürst

Add new comment

Share this article

Sign up to our newsletter!

Our thoughts

Let's work together

Get in touch and find out how we can empower your organisation.
Back to top