By jcharlesberry on
Hello, I have read different things about the conditional inclusion of JS. I need to include different JS files on different pages/content types. Is it true that the logic should never be put in the preprocess functions and instead in files like html.tpl.php?
Comments
That's backwards. JS should
That's backwards. JS should ideally be added to the render array for the element. If that cannot be done, then the preprocessing function is the next place. It should not be added in the template.
Contact me to contract me for D7 -> D10/11 migrations.
This says...
This page https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_... says:
$options['preprocess'] should be only set to TRUE when a file is required for all typical visitors and most pages of a site. It is critical that all preprocessed files are added unconditionally on every page, even if the files are not needed on a page. This is normally done by calling drupal_add_js() in a hook_init() implementation.
Ahh, I understand your
Ahh, I understand your confusion now. You are mixing up two different things.
1) A preprocess function (which you mentioned in your original post) is a function that is called before a template or theme function is executed.
2) $options['preproccess'] is an option that can be added in drupal_add_js() (or a render array that adds JS using the #attached attribute).
They aren't the same thing.
JS shouldn't be added in template files. As I said in my original response, they are best added to pages/blocks/elements using the #attached property of render arrays, or in a preprocess function.
Contact me to contract me for D7 -> D10/11 migrations.