Skip to content

Commit

Permalink
UHF-8837: remove pages with X-Robots-Tag: noindex from sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Sep 4, 2023
1 parent 257e937 commit 61f9c2f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions helfi_proxy.module
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,38 @@ function helfi_proxy_page_attachments_alter(array &$attachments) {
$attachments['#attached']['html_head'][] = [$helfi_content_type, $tag_name];
}
}

/**
* Implements hook_page_attachments_alter().
*/
function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) {
/** @var \Drupal\Core\Config\ImmutableConfig $config */
$config = \Drupal::service('config.factory')->get('helfi_proxy.settings');

if (!$paths = implode("\n", $config->get(ProxyManagerInterface::ROBOTS_PATHS) ?? [])) {
return;
}

/** @var \Drupal\helfi_api_base\Environment\Environment $environment */
$environment = \Drupal::service('helfi_api_base.environment_resolver')->getActiveEnvironment();

/** @var \Drupal\Core\Path\PathMatcherInterface $pathMatcher */
$pathMatcher = \Drupal::service('path.matcher');

// helfi_proxy module sets "X-Robots-Tag: noindex" header for configured
// paths. These url should not be included in the sitemap.xml file.
foreach ($links as $key => $link) {
$baseUrl = $environment->getUrl($link['langcode']);
$url = $link['url'];

if (str_starts_with($url, $baseUrl)) {
$path = substr($url, strlen($baseUrl));

// Remove matched paths from sitemap.xml file.
if ($pathMatcher->matchPath($path, $paths)) {
unset($links[$key]);
}
}
}

}

0 comments on commit 61f9c2f

Please sign in to comment.