调用drupal [英] Invoking drupal

查看:96
本文介绍了调用drupal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续行动:核心JavaScript功能可以显示错误,警告或通知消息?

基本上, drupal_set_message()存储会话中的错误消息。我需要一种方法来告诉drupal VIA JAVSCRIPT不要显示这些消息。如果页面被刷新,这些消息将被显示,但我想要一种方法来强制他们立即显示。

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.

推荐答案

我不知道我是否理解你是正确的,但是似乎你想对你的javascript中的某些内容做出反应,并用drupal样式显示一个消息。

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.

首先要弄清楚,JavaScript和Drupal(PHP)不能很好地相互交流。唯一的解决方案是通过AJAX,所以从你的javascript中使用drupal_set_message()是没有意义的。原因是这将是相当困难的,你最终只需要使用html,你需要附加到你的页面。所以在javascript直接创建html将会更容易,就像我在其他问题的答案中提出的那样。所以剩下的一切都是追加效果。这实际上不是很难做,取决于你的效果。唯一棘手的部分是从messagefx模块获取设置,这似乎是您目前无法使用的,因为它不会在脚本变量中保存任何设置。

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);
});

根据您要创建的效果,您可以调用其他功能,查看whatf在它的第55行周围的模块文件中。

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.

另一种可能性是你想对任何drupal_set_message()做出反应。那个困难的部分是找出什么时候有一个消息,因为没有一个真正的方法知道在javascript。还要考虑大多数消息是提交表单的结果,其中页面重新加载,这是不必要的。因此,您将首先创建一个简单的AJAX函数来获取数据,并创建自己的简单模块来执行此操作。基本上你需要使用hook_menu定义一个url,并在该回调句柄中处理ajax。你可以从$ _SESSION超级全局获取消息。

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);
}

其余的将看起来像第一个解决方案,只有您才能从ajax获取html。

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

这篇关于调用drupal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆