Drupal 7 API: Display Drupal Messages in/after an AJAX call

Sometimes you need a snippet to display the drupal messages (drupal_set_message) created messages after an ajax call as result (for example if a node was created via ajax).

The following snippet removes old messages from the site and appends new messages in the AJAX callback:

  1. function ..._ajax_callback(){
  2.   $commands = array();
  3.   $commands[] = ajax_command_remove('div.messages');
  4.   $commands[] = ajax_command_after('#your-container-id', theme('status_messages'));
  5.   return array('#type' => 'ajax', '#commands' => $commands);
  6. }

This snippet was inspired by this discussion:
https://www.drupal.org/node/1028410

Comments

PS: The best solution for

PS: The best solution for AJAXified entity forms (nodes, ...) is this, because it already handles several typical problems out of the box:

https://www.drupal.org/project/ajax_form_entity

Post new comment

The content of this field is kept private and will not be shown publicly.

More information about formatting options