backdrop-issues icon indicating copy to clipboard operation
backdrop-issues copied to clipboard

Allow JS in theme .info files to be added to footer

Open BWpanda opened this issue 3 years ago • 2 comments

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.

BWpanda avatar Jul 07 '22 02:07 BWpanda

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

ghost avatar Jul 07 '22 03:07 ghost

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.

indigoxela avatar Jul 10 '22 10:07 indigoxela

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, or footer - any other key should trigger a watchdog warning to help people troubleshoot (if they have mistyped the scope for example)

klonos avatar Feb 08 '23 06:02 klonos

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'.

ghost avatar Feb 08 '23 09:02 ghost

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 avatar Feb 13 '23 09:02 klonos

@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.

ghost avatar Feb 13 '23 10:02 ghost

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?

klonos avatar Feb 13 '23 10:02 klonos

There is one exception to the above for minor release milestones:

  1. 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

ghost avatar Feb 13 '23 11:02 ghost

Touché ...milestone added then 😉

klonos avatar Feb 13 '23 11:02 klonos

Thanks to @BWPanda, @indigoxela, and @klonos. I have merged this into 1.x.

hosef avatar Feb 22 '23 20:02 hosef