Commit fa3a2f92 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #3056975 by peteamd, gbyte.co: Index page links broken when path...

Issue #3056975 by peteamd, gbyte.co: Index page links broken when path prefixes used in multi-lingual site
parent 1a6ceaeb
......@@ -76,11 +76,16 @@ services:
- '@messenger'
- '@current_user'
simple_sitemap.path_processor_variant:
class: Drupal\simple_sitemap\PathProcessor\PathProcessorSitemapVariant
simple_sitemap.path_processor.variant.in:
class: Drupal\simple_sitemap\PathProcessor\PathProcessorSitemapVariantIn
tags:
- { name: path_processor_inbound, priority: 300 }
simple_sitemap.path_processor.variant.out:
class: Drupal\simple_sitemap\PathProcessor\PathProcessorSitemapVariantOut
tags:
- { name: path_processor_outbound, priority: 300 }
logger.channel.simple_sitemap:
parent: logger.channel_base
public: false
......
......@@ -6,10 +6,10 @@ use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Class PathProcessorSitemapVariant
* Class PathProcessorSitemapVariantIn
* @package Drupal\simple_sitemap\PathProcessor
*/
class PathProcessorSitemapVariant implements InboundPathProcessorInterface {
class PathProcessorSitemapVariantIn implements InboundPathProcessorInterface {
/**
* {@inheritdoc}
......
<?php
namespace Drupal\simple_sitemap\PathProcessor;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Render\BubbleableMetadata;
/**
* Class PathProcessorSitemapVariantOut
* @package Drupal\simple_sitemap\PathProcessor
*/
class PathProcessorSitemapVariantOut implements OutboundPathProcessorInterface {
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
$args = explode('/', $path);
if (count($args) === 4 && $args[3] === 'sitemap.xml') {
$path = '/' . $args[2] . '/sitemap.xml';
}
return $path;
}
}
......@@ -9,6 +9,7 @@ use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Component\Datetime\Time;
use Drupal\Core\Language\LanguageInterface;
/**
* Class SitemapGeneratorBase
......@@ -55,6 +56,11 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
*/
protected $sitemapVariant;
/**
* @var array
*/
protected $sitemapUrlSettings;
/**
* @var array
*/
......@@ -90,6 +96,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
$this->time = $time;
$this->writer = $sitemap_writer;
$this->sitemapVariant = $this->settings['default_variant'];
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
......@@ -167,11 +174,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
$this->writer->startElement('sitemap');
// Build the URL from route to be able to show potentially aliased paths.
$url = $this->isDefaultVariant()
? Url::fromRoute('simple_sitemap.sitemap_default', ['page' => $chunk_data->delta])
: Url::fromRoute('simple_sitemap.sitemap_variant', ['variant' => $chunk_data->type, 'page' => $chunk_data->delta]);
$this->writer->writeElement('loc', $this->getCustomBaseUrl() . $url->toString(TRUE)->getGeneratedUrl());
$this->writer->writeElement('loc', $this->getSitemapUrl($chunk_data->delta, $chunk_data->type));
$this->writer->writeElement('lastmod', date('c', $chunk_data->sitemap_created));
$this->writer->endElement();
}
......@@ -299,6 +302,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
*/
public function setSettings(array $settings) {
$this->settings = $settings;
return $this;
}
......@@ -307,7 +311,33 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
*/
protected function getCustomBaseUrl() {
$customBaseUrl = $this->settings['base_url'];
return !empty($customBaseUrl) ? $customBaseUrl : $GLOBALS['base_url'];
}
protected function getSitemapUrlSettings() {
if (NULL === $this->sitemapUrlSettings) {
$this->sitemapUrlSettings = [
'absolute' => TRUE,
'base_url' => $this->getCustomBaseUrl(),
'language' => $this->languageManager->getLanguage(LanguageInterface::LANGCODE_NOT_APPLICABLE),
];
}
return $this->sitemapUrlSettings;
}
/**
* @param $delta
* @param $variant
* @return \Drupal\Core\GeneratedUrl|string
*/
protected function getSitemapUrl($delta, $variant) {
$url = $this->isDefaultVariant()
? Url::fromRoute('simple_sitemap.sitemap_default', ['page' => $delta], $this->getSitemapUrlSettings())
: Url::fromRoute('simple_sitemap.sitemap_variant', ['variant' => $variant, 'page' => $delta], $this->getSitemapUrlSettings());
return $url->toString();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment