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 8103 sitemap urls #52

Merged
merged 14 commits into from
Feb 28, 2023
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
1 change: 1 addition & 0 deletions helfi_proxy.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ core_version_requirement: ^8 || ^9
dependencies:
- path_alias
- redirect
- drupal:helfi_api_base
78 changes: 78 additions & 0 deletions src/Config/SitemapPathOverride.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_proxy\Config;

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;

/**
* Override sitemap path dynamically.
*/
class SitemapPathOverride implements ConfigFactoryOverrideInterface {

/**
* Constructs a new instance.
*
* @param \Drupal\helfi_proxy\ActiveSitePrefix $prefix
* 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,
private LanguageManagerInterface $languageManager,
private RequestStack $requestStack,
) {
}

/**
* {@inheritdoc}
*/
public function loadOverrides($names): array {
$overrides = [];
$url = $this->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;
}

}
10 changes: 10 additions & 0 deletions src/HelfiProxyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'));
}

}

}