13

I'm making an AJAX call in drupal 7 via the standard AJAX Framework. My problem is that when a status message is set by the AJAX callback, it is simply lost (or displayed at the next reload of the page). How can I display the status messages right after the AJAX callback has finished running, without reloading the page? Is there a module for that?

7 Answers 7

15

You can render the status messages and send them as anohter AJAX command.

For example:

$commands = array();

// Add other commands

$commands[] = ajax_command_prepend('div#ajax-status-messages-wrapper', theme('status_messages'));

return array('#type' => 'ajax', '#commands' => $commands);

At least this is the way I solved this problem when I faced it.

2
  • What is the use of theme('status_messages') in there? Commented Jul 27, 2015 at 1:38
  • @Bebang Bakikang render the status messages, returning HTML code with the current status messages.
    – sanzante
    Commented Nov 10, 2015 at 15:16
10

For Drupal 8 it's

$response = new AjaxResponse();    
$status_messages = array('#type' => 'status_messages');
$messages = \Drupal::service('renderer')->renderRoot($status_messages);
if (!empty($messages)) {
  $response->addCommand(new PrependCommand('.your_selector', $messages));
}

return $response;
4

For Drupal 8 form with AJAX, answer by Tim Bozeman worked, but messages were showed up with no styling. This is what worked for me:

$response = new AjaxResponse();
drupal_set_message($action);
$form['messages']['status'] = [
  '#type' => 'status_messages',
];
$response->addCommand(new InsertCommand(null, $form['messages']));

return $response;
1

it's now much more easier :

$response = new AjaxResponse();
$response->addCommand(new MessageCommand('Example of status message.'));

Get all information on this article: https://www.drupal.org/node/3086403

0

for me

$commands[] = ajax_command_remove('div.messages');
$commands[] = ajax_command_after('#main-content', theme('status_messages'));

worked. where #main-content is standard, and maybe needs to be customized for your theme's acutal messages position. (maybe you have to change the second method to ajax_command_append() or another)

0

I had this originally:

\Drupal::messenger()->addMessage($this->t('Form Submitted Successfully'), 'status', TRUE);
$allMessages = \Drupal::messenger()->all();
$html = \Drupal::service('renderer')->renderRoot($allMessages);
$response->addCommand(new AfterCommand('div[data-drupal-messages-fallback]', $html));

But $html was always "". After reading answer from Tim, I added this:

$allMessages['#type'] = 'status_messages';

after retrieving all the messages.. and it worked. Odd that ->all() wouldn't include that.

0

If you're here because in Drupal 8 / 9 you're trying to clear validation error messages upon successful form submission, here's what worked for me (with thanks to rémy for the Drupal 7 inspiration):

// Remove any validation errors from previous submission.
$response = new AjaxResponse();
$selector = '.messages--error';
$response->addCommand(new RemoveCommand($selector));

return $response;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.