4

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?

11

The invocation of the hook_html_head_alter was removed from Drupal core.
Issue - Remove invocation of hook_html_head_alter().

D8 has a new hook to achieve it : hook_page_attachments_alter().

Code from 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'];
  }
  • Thx a lot. One more question: how to unset <meta name="Generator" content="Drupal 8 (https://www.drupal.org)"> and <meta name="MobileOptimized" content="width"> and <meta name="HandheldFriendly" content="true">? – Platon Nov 24 '15 at 10:51
  • OK. This blog post solved my comment: blizzy.ru/node/15 – Platon Nov 25 '15 at 10:04
  • For nodes you may have to use hook_node_view_alter instead. See drupal.stackexchange.com/a/263094/15055 – leymannx Oct 23 '18 at 19:28
1

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") {
    ...
  }  
}
1

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, privacy policy and cookie policy

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