Score:0

Why don't I see Asset Injector code directly in the HTML?

bd flag

I’m having trouble getting the Asset_Injector module to work, and would like to find out why it's not working.   As a simple test, I want to insert some CSS using the module’s CSS injector:

#container {
    max-width: 1000px;
    margin: 1em auto;
}

Asset_Injector is configured with no restrictions on injection:  content types, pages, etc. But when I create a page, then look at the source code, the CSS is not injected.

I’ve cleared the Drupal caches and the browser cache. I’m running a Drupal 9.4 site on Pantheon with Asset_Injector 8.x-2.13.  

To re-create the problem:

Inline JavaScript is highly discouraged. It's recommended to put the JS you want to use inline in a file instead, because that allows JavaScript to be cached on the client side. It also allows JavaScript code to be reviewed and listed.

 

id flag
Where did you see the "Inline JavaScript is highly discouraged..." message? That string is not part of Drupal Core. Also, the idea is to inject CSS. What does JavaScript have to do with this? Also, what is your question?
bburch2000 avatar
bd flag
https://www.drupal.org/docs/theming-drupal/adding-assets-css-js-to-a-drupal-theme-via-librariesyml#inlinejs.
id flag
Yes, it is not good in principle to inline CSS and JavaScript but what does that have to do with whatever your question is?
bburch2000 avatar
bd flag
Asset Injector can inject CSS and Javascript. I'm using CSS as a simple test. My question is, "why is Asset Injector not working". Thanks, Bill
bburch2000 avatar
bd flag
My working theory is that there's some security restriction on Drupal that's blocking inline JS/CSS. And that's why Asset Injector is not working
id flag
The actual question belongs ^ up in the question above.
bburch2000 avatar
bd flag
Thanks, edit made. Bill
id flag
Is your expectation that the raw CSS will be injected into the HTML of the page inside a `<style>` element?
bburch2000 avatar
bd flag
I don't know how Asset Injector would insert the CSS. I need to get it working first. I copied the CSS from a JS Fiddle provided by Highcharts (links below). Do you think I need to change the CSS because it's not properly formatted?
bburch2000 avatar
bd flag
https://jsfiddle.net/gvaartjes/87toLqnp/
bburch2000 avatar
bd flag
https://www.highcharts.com/docs/gantt/getting-started-gantt
Score:3
id flag

Asset Injector adds the custom CSS and JavaScript as libraries, which means these appear as attached assets. Asset Injector does not insert the CSS or JavaScript directly into the page HTML. That is the reason you do not see these items directly in the HTML.

bburch2000 avatar
bd flag
That's very helpful -- thank you so much. I had been using Add to Head (https://www.drupal.org/project/add_to_head). The use of "inject" in the two modules' descriptions misled me. Add to Head "injects" code, but Asset Injector "attaches".
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.