Skip to content

Commit

Permalink
Merge pull request #52 from City-of-Helsinki/UHF-8103_sitemap_urls
Browse files Browse the repository at this point in the history
Uhf 8103 sitemap urls
  • Loading branch information
rpnykanen authored Feb 28, 2023
2 parents 467379e + f737f71 commit 625d1ba
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
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
26 changes: 26 additions & 0 deletions helfi_proxy.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

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

}

}

0 comments on commit 625d1ba

Please sign in to comment.