tags:

views:

188

answers:

2

This is a follow up to: http://stackoverflow.com/questions/1296984/core-javascript-functions-to-make-error-warning-or-notice-messages-appear/1298286

Basically, the "drupal_set_message()" function stores error messages in session. I need a way to tell drupal VIA JAVSCRIPT to now display those messages. If the page was refreshed, these messages would be displayed, but I want a way to force them to be displated immediately?

A: 

since these messages are in the session and/or db, you'd have to use ajax i guess. thats not a very conventional approach.

have to say that on my sites this drupal_set_message() function frequently displays the message one page too late, so i feel your pain.

stef
Exactly. SO, there must be a way to call it directly?
RD
+1  A: 

I'm not sure if I have understood you correct, but it seems you want to react upon something in your javascript and display a message in drupal style with the effects.

First of all to make things clear, the JavaScript and Drupal (PHP) can't really talk to each other very well. The only solution is through AJAX, so it wouldn't make any sense to want to use drupal_set_message() from your javascript. The reason is that it would be quite difficult and you would just end up with html you would need to append to your page. So it would be much easier to just create the html in your javascript direct, like I proposed in the answer to your other question. So all that would be left would be to append the effects. This is actually not that hard to do, depending on your effects. The only tricky part is getting the settings from the messagefx module, which it seems you currently cant, since it doesn't save any settings in the script variables.

So this solution in code would be something like this:

$("#something").a_trigger(function () {
    $("#messages").append('your html with message').find('the html you inserted').effect(some effect here);
});

Depending on the effect you want to create, you would to call a different function, look at what messagefx does in it's module file around line 55.

The other possibility is that you want to react on any drupal_set_message(). That hard part would be to figure out when there is a message, as there isn't really a way to know in the javascript. Also consider that most messages is the result of submitting a form, where the page reload anyways, and this would be unnecessary. So you would start by creating a simple AJAX function to fetch the data, and create your own simple module to do this. Basically all you need to to define an url with hook_menu and in that callback handle the ajax. You can get the messages from the $_SESSION super global.

So the callback, would look something like this

function mymodule_ajax_callback() {
    if (isset($_SEEION['messages'] && !empty($_SEEION['messages'])) {
        $result = array();
        foreach($_SEEION['messages'] as $message) {
            $result[] = 'Generate html based on $message';
        }
        unset($_SEEION['messages']) // Remove the messages so they wont appear twice.
    }
    return drupal_json($result);
}

The rest will look like the first solution, only you will get the html from the ajax instead.

googletorp
You should not manipulate the $_SESSION array directly for this - use `drupal_get_messages()` instead, or even simpler, as you want the rendered html anyways, call theme('status_messages'), which will fetch them itself. (NOTE: To clear the messages you'd need to call `drupal_get_messages()` with the $clear_queue parameter set to true, otherwise they will be displayed again next time around)
Henrik Opel
Yeah, that's better. Didn't know about drupal_get_message. Good point
googletorp