开发者

How to add conditional CSS and JS to every page from a module?

开发者 https://www.devze.com 2023-03-23 08:05 出处:网络
I am building a module that lets the administrator choose a number of optional CSS and JS files that should be included when every page is rendered.

I am building a module that lets the administrator choose a number of optional CSS and JS files that should be included when every page is rendered.

The function hook_init seems to be the right pl开发者_Python百科ace, but on the hook_init page it says that this is actually not the right way to do it:

To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.

Does this not apply to my case, or is there a better way to do it?


That doesn't apply in your case. It only applies if the include of the CSS and JS are not conditional on anything.


My problem was aggregation if the css was added conditionally(in my case conditioned on language). The result was the css not being loaded when aggregation was enabled. Simply reading the documentation of drupal_add_css helped me a lot. I had 2 options preventing this from working: 'every_page' set to TRUE because I misinterpreted it ( I actually wanted it on all pages, but only for certain languages ) 'preprocess' has a default value of TRUE and it causes a css to be included in aggreation, problem being that if it is not loaded at the time the aggregated files get created, it will not be loaded at all afterwards.

Solution was: drupal_add_css(path_to_theme() . '/css/language-override.css', array('weight' => 101, 'preprocess' => FALSE));

0

精彩评论

暂无评论...
验证码 换一张
取 消