Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-8837: Remove pages with X-Robots-Tag: noindex from sitemap #62

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions helfi_proxy.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\helfi_proxy\ProxyManagerInterface;

/**
* Implements hook_module_implements_alter().
Expand Down Expand Up @@ -82,3 +83,38 @@ function helfi_proxy_page_attachments_alter(array &$attachments) {
$attachments['#attached']['html_head'][] = [$helfi_content_type, $tag_name];
}
}

/**
* Implements hook_simple_sitemap_links_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();
hyrsky marked this conversation as resolved.
Show resolved Hide resolved

/** @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]);
}
}
}

}