6

In Drupal 7 I used this to unset unneeded elements and total overwrite meta tags:

function MYTHEME_html_head_alter(&$head_elements) {
  // Unset all.
  $remove_array = array(
    'system_meta_content_type', 
    'system_meta_generator', 
    'system_shortcut_icon', 
    'metatag_canonical',
  );
  foreach ($remove_array as $key) {
    if ($head_elements[$key]) {
      unset($head_elements[$key]);
    }
  }
  // Optimize the mobile viewport.
  $head_elements['mobile_viewport'] = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#weight' => -10,
    '#attributes' => array(
        'name' => 'viewport',
        'content' => 'initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width',
    ),
  );
  // ...
  // Many other my META tags
  // ...
}

But in Drupal 8 this doesn't work anymore. New elements such as $head_elements['mobile_viewport'] don't get added.

And old meta tags, such as

  • <meta name="Generator" content="Drupal 8 (https://www.drupal.org)">
  • <meta name="MobileOptimized" content="width">
  • <meta name="HandheldFriendly" content="true">
  • <link rel="shortcut icon" href="http://example.com/core/misc/favicon.ico" type="image/vnd.microsoft.icon">

are still in <head>. How can I remove them and add others instead in Drupal 8?

3 Answers 3

14

Drupal 8 doesn't use hook_html_head_alter(). Its usage has been removed in Remove invocation of hook_html_head_alter(). As described in drupal_add_*() deprecated in favour of #attached, in Drupal 8 is possible to use hook_page_attachments() and hook_page_attachments_alter(), for example using code similar to the following one, suggested in one of the comments for the change record.

function MYTHEME_page_attachments_alter(array &$page) {
  $viewport = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'name' => 'viewport',
      'content' => 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no',
    ],
  ];
  
  $page['#attached']['html_head'][] = [$viewport, 'viewport'];
}
1
2

override meta from page generate in routing/controller

/**
 * Implements hook_metatags_alter().
 */
function mymod_metatags_alter(array &$metatags, array $context) {
  $route_name = \Drupal::service('current_route_match')->getRouteName();

  if ($route_name == "mymod.default_controller_content") {
    ...
  }  
}
2

Just to alter this code is useful:

/**
 * Implements hook_page_attachments_alter().
 */
function MY_THEME_page_attachments_alter(array &$variables) {
  if ($variables['#attached']['html_head']) {
    foreach ($variables['#attached']['html_head'] as $delta => $tag) {
      if (isset($tag[0]['#tag']) && $tag[0]['#tag'] == '__THE_DESIRED_TAG__' && $tag[0]['#attributes']['rel'] == '__THE_REL_ATTR__') {
        $variables['#attached']['html_head'][$delta][0]['#attributes']['__THE_DESIRED_ATTR__'] = '__THE_DESIRED_VALUE__';
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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