1

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 place, 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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate, my first Drupal module, so was not completely sure.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.