a12s-1.0.0-beta7/modules/seo/a12s_seo.module
modules/seo/a12s_seo.module
<?php
/**
* @file
* Contains a12s_seo.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
/**
* Implements hook_preprocess_pager().
*
* Manage the "rel next" and "rel prev" head tags.
*
* @see https://www.drupal.org/project/drupal/issues/1567684#comment-12398066
*/
function a12s_seo_preprocess_pager(&$pager): void {
if (!isset($pager['current']) || empty($pager['items'])) {
return;
}
$currentPath = Url::fromRoute('<current>');
$path = $currentPath->setAbsolute(TRUE)->toString();
// Correct for first page actually being page 0.
$currentPage = $pager['current'] - 1;
// Use the base path if on page 2 otherwise `page={{current_page-1}}`.
$prevHref = ($currentPage == 1 ? $path : (!empty($pager['items']['previous']['href']) ? $path . $pager['items']['previous']['href'] : NULL));
$nextHref = (!empty($pager['items']['next']['href']) ? $path . $pager['items']['next']['href'] : NULL);
// The $pager variable is never rendered, as the 'page', 'prev', or 'next'
// keys.
// So we need to attach the HTML head links to a dummy, element that have to
// be displayed in the pager.html.twig template.
// We could also transform the "text" key of the "prev" or "next element to
// a renderable array, but it could lead to issue with other contrib modules
// that want to alter the "text"...
// Add The prev rel link.
if ($prevHref) {
$pager['items']['html_head']['#attached']['html_head_link'][] = [
[
'rel' => 'prev',
'href' => $prevHref,
],
TRUE,
];
}
// Add the next rel link.
if ($nextHref) {
$pager['items']['html_head']['#attached']['html_head_link'][] = [
[
'rel' => 'next',
'href' => $nextHref,
],
TRUE,
];
}
}
/**
* Implements hook_page_attachments_alter().
*/
function a12s_seo_page_attachments_alter(array &$attachments): void {
// Remove some useless link tags (not SEO relevant, and may lead to 403).
// @see \Drupal\a12s_seo\SeoManager::getUselessRelAttributes()
$htmlHeadLink = $attachments['#attached']['html_head_link'];
/** @var \Drupal\a12s_seo\SeoManager $seoManager */
$seoManager = \Drupal::service('a12s_seo.manager');
$uselessRelAttributes = $seoManager->getUselessRelAttributes();
foreach ($htmlHeadLink as $key => $value) {
if (isset($value[0]['rel']) && in_array($value[0]['rel'], $uselessRelAttributes, TRUE)) {
unset($attachments['#attached']['html_head_link'][$key]);
}
}
}
/**
* Implements hook_module_implements_alter().
*/
function a12s_seo_module_implements_alter(&$implementations, $hook): void {
if ($hook === 'page_attachments_alter') {
$group = $implementations['a12s_seo'];
unset($implementations['a12s_seo']);
$implementations['a12s_seo'] = $group;
}
}
/**
* Implements hook_entity_view_alter().
*
* Remove some useless link tags (not SEO relevant, and may lead to 403).
*
* @see \Drupal\a12s_seo\SeoManager::getUselessRelAttributes()
*/
function a12s_seo_entity_view_alter(array &$build, EntityInterface $entity): void {
if ($build['#view_mode'] === 'full' && $entity instanceof NodeInterface) {
if (!isset($build['#attached']['html_head_link'])) {
return;
}
/** @var \Drupal\a12s_seo\SeoManager $seoManager */
$seoManager = \Drupal::service('a12s_seo.manager');
$uselessRelAttributes = $seoManager->getUselessRelAttributes();
foreach ($build['#attached']['html_head_link'] as $key => $value) {
if (isset($value[0]['rel']) && in_array($value[0]['rel'], $uselessRelAttributes, TRUE)) {
unset($build['#attached']['html_head_link'][$key]);
}
}
}
}
