add meta tag to head in drupal 8

Aashi picture Aashi · Mar 10, 2016 · Viewed 12.7k times · Source

Iam new to drupal 8 and want to add meta tag to my drupal 8 site.

I had done this:

  1. added below code in my THEME.theme file

    /**
     * Implements hook_page_attachments_alter().
     *
     * Include meta tags and fonts using attachment method.
     */
    function bartik_page_attachments_alter(array &$page) {
     // echo "<pre>"; echo "BEFORE"; print_r($page);
      // Include fonts.
      $font_attributes = array(
        'rel' => 'stylesheet',
        'type' => 'text/css',
        'href' => 'https://fonts.googleapis.com/css?family=Lato:400,100,100italic,300,300italic,400italic,700',
      );
    
      $page['#attached']['html_head_link'][] = array($font_attributes);
    
      // Set up meta tags.
      // Modern IE & chrome-frame rendering engine tag.
      $rendering_meta = array(
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => array(
          'name' => 'SKYPE_TOOLBAR',
          'content' => 'SKYPE_TOOLBAR_PARSER_COMPATIBLE',
        ),
      );
      $page['#attached']['html_head'][] = [$rendering_meta, 'rendering_meta'];
     //echo "<pre>"; echo "AFTER"; print_r($page);
    }
    
    1. cleared cashe.

This code just attaches the meta tag for login page i.e in my case is:

mysite.com/user/login

Where I am doing wrong? What should I do to attach it for whole site

Answer

Danielishko picture Danielishko · Mar 11, 2016

What I did to add meta tag to head was:

function theme_preprocess_html(&$variables) {

  $xuacompatible = [
    '#tag' => 'meta',
    '#attributes' => [
      'http-equiv' => 'x-ua-compatible',
      'content' => 'ie=edge',
    ],
  ];


  $variables['page']['#attached']['html_head'][] = [$xuacompatible, 'x-ua-compatible'];
}; 

It results in

<meta http-equiv="x-ua-compatible" content="ie=edge">

Maybe not the best solution but it works :)

Good luck