简体   繁体   中英

In drupal 9. How can I add custom html between <head></head> Doing a hook or something rather than install a module

I am trying to add custom html between <head></head> Doing a hook or something rather than install a module.

But i cant figure how to doit.

I am using bartik and i make this function

function bartik_add_text_to_header(&$vars, $hook) {//}

but i cant figure how or which function to used.

I try with

drupal_set_html_head('style type="text/css">@import url(' . $GLOBALS[base_url] . '/modules/codefilter/codefilter.css);</style>');

But the drupal_set_html_head looks that is not existing in drupal 8 or 9

You should be using a custom theme with bartik as it's base theme rather than the bartik theme itself.
Just make the minimum file for a theme and set the bartik theme as it's base theme.

You can put whatever you want in the head section by overriding the template file that is currently being used to output that part of the html. For Bartik it is a file named html.html.twig in the core/themes/bartik/templates/classy/layout folder.
You would make a copy this file and put it into your custom themes templates folder so your file is used instead of the original.
To easily find what file is currently being used, you can enable twig debugging so comments are output in your html that show exactly what template files are being used.

Having said all that, and seeing as you are only looking to add css, you probably want to check out this page on Adding stylesheets (CSS) and JavaScript (JS) to a Drupal theme which will show you the different options you have to add js and css.

If you are just after a quick answer.... the function you want may be:

function fluffiness_preprocess_page(&$variables) {
  $variables['#attached']['library'][] = 'fluffiness/global-styling';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM