backdrop-issues
backdrop-issues copied to clipboard
Allow JS in theme .info files to be added to footer
Description of the need
When adding a JS file to your theme via the .info file, it's automatically added to the header (i.e. between the <head> tags of the HTML). If you instead want to add it to the footer (i.e. just before the closing <body> tag in the HTML) you need to instead add the JS via backdrop_add_js() in something like THEME_preprocess_page(), which allows you to specify 'scope' => 'footer'.
Proposed solution
It'd be nice if we could instead do something like this in the .info file:
scripts[footer][] = js/my_footer.js
That'd be much simpler and, IMO, the more expected solution.
Alternatives that have been considered
The current solution works well enough, it's just not as easy/obvious.
Additional information
Here's a question in the forum of someone expecting this functionality but finding it didn't work: https://forum.backdropcms.org/forum/how-add-custom-js-template-head-or-footer
Draft of feature description for Press Release (1 paragraph at most)
Backdrop now allows themes to add javascript files to the footer directly from the .info file.
Here's a PR: https://github.com/backdrop/backdrop/pull/4128
It's fully BC, so the following will all work:
; These files will be added to the header:
scripts[] = js/the-old-way.js
scripts[header][] = js/the-new-way.js
; This file will be added to the footer:
scripts[footer][] = js/footer.js
I'm supporting this request! Many times theme creators will be frontend devs with possibly little php skills - and I have to admit, function backdrop_add_js() can be quite confusing.
Providing the ability to simply add js to the footer that way will make their life easier.
Sorry it has taken so long to notice this @BWPanda ...I'm also supportive of this 👍🏼
Can you please do the following?:
- rebase the PR to spin up a fresh sandbox (people can upload test theme .zip files to test there, but also to allow to run against latest tests)
- add a sanity check for the key being blank/missing,
header, orfooter- any other key should trigger a watchdog warning to help people troubleshoot (if they have mistyped the scope for example)
PR rebased.
add a sanity check for the key being blank/missing
What do you mean by this @klonos? Other values are allowed: https://docs.backdropcms.org/api/backdrop/core%21includes%21common.inc/function/backdrop_add_js/1
scope: The location in which you want to place the script. Possible values are 'header' or 'footer'. If your theme implements different regions, you can also use these. Defaults to 'header'.
add a sanity check for the key being blank/missing
What do you mean by this @klonos? Other values are allowed:
Oops, sorry - I thought that only header/footer were allowed.
Code looks good to me. I've left an optional suggestion in the PR, which you are free to ignore, in which case this issue is RTBC from me 👍🏼
@klonos Yeah, that seems like a larger issue for possibly refactoring this entire function... Can you please file a follow-up for that?
In the meantime, marking this RTBC on your behalf.
Can you please file a follow-up for that?
Done: #5976
@BWPanda this issue is a feature request, and it is now RTBC. All we need in order to set a milestone is an advocate for it. Am I to assume that you are advocating?
There is one exception to the above for minor release milestones:
- If an issue has an accompanying Pull Request in a near-ready state that has earned either of the labels 'pr - ready to be committed' or 'pr - works for me', but is not suitable for a bug-fix release, this issue can also be added to the next minor release milestone.
(https://docs.backdropcms.org/documentation/adding-milestones-to-issues)
#NoAdvocateRequired
Touché ...milestone added then 😉
Thanks to @BWPanda, @indigoxela, and @klonos. I have merged this into 1.x.