Skip to content

Commit

Permalink
UHF-8837: handle environment_resolver exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Sep 4, 2023
1 parent cd70092 commit 1d9c336
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions helfi_proxy.module
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,33 @@ function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant)
return;
}

/** @var \Drupal\helfi_api_base\Environment\Environment $environment */
$environment = \Drupal::service('helfi_api_base.environment_resolver')->getActiveEnvironment();
try {
/** @var \Drupal\helfi_api_base\Environment\Environment $environment */
$environment = \Drupal::service('helfi_api_base.environment_resolver')->getActiveEnvironment();
} catch (\InvalidArgumentException) {
return;
}

/** @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']);
try {
$baseUrl = $environment->getUrl($link['langcode']);
} catch (\InvalidArgumentException) {
// Base url not found for given langcode.
continue;
}

$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)) {
if ($pathMatcher->matchPath($url, $paths)) {
unset($links[$key]);
}
}
Expand Down

0 comments on commit 1d9c336

Please sign in to comment.