diff --git a/composer.json b/composer.json index 2395575..4bffd79 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", "drupal/coder": "^8.3", - "phpspec/prophecy-phpunit": "^2" + "phpspec/prophecy-phpunit": "^2", + "drupal/simple_sitemap": "^4.1" } } diff --git a/helfi_proxy.info.yml b/helfi_proxy.info.yml index c90ad0c..945fc10 100644 --- a/helfi_proxy.info.yml +++ b/helfi_proxy.info.yml @@ -6,3 +6,4 @@ core_version_requirement: ^8 || ^9 dependencies: - path_alias - redirect + - drupal:helfi_api_base diff --git a/helfi_proxy.module b/helfi_proxy.module index fa91826..bdfa872 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -82,3 +82,29 @@ 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) { + if ($sitemap_variant->getOriginalId() != 'default') { + return; + } + + $userPreferredAdminLangcode = \drupal::currentUser()->getPreferredAdminLangcode(FALSE) ?: 'fi'; + + $config = \Drupal::configFactory()->get('simple_sitemap.settings')?->get('base_url'); + if ($config) { + $pattern = "/\/$userPreferredAdminLangcode\/.*?\//"; + foreach ($links as $key => &$link) { + $url = &$link['url']; + $link['url'] = preg_replace($pattern, '/', $url, 1); + $alternateUrls = &$link['alternate_urls']; + foreach ($alternateUrls as $alternateurl_key => $alternateUrl) { + $link['alternate_urls'][$alternateurl_key] = preg_replace($pattern, '/', $alternateUrl, 1); + } + $links[$key] = $link; + } + } + +} diff --git a/src/Config/SitemapPathOverride.php b/src/Config/SitemapPathOverride.php new file mode 100644 index 0000000..b8d7726 --- /dev/null +++ b/src/Config/SitemapPathOverride.php @@ -0,0 +1,78 @@ +requestStack->getCurrentRequest()?->getSchemeAndHttpHost(); + + if (!$url) { + return $overrides; + } + + if (in_array('simple_sitemap.settings', $names)) { + $langcode = $this->languageManager->getCurrentLanguage()->getId(); + $prefix = $this->prefix->getPrefix($langcode); + $baseUrl = sprintf('%s/%s/%s', $url, $langcode, $prefix); + $overrides['simple_sitemap.settings']['base_url'] = $baseUrl; + } + + return $overrides; + } + + /** + * {@inheritdoc} + */ + public function getCacheSuffix() { + return 'SitemapPathOverride'; + } + + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($name) { + return new CacheableMetadata(); + } + + /** + * {@inheritdoc} + */ + public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) { + return NULL; + } + +} diff --git a/src/HelfiProxyServiceProvider.php b/src/HelfiProxyServiceProvider.php index dc7a147..8d0d14b 100644 --- a/src/HelfiProxyServiceProvider.php +++ b/src/HelfiProxyServiceProvider.php @@ -6,6 +6,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceProviderBase; +use Drupal\helfi_proxy\Config\SitemapPathOverride; use Drupal\helfi_proxy\EventSubscriber\TunnistamoRedirectUrlSubscriber; use Symfony\Component\DependencyInjection\Reference; @@ -29,6 +30,15 @@ public function register(ContainerBuilder $container) { ->addArgument(new Reference('helfi_proxy.proxy_manager')) ->addArgument(new Reference('helfi_proxy.active_prefix')); } + + if (isset($modules['simple_sitemap'])) { + $container->register('helfi_proxy.sitemap_path_override', SitemapPathOverride::class) + ->addTag('config.factory.override') + ->addArgument(new Reference('helfi_proxy.active_prefix')) + ->addArgument(new Reference('language_manager')) + ->addArgument(new Reference('request_stack')); + } + } }