Invoking drupal

2019-07-25 06:10发布

This is a follow up to: Core javascript functions to make error, warning or notice messages appear?

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

标签: drupal
2条回答
forever°为你锁心
2楼-- · 2019-07-25 06:16

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.

查看更多
虎瘦雄心在
3楼-- · 2019-07-25 06:29

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($_SESSION['messages'] && !empty($_SESSION['messages'])) {
        $result = array();
        foreach($_SESSION['messages'] as $message) {
            $result[] = 'Generate html based on $message';
        }
        unset($_SESSION['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.

查看更多
登录 后发表回答