From 54efc8e7b6d183fb191dac2c02baa2ad0feeb14f Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Wed, 22 Feb 2023 11:32:26 +0200 Subject: [PATCH 01/13] UHF-8103: sitemap as dependency --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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" } } From 6ad0bc7309d2d6a595d35011cbd7a5ec5b37a130 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Wed, 22 Feb 2023 17:23:03 +0200 Subject: [PATCH 02/13] UHF-8103: intermediate commit --- helfi_proxy.info.yml | 1 + helfi_proxy.module | 1 + src/Config/SitemapPathOverride.php | 70 ++++++++++++++++++++++++++++++ src/HelfiProxyServiceProvider.php | 9 ++++ 4 files changed, 81 insertions(+) create mode 100644 src/Config/SitemapPathOverride.php 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..feadf0b 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -27,6 +27,7 @@ function helfi_proxy_module_implements_alter(&$implementations, $hook) : void { * Implements hook_file_url_alter(). */ function helfi_proxy_file_url_alter(&$uri) : void { + $conf = \Drupal::configFactory()->getEditable('simple_sitemap.settings'); /** @var \Drupal\helfi_proxy\ProxyManagerInterface $service */ $service = \Drupal::service('helfi_proxy.proxy_manager'); $uri = $service->processPath($uri); diff --git a/src/Config/SitemapPathOverride.php b/src/Config/SitemapPathOverride.php new file mode 100644 index 0000000..e7afcba --- /dev/null +++ b/src/Config/SitemapPathOverride.php @@ -0,0 +1,70 @@ +getSchemeAndHttpHost(); + $prefix = $this->prefix->getPrefix('fi'); + $langcode = 'fi'; + $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..91a3331 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,14 @@ 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.proxy_manager')) + ->addArgument(new Reference('helfi_proxy.active_prefix')); + } + } } From 2e99df33c639ce40ee5e0def144538ae6c1da48e Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Wed, 22 Feb 2023 17:42:49 +0200 Subject: [PATCH 03/13] UHF-8103: langcode --- src/Config/SitemapPathOverride.php | 11 ++++++----- src/HelfiProxyServiceProvider.php | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Config/SitemapPathOverride.php b/src/Config/SitemapPathOverride.php index e7afcba..d015418 100644 --- a/src/Config/SitemapPathOverride.php +++ b/src/Config/SitemapPathOverride.php @@ -7,6 +7,7 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Config\ConfigFactoryOverrideInterface; use Drupal\Core\Config\StorageInterface; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\helfi_proxy\ActiveSitePrefix; use Drupal\helfi_proxy\ProxyManagerInterface; @@ -18,14 +19,14 @@ class SitemapPathOverride implements ConfigFactoryOverrideInterface { /** * Constructs a new instance. * - * @param \Drupal\helfi_proxy\ProxyManagerInterface $proxyManager - * The proxy manager. * @param \Drupal\helfi_proxy\ActiveSitePrefix $prefix * The active site prefix service. + * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager + * The language manager. */ public function __construct( - private ProxyManagerInterface $proxyManager, private ActiveSitePrefix $prefix, + private LanguageManagerInterface $languageManager, ) { } @@ -37,8 +38,8 @@ public function loadOverrides($names): array { if (in_array('simple_sitemap.settings', $names)) { $url = \Drupal::request()->getSchemeAndHttpHost(); - $prefix = $this->prefix->getPrefix('fi'); - $langcode = 'fi'; + $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; } diff --git a/src/HelfiProxyServiceProvider.php b/src/HelfiProxyServiceProvider.php index 91a3331..05669a7 100644 --- a/src/HelfiProxyServiceProvider.php +++ b/src/HelfiProxyServiceProvider.php @@ -34,8 +34,8 @@ public function register(ContainerBuilder $container) { if (isset($modules['simple_sitemap'])) { $container->register('helfi_proxy.sitemap_path_override', SitemapPathOverride::class) ->addTag('config.factory.override') - ->addArgument(new Reference('helfi_proxy.proxy_manager')) - ->addArgument(new Reference('helfi_proxy.active_prefix')); + ->addArgument(new Reference('helfi_proxy.active_prefix')) + ->addArgument(new Reference('language_manager')); } } From 54d9846ad27cd4f4c8a9923ebede704343ea5c01 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Thu, 23 Feb 2023 08:50:06 +0200 Subject: [PATCH 04/13] UHF-8103: request stack as dependency --- helfi_proxy.module | 1 - src/Config/SitemapPathOverride.php | 8 +++++++- src/HelfiProxyServiceProvider.php | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/helfi_proxy.module b/helfi_proxy.module index feadf0b..fa91826 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -27,7 +27,6 @@ function helfi_proxy_module_implements_alter(&$implementations, $hook) : void { * Implements hook_file_url_alter(). */ function helfi_proxy_file_url_alter(&$uri) : void { - $conf = \Drupal::configFactory()->getEditable('simple_sitemap.settings'); /** @var \Drupal\helfi_proxy\ProxyManagerInterface $service */ $service = \Drupal::service('helfi_proxy.proxy_manager'); $uri = $service->processPath($uri); diff --git a/src/Config/SitemapPathOverride.php b/src/Config/SitemapPathOverride.php index d015418..0df4e37 100644 --- a/src/Config/SitemapPathOverride.php +++ b/src/Config/SitemapPathOverride.php @@ -7,6 +7,7 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Config\ConfigFactoryOverrideInterface; use Drupal\Core\Config\StorageInterface; +use Drupal\Core\Http\RequestStack; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\helfi_proxy\ActiveSitePrefix; use Drupal\helfi_proxy\ProxyManagerInterface; @@ -27,6 +28,7 @@ class SitemapPathOverride implements ConfigFactoryOverrideInterface { public function __construct( private ActiveSitePrefix $prefix, private LanguageManagerInterface $languageManager, + private RequestStack $requestStack, ) { } @@ -35,9 +37,13 @@ public function __construct( */ public function loadOverrides($names): array { $overrides = []; + $url = $this->requestStack->getCurrentRequest()?->getSchemeAndHttpHost(); + + if (!$url) { + return $overrides; + } if (in_array('simple_sitemap.settings', $names)) { - $url = \Drupal::request()->getSchemeAndHttpHost(); $langcode = $this->languageManager->getCurrentLanguage()->getId(); $prefix = $this->prefix->getPrefix($langcode); $baseUrl = sprintf('%s/%s/%s', $url, $langcode, $prefix); diff --git a/src/HelfiProxyServiceProvider.php b/src/HelfiProxyServiceProvider.php index 05669a7..8d0d14b 100644 --- a/src/HelfiProxyServiceProvider.php +++ b/src/HelfiProxyServiceProvider.php @@ -35,7 +35,8 @@ public function register(ContainerBuilder $container) { $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('language_manager')) + ->addArgument(new Reference('request_stack')); } } From 1df574c66bf0311eed13ba6abb3c1f14829b3df8 Mon Sep 17 00:00:00 2001 From: rpnykanen <48206082+rpnykanen@users.noreply.github.com> Date: Thu, 23 Feb 2023 09:37:58 +0200 Subject: [PATCH 05/13] code fixes --- src/Config/SitemapPathOverride.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Config/SitemapPathOverride.php b/src/Config/SitemapPathOverride.php index 0df4e37..b8d7726 100644 --- a/src/Config/SitemapPathOverride.php +++ b/src/Config/SitemapPathOverride.php @@ -10,11 +10,10 @@ use Drupal\Core\Http\RequestStack; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\helfi_proxy\ActiveSitePrefix; -use Drupal\helfi_proxy\ProxyManagerInterface; /** -* Override sitemap path dynamically. -*/ + * Override sitemap path dynamically. + */ class SitemapPathOverride implements ConfigFactoryOverrideInterface { /** @@ -24,6 +23,8 @@ class SitemapPathOverride implements ConfigFactoryOverrideInterface { * The active site prefix service. * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager * The language manager. + * @param \Drupal\Core\Http\RequestStack $requestStack + * The request stack. */ public function __construct( private ActiveSitePrefix $prefix, @@ -33,8 +34,8 @@ public function __construct( } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function loadOverrides($names): array { $overrides = []; $url = $this->requestStack->getCurrentRequest()?->getSchemeAndHttpHost(); @@ -54,22 +55,22 @@ public function loadOverrides($names): array { } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getCacheSuffix() { return 'SitemapPathOverride'; } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getCacheableMetadata($name) { return new CacheableMetadata(); } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) { return NULL; } From 98d2177bbe76db6794c8893b3445165be82f3876 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Fri, 24 Feb 2023 08:24:10 +0200 Subject: [PATCH 06/13] UHF-8103: strip excess stuff from url --- helfi_proxy.module | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/helfi_proxy.module b/helfi_proxy.module index fa91826..de92e64 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -82,3 +82,18 @@ 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) { + foreach($links as $key => &$link) { + $url =& $link['url']; + $link['url'] = preg_replace('/\/fi\/.*?\//','/', $url, 1); + $alternateUrls =& $link['alternate_urls']; + foreach($alternateUrls as $key => &$alternateUrl) { + $link['alternate_urls'][$key] = preg_replace('/\/fi\/.*?\//','/', $alternateUrl, 1); + } + $links[$key] = $link; + } +} From c927a34f21dfb2211984022a058cd022888ece57 Mon Sep 17 00:00:00 2001 From: rpnykanen <48206082+rpnykanen@users.noreply.github.com> Date: Mon, 27 Feb 2023 08:17:50 +0200 Subject: [PATCH 07/13] code fixes --- helfi_proxy.module | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/helfi_proxy.module b/helfi_proxy.module index de92e64..1ce1779 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -87,12 +87,12 @@ function helfi_proxy_page_attachments_alter(array &$attachments) { * Implements hook_page_attachments_alter(). */ function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) { - foreach($links as $key => &$link) { + foreach ($links as $key => &$link) { $url =& $link['url']; - $link['url'] = preg_replace('/\/fi\/.*?\//','/', $url, 1); + $link['url'] = preg_replace('/\/fi\/.*?\//', '/', $url, 1); $alternateUrls =& $link['alternate_urls']; foreach($alternateUrls as $key => &$alternateUrl) { - $link['alternate_urls'][$key] = preg_replace('/\/fi\/.*?\//','/', $alternateUrl, 1); + $link['alternate_urls'][$key] = preg_replace('/\/fi\/.*?\//', '/', $alternateUrl, 1); } $links[$key] = $link; } From 327b33341ed59bf33b1fdebf7668d4a6b8bd5c8f Mon Sep 17 00:00:00 2001 From: rpnykanen <48206082+rpnykanen@users.noreply.github.com> Date: Mon, 27 Feb 2023 09:03:14 +0200 Subject: [PATCH 08/13] code fixes --- helfi_proxy.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helfi_proxy.module b/helfi_proxy.module index 1ce1779..11a858d 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -91,7 +91,7 @@ function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) $url =& $link['url']; $link['url'] = preg_replace('/\/fi\/.*?\//', '/', $url, 1); $alternateUrls =& $link['alternate_urls']; - foreach($alternateUrls as $key => &$alternateUrl) { + foreach ($alternateUrls as $key => &$alternateUrl) { $link['alternate_urls'][$key] = preg_replace('/\/fi\/.*?\//', '/', $alternateUrl, 1); } $links[$key] = $link; From e06efd994a4d5e8a2b248f2b1581ccebc3185f23 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 27 Feb 2023 09:21:19 +0200 Subject: [PATCH 09/13] UHF-8103: key mismatch --- helfi_proxy.module | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/helfi_proxy.module b/helfi_proxy.module index 11a858d..dbe0e58 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -88,11 +88,11 @@ function helfi_proxy_page_attachments_alter(array &$attachments) { */ function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) { foreach ($links as $key => &$link) { - $url =& $link['url']; + $url = &$link['url']; $link['url'] = preg_replace('/\/fi\/.*?\//', '/', $url, 1); - $alternateUrls =& $link['alternate_urls']; - foreach ($alternateUrls as $key => &$alternateUrl) { - $link['alternate_urls'][$key] = preg_replace('/\/fi\/.*?\//', '/', $alternateUrl, 1); + $alternateUrls = &$link['alternate_urls']; + foreach ($alternateUrls as $alternateurl_key => $alternateUrl) { + $link['alternate_urls'][$alternateurl_key] = preg_replace('/\/fi\/.*?\//', '/', $alternateUrl, 1); } $links[$key] = $link; } From f381c32617a6f8448b0b4ca220f002fac708df58 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 27 Feb 2023 14:08:12 +0200 Subject: [PATCH 10/13] UHF-8103: overwrite url by admin language, run only for default sitemap and only if base_url is set --- helfi_proxy.module | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/helfi_proxy.module b/helfi_proxy.module index dbe0e58..ed8869d 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -87,13 +87,25 @@ function helfi_proxy_page_attachments_alter(array &$attachments) { * Implements hook_page_attachments_alter(). */ function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) { - foreach ($links as $key => &$link) { - $url = &$link['url']; - $link['url'] = preg_replace('/\/fi\/.*?\//', '/', $url, 1); - $alternateUrls = &$link['alternate_urls']; - foreach ($alternateUrls as $alternateurl_key => $alternateUrl) { - $link['alternate_urls'][$alternateurl_key] = preg_replace('/\/fi\/.*?\//', '/', $alternateUrl, 1); + 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; } - $links[$key] = $link; } + } + From 86f65e33754a599f9cb11449e4efcc3766337a86 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 27 Feb 2023 14:25:32 +0200 Subject: [PATCH 11/13] UHF-8103: code fixes --- helfi_proxy.module | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/helfi_proxy.module b/helfi_proxy.module index ed8869d..214bbeb 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -100,7 +100,7 @@ function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) $url = &$link['url']; $link['url'] = preg_replace($pattern, '/', $url, 1); $alternateUrls = &$link['alternate_urls']; - foreach($alternateUrls as $alternateurl_key => &$alternateUrl) { + foreach ($alternateUrls as $alternateurl_key => $alternateUrl) { $link['alternate_urls'][$alternateurl_key] = preg_replace($pattern, '/', $alternateUrl, 1); } $links[$key] = $link; @@ -108,4 +108,3 @@ function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) } } - From 67e7689daa6c081d794fd750e53e8f51c86bf61c Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 28 Feb 2023 08:47:56 +0200 Subject: [PATCH 12/13] UHF-8103: returns empty string if admin language not set --- helfi_proxy.module | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/helfi_proxy.module b/helfi_proxy.module index 214bbeb..94f354a 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -91,17 +91,19 @@ function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) return; } - $userPreferredAdminLangcode = \drupal::currentUser()->getPreferredAdminLangcode(FALSE) ?? 'fi'; + $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']; + $x = preg_replace($pattern, '/', $url, 1); $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); + //$link['alternate_urls'][$alternateurl_key] = preg_replace($pattern, '/', $alternateUrl, 1); } $links[$key] = $link; } From f737f714640bc17c50e429771142a2637499a96b Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 28 Feb 2023 08:55:52 +0200 Subject: [PATCH 13/13] UHF-8103: remove debug code --- helfi_proxy.module | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/helfi_proxy.module b/helfi_proxy.module index 94f354a..bdfa872 100644 --- a/helfi_proxy.module +++ b/helfi_proxy.module @@ -98,12 +98,10 @@ function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) $pattern = "/\/$userPreferredAdminLangcode\/.*?\//"; foreach ($links as $key => &$link) { $url = &$link['url']; - $x = preg_replace($pattern, '/', $url, 1); $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); + $link['alternate_urls'][$alternateurl_key] = preg_replace($pattern, '/', $alternateUrl, 1); } $links[$key] = $link; }