You are here

unset_html_head_link.module in Unset HTML head link 8

File

unset_html_head_link.module
View source
<?php

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\NodeInterface;

/**
 * Implements hook_entity_view_alter().
 * @param array $build
 * @param EntityInterface $entity
 * @param EntityViewDisplayInterface $display
 */
function unset_html_head_link_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {

  // Cheking view_mode for node.
  if ($build['#view_mode'] !== 'full' && $entity instanceof NodeInterface) {
    return;
  }
  _remove_header_links($build);
}

/**
 * Implements hook_page_attachments_alter().
 * @param array $attachments
 */
function unset_html_head_link_page_attachments_alter(array &$attachments) {
  _remove_header_links($attachments);
}
function _remove_header_links(array &$attachments) {

  // Cheking html_head_link on attached tags in head.
  if (!isset($attachments['#attached']['html_head_link'])) {
    return;
  }

  // Array to unset.
  $unset_html_head_link = [
    'delete-form',
    'edit-form',
    'version-history',
    'revision',
    'display',
    'drupal:content-translation-overview',
    'drupal:content-translation-add',
    'drupal:content-translation-edit',
    'drupal:content-translation-delete',
    'shortlink',
  ];

  // Unset loop.
  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 unset_html_head_link_module_implements_alter(&$implementations, $hook) {
  if ($hook === 'page_attachments_alter') {
    $group = $implementations['unset_html_head_link'];
    unset($implementations['unset_html_head_link']);
    $implementations['unset_html_head_link'] = $group;
  }
}