rockstardev
rockstardev

Reputation: 13537

Forcing "drupal_set_message" messages to appear immediately?

Currently, if the following code is executed:

drupal_set_message("TEST");

Only once a page has refreshed, will the message appear on the page.

I am currently in a situation where an ajax call is executing this method. Therefore, the current page is not changing. So, I need a way to include in my ajax call that after this method has been called, that i must immediately display the error message.

And no, I don't want to just do it manually by setting the "error" div to whatever value I want. I'm using message effects and therefore need to plug into the way drupal does it, so that the messages are shown as Drupal would.

Anyone?

Upvotes: 1

Views: 8524

Answers (3)

joachim
joachim

Reputation: 30831

I think what the question is about is the way that Views shows messages when you preview a view or edit a component. This very usefully allows you to put dsm() statements in code for Views handlers and see output where it's relevant to debugging.

Views does it like this:

if ($messages = theme('status_messages')) {
  $display = '<div class="views-messages">' . $messages . '</div>';
}

Where you print that in your ajax callback depends on the particularities of your code.

Upvotes: 1

googletorp
googletorp

Reputation: 33285

I believe this is more or less a duplicate of this question that you asked yourself, only worded a bit more precise. My answer from there still applies to this answer.

Basically, there is no reason to go through all the AJAX hoops to use drupal_set_message, it will only slow things down. Using drupal_set_meesage will not get the messagefx module to react upon the html you insert, because it was generated from drupal_set_message. It will be a lot faster and simpler to generate and input the html directly in your javascript without any ajax calls, and then add the fx by calling relevant js function.

Edit:

What you are asking is impossible, you can't get drupal to show the messages from drupal_set_message() with effects on the fly for two reasons:

  1. Drupal is written en PHP so the only way to get new content to the page using PHP is reloading the page or loading a new one.
  2. Messagefx functions by running a simple js that targets the messages that has been created when the page loads, so even if it was possible to get drupal to update the page, printing new messages, they wouldn't get the effects from messagefx.

You could accomplish your goal like I have explained, I would probably alter it slightly to fit and optimize to what you have written here. However, you can't get new messages to the screen without doing something like jQuery's append(), prepend() functions. So I'm afraid you have to decide to do like I have described or similar, or not do it at all.

Also one thing you have to considder is, that whether you make/input the html through your js or through drupal, wont really matter regarding the end result: a page written in html.

Upvotes: 1

McAden
McAden

Reputation: 13970

drupal_get_messages($type = NULL, $clear_queue = TRUE)

Unless I'm mistaken though you will have to set the error div or display them yourself in some manner.

EDIT after comment:

OH! Normally the messages are displayed on page load in page.tpl.php so you have to place them within a div manually for them to show up immediately.

You should be able to call theme() on each one passing in the appropriate params to trigger all the drupal hooks and it will return the correct HTML which you place into the page. Calling theme will format them like they would normally appear on the page as if they were printed from your theme although they won't have any extra changes that you might have in page.tpl.php.

Upvotes: 1

Related Questions