5

I want to add meta tags to my site.

I added this code in my THEME.theme file and cleared the cache.

/**
 * Implements hook_page_attachments_alter().
 *
 * Include meta tags and fonts using attachment method.
 */
function bartik_page_attachments_alter(array &$page) {
  $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);

  $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'];
}

This code just attaches the meta tag to the login page.

What should I do to add the meta tag to all the site pages?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Aashi
  • 389
  • 1
  • 9
  • 20

4 Answers4

11

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

Danielishko
  • 111
  • 2
7

This following code is working for me to override meta title and description with custom fields from Basic Page content type :

/**
 * Implements hook_preprocess_html() for html templates.
 */
function theme_preprocess_html(&$variables)
{
    // Add META tag title + description from back-office node basic page field
    $node = \Drupal::routeMatch()->getParameter('node');// Load the node entity from current route
    if ($node) {
        if ($node->getType() === 'page') {// Check if node type is basic page
            $title = [
                '#tag' => 'meta',
                '#attributes' => [
                    'name' => 'title',
                    'content' => $node->get('field_custom_meta_title')->value,
                ],
            ];
            $description = [
                '#tag' => 'meta',
                '#attributes' => [
                    'name' => 'description',
                    'content' => $node->get('field_custom_meta_description')->value,
                ],
            ];
            $variables['page']['#attached']['html_head'][] = [$title, 'title'];
            $variables['page']['#attached']['html_head'][] = [$description, 'description'];
        }
    }
}

Happy coding !

Antoine Subit
  • 9,803
  • 4
  • 36
  • 52
0

Maybe try this module https://www.drupal.org/project/metatag or create preprocess function ?

0

I wanted to make a minor change to Antoine's answer since the title metatag will not work as he has described it. The array will need to be changed to allow for a <title> naming convention. Here's is a sample of how I have it working within a custom module:

function meta_manager_preprocess_html(&$variables) {
    $node = \Drupal::routeMatch()->getParameter('node');

    if ($node) {        // Make sure we are in a node
        $title = [
          '#tag' => 'title',            // Set title for element
          '#value' => 'Sample Title'    // Set value for title
        ];
        $description = [
          '#tag' => 'meta',             // Set meta for element
          '#attributes' => [            // Set attributes for meta
            'name' => 'description',
            'content' => 'Sample Description',
          ],
        ];
        $variables['page']['#attached']['html_head'][] = [$title, 'title'];
        $variables['page']['#attached']['html_head'][] = [$description, 'description'];
    }
}

NOTE: Also remember that by default there is a <title> tag in the html.html.twig file, so if you do this then you may want to either comment it out or remove it first.

I am also working on this module to implement custom metatag information on each pages individual node to allow for more customization, but this will work for a generic application. Or, you can use the example Antoine is using where he adds custom fields into his content types.

Ron
  • 151
  • 2