drupal_add_css、drupal_add_js 不起作用 [英] drupal_add_css, drupal_add_js does not work

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

问题描述

我正在使用 drupal_add_css()drupal_add_js() 将 CSS 和 JavaScript 文件添加到我的 Drupal 站点.我在一个名为 control 的模块中执行此操作,因此我使用的函数称为 control_preprocess_page(&$vars).

I am using drupal_add_css() and drupal_add_js() to add CSS and JavaScript files to my Drupal site. I am doing this in a module called control so the function I'm using is called control_preprocess_page(&$vars).

但在我的主题中没有添加任何内容!

But in my theme nothing is added!

推荐答案

这些函数在 preprocess_page() 函数中不起作用的原因是 template_preprocess_page()(首先调用)已经将结构化内容格式化为变量$scripts和$styles.如果要在预处理级别添加额外的 js 或 css,则需要重新生成这 2 个变量,如下所示:

The reason these functions aren't working within the preprocess_page() function is that template_preprocess_page() (which is called first) has already formatted the structured content into variables $scripts and $styles. If you want to add additional js or css at the preprocess level, you need to regenerate those 2 variables, something like this:

function control_preprocess_page(&$vars) {
  // Add new CSS.
  drupal_add_css('path/to/css/foo.css');
  // Rebuild the 'styles variable.
  $vars['styles'] = drupal_get_css();

  // Add new JS.
  drupal_add_js(...);
  $vars['scripts'] = drupal_get_js();
}

在 hook_init 中使用 drupal_add_js/drupal_add_css 或更精确的目标函数(例如,alter hook 或 nodeapi hook,如果适用)将避免重新生成这些变量.

Using drupal_add_js/drupal_add_css in hook_init, or a more precisely targeted function (eg, an alter hook, or nodeapi hook, if applicable), will avoid having to regenerate those variables.

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

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