2

How to remove specific meta-tag from header section of page ?

I am trying to remove

<link rel="delete-form-edit" href="/node/11/delete" />

tag from header section of the page ,but no luck.

As there is no tag as shown above found on html.html.twig file header section ,its automatically generating.

3

I think you need hook_page_attachments_alter, which Alter attachments (typically assets) to a page before it is rendered.

 /**
 * Implements hook_page_attachments_alter().
 * @param array $attachments
 */
function my_module_page_attachments_alter(array &$attachments) {
  if (!isset($attachments['#attached']['html_head_link'])) {
    return;
  }
  $unset_html_head_link = [
    'canonical',
    'shortlink',
    'delete-form',
    'edit-form',
    'version-history',
    'revision',
  ];
  foreach ($attachments['#attached']['html_head_link'] as $key => $value) {
    if (isset($value[0]['rel']) && in_array($value[0]['rel'], $unset_html_head_link)) {
      unset($attachments['#attached']['html_head_link'][$key]);
    }
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function my_module_module_implements_alter(&$implementations, $hook) {
  if ($hook === 'page_attachments_alter') {
    $group = $implementations['my_module'];
    unset($implementations['my_module']);
    $implementations['my_module'] = $group;
  }
}

excerpt from : How to unset link rel: shortlink, edit-form, version-history and revision from ?

  • 2
    Just to clarify this a little bit, <link> elements are in the 'html_head_link' group. <meta> are in the 'html_head' group. – mpdonadio Oct 11 '17 at 14:28
  • in_array($value[0]['rel'], $unset_html_head_link) would be better written as isset( $unset_html_head_link [ $value[0]['rel'] ] ) as its an array key and the looping of the array is not needed. – Andrew Killen Apr 3 '18 at 13:01
  • This doesn't work for nodes. You have to use hook_ENTITY_TYPE_view_alter instead and preprocess the $build['#attached']['html_head_link'] like shown in the other answer by @masdzen. – leymannx Aug 23 '18 at 13:34
1

For nodes you have to use hook_ENTITY_TYPE_view_alter.

There you can alter node-specific values for $build['#attached']['html_head_link'] and $build['#attached']['html_head'].

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function MYMODULE_node_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {

  if (empty($build['#attached']['html_head_link'])) {
    return;
  }

  $tags = [
    'canonical',
    'shortlink',
    'delete-form',
    'edit-form',
    'version-history',
    'revision',
  ];

  foreach ($build['#attached']['html_head_link'] as $key => $tag) {

    if (empty($tag[0]['rel'])) {
      continue;
    }
    if (!in_array($tag[0]['rel'], $tags)) {
      continue;
    }

    // Hide tag.
    unset($build['#attached']['html_head_link'][$key]);
  }
}

In /core/modules/node/src/Controller/NodeViewController.php from class NodeViewController method view() Drupal initially adds the canonical tag:

if ($rel == 'canonical') {
  // Set the non-aliased canonical path as a default shortlink.
  $build['#attached']['html_head_link'][] = [
    [
      'rel' => 'shortlink',
      'href' => $url->setOption('alias', TRUE)->toString(),
    ],
    TRUE,
  ];
}
  • This one worked like a charm. And I just found out, that in the end we'll find all the head tags in hook_preprocess_html in the $variables['page']['#attached'] array to be altered. – leymannx Aug 24 '18 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy