tags:

views:

455

answers:

2

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?

+1  A: 
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.

McAden
Think you misunderstood. I have an ajax call that calls drupals_set_messages on via page b from page a. After page b has been called, a hasnt changed. On page a I need a JAVASCRIPT call of some type, to show the messages in session on page a. Does that make sense?
RD
O... i see there your just telling me to get them. I know that. I can get them. I just want to display them by CALLING THE INTERNAL DRUPAL FUNCTIONS to show them on the screen. There must be something that drupal does, to show the error messages on the screen. My question is what?
RD
I've edited my post to address the comments
McAden
+2  A: 

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.

googletorp
The reason why I want to do this is as follows: Users will change settings in the module, which then won't reflect in my code. I want it all centralised and make it so that the changes in the plugin, will reflect on the page itself. If I it was just simple javascript, then yes, it would be faster without the ajax, but unfortunately, it's not.
RD
p.s. sorry for the duplicate, but once a question has few answers, people think it's solved. so I thought maybe making my question more exact might make it get a response.
RD

related questions