0

that's the first time I approach to Drupal or a CMS more generally but I've been working since years as FE dev.

I'm creating a theme based on a template created from scratch for the Italian Public Administration(PA) in such way to define a standard for the PA websites.

This is the template project

So I have to fully rely over the CSS and JS files provided by the theme, and can't use a base theme.

I'm struggling to understand how to change the content of the head-placehoder and the title one in the html.html.twig file

  <head>
    <head-placeholder token="{{ placeholder_token }}">
    <title>{{ head_title|safe_join(' | ') }}</title>
    <css-placeholder token="{{ placeholder_token }}">
    <js-placeholder token="{{ placeholder_token }}">
  </head>
0

I suggest you look at https://www.drupal.org/docs/8/theming-drupal-8/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-theme, which provides pretty good documentation on how to add CSS in a theme.

If you don't extend from a theme then you technically extend from a special theme called "stable". You should get almost no default CSS then, at least from core modules. Unless that really results in a problem, in which case there are some options to override it (documented above), I wouldn't worry about that.

3
  • 1
    Actually this allows me to add CSS, but what i need is to modify the head-placeholder section like the <meta> tags – pizzaboy Dec 31 '16 at 17:39
  • To add what kind of metatags exactly? You can either just hardcode them below/above in the template or you can add them through a hook like hook_page_attachments_alter(): stackoverflow.com/questions/35913739/… – Berdir Jan 2 '17 at 7:07
  • The question is 'Modify head-placeholder' not 'suggest best practice for placing css'. I want to modify the head placeholder for other reasons. – Kiee Sep 2 '19 at 11:22
0

For those who came here by Google, if you want to change the meta tags or other things injected by head-placeholder, you can use hook_page_attachments_alter(). Here is an example where I have removed the meta tag for Generator in Drupal 8. This meta tag:

<meta name="Generator" content="Drupal 8 (https://www.drupal.org)">

In your your_theme.theme, here is the code:

function your_theme_page_attachments_alter(array &$attachments) {
   foreach ($attachments["#attached"]["html_head"] as $key => $attachment) {
     if ('meta' === $attachment[0]["#tag"] &&
         !empty($attachment[0]["#attributes"]["name"]) &&
         'Generator' === $attachment[0]["#attributes"]["name"]) {

            unset($attachments["#attached"]["html_head"]["$key"]);
     }
   }
}

If you want to know about header-placholder and how the rendering is happening, read the accepted answer for this post What is placeholder_token in html.html.twig for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.