Remove and change meta tags in Drupal 8

Drupal 8.Instructions for organization and output of META tag

Out of the box, 8.x provides a pretty poor set of META tags, and many of them - for some reason does not feature on the specifications of W3C(World Wide Web Consortium).

Overriding the standard META tags

Drupal 7.x has the ability to override any of the items in header with the functionhook_html_head_alter(). In Drupal 8.x we use hook_page_attachments_alter (), which is triggered after a callhook_page_attachments()and before the page is rendered.

Override the output tag: Generator, MobileOptimized and HandheldFriendly

So, to override the standard META tags in Drupal 8, first of all, we need to remove them - use the built-in functionunset()To this end, in a fileTHEME_NAME.theme(whereTHEME_NAME- is your template name) in root of the template, write the following code:


// ...
function THEME_NAME_page_attachments_alter(array &$page) {
  // Define an array of META tags to remove.
  $unset_meta = [
    'system_meta_generator', // Meta name "Generator"
    'MobileOptimized', // Meta name "MobileOptimized"
    'HandheldFriendly', // Meta name "HandheldFriendly"
  ];
  // Check values and delete.
  foreach ($page['#attached']['html_head'] as $key => $value) {
    if (in_array($value[1], $unset_meta)) unset($page['#attached']['html_head'][$key]);
  }
  // ...
}
// ...

Adding new META tags, reorganize the overall rendering


// ...
function THEME_NAME_page_attachments_alter(array &$page) {
  // Remove unnecessary META tags.
  // Define an array of values.
  $unset_meta = [
    'system_meta_generator', // Meta name "Generator"
    'MobileOptimized', // Meta name "MobileOptimized"
    'HandheldFriendly', // Meta name "HandheldFriendly"
  ];
  // Check values and delete.
  foreach ($page['#attached']['html_head'] as $key => $value) {
    if (in_array($value[1], $unset_meta)) unset($page['#attached']['html_head'][$key]);
  }
  // Add the necessary and override standard META tags.
  // Define an array of values.
  $attach_meta = array(
       // Add special permission to display the site as a Web App.
    'web_app_capable_default' => array(
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'mobile-web-app-capable',
        'content' => 'yes',
      ),
    ),
    'web_app_capable_android' => array(
      '#tag' => 'meta',      
      '#attributes' => array(
        'name' => 'android-mobile-web-app-capable',
        'content' => 'yes',
      ),
    ),
    // Add a special icon for the site on an Android iOS device.
    // Minimum size: 60x60 px.
    'android_touch_icon' => array(
      '#tag' => 'link',      
      '#attributes' => array(
        'rel' => 'android-touch-icon',
        'href' => file_create_url(drupal_get_path('theme', 'THEME_NAME') . '/path/to/image/android-touch-icon.png'),
      ),
    ),
  );
  // Loop add / override the values of meta tags.
  foreach ($attach_meta as $key => $value) {
    $page['#attached']['html_head'][] = [$value, $key];
  }
}

If you need to add any other tag that simply use in analogy with exampleandroid_touch_icon