You are here

disable_link_rel.module in Disable link rel 8

The main functionality of the module. Getting data and using it in hooks.

File

disable_link_rel.module
View source
<?php

/**
 * @file
 * The main functionality of the module. Getting data and using it in hooks.
 */
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\NodeInterface;

/**
 * Implements hook_entity_view_alter().
 */
function disable_link_rel_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);
}

/**
 * Remove links.
 *
 * @param array $attachments
 *   Array build.
 */
function _remove_header_links(array &$attachments) {
  $config = \Drupal::getContainer()
    ->get('config.factory')
    ->getEditable('disable_link_rel.import');
  $enable = $config
    ->get('enable', FALSE);
  if ($enable) {
    if (!isset($attachments['#attached']['html_head_link'])) {
      return;
    }
    $unset_html_head_links = _disable_link_rel_parse_values($config
      ->get('links', ''));
    foreach ($attachments['#attached']['html_head_link'] as $key => $value) {
      if (isset($value[0]['rel']) && in_array($value[0]['rel'], $unset_html_head_links)) {
        unset($attachments['#attached']['html_head_link'][$key]);
      }
    }
  }
}

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

/**
 * Implements hook_module_implements_alter().
 */
function disable_link_rel_module_implements_alter(&$implementations, $hook) {
  $config = \Drupal::getContainer()
    ->get('config.factory')
    ->getEditable('disable_link_rel.import');
  $enable = $config
    ->get('enable', FALSE);
  if ($enable) {
    if ($hook === 'page_attachments_alter') {
      $group = $implementations['disable_link_rel'];
      unset($implementations['disable_link_rel']);
      $implementations['disable_link_rel'] = $group;
    }
  }
}

/**
 * Implements hook_link_alter().
 */
function disable_link_rel_link_alter(&$variables) {
  $config = \Drupal::getContainer()
    ->get('config.factory')
    ->getEditable('disable_link_rel.import');
  $enable = $config
    ->get('remove_link_attr', FALSE);
  if ($enable) {
    $unset_attr_list = _disable_link_rel_parse_values($config
      ->get('remove_link_attr_list', ''));
    foreach ($unset_attr_list as $attr) {
      if (array_key_exists($attr, $variables['options']['attributes'])) {
        unset($variables['options']['attributes'][$attr]);
      }
    }
  }
}

/**
 * Returns converted values.
 *
 * @param string $source_data
 *   The source value.
 *
 * @return string[]
 *   The values array.
 */
function _disable_link_rel_parse_values($source_data) {
  $values = [];
  $split_data = explode(',', $source_data);
  array_walk_recursive($split_data, function ($value) use (&$values) {
    $values[] = trim($value);
  });
  return $values;
}