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

Issue #2990770 by gbyte.co: Choose default sitemap variant in UI

parent cda35dae
......@@ -5,6 +5,7 @@ remove_duplicates: true
skip_untranslated: true
batch_process_limit: 1500
base_url: ''
default_variant: 'default'
custom_links_include_images: false
excluded_languages: []
enabled_entity_types:
......
......@@ -65,7 +65,7 @@ class SimplesitemapController extends ControllerBase {
* @return object
* Returns an XML response.
*/
public function getSitemap(Request $request, $variant = Simplesitemap::DEFAULT_SITEMAP_VARIANT) {
public function getSitemap(Request $request, $variant = NULL) {
$output = $this->generator->getSitemap($variant, $request->query->getInt('page'));
if (!$output) {
$this->cacheKillSwitch->trigger();
......
......@@ -4,6 +4,7 @@ namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\simple_sitemap\Simplesitemap;
/**
* Class SimplesitemapSettingsForm
......@@ -83,6 +84,20 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#open' => TRUE,
];
$variants = [];
foreach ($this->generator->getSitemapVariants(NULL, FALSE) as $name => $info) {
$variants[$name] = $this->t($info['label']);
}
$default_variant = $this->generator->getSetting('default_variant', Simplesitemap::DEFAULT_SITEMAP_VARIANT);
$form['simple_sitemap_settings']['advanced']['default_variant'] = [
'#type' => 'select',
'#title' => $this->t('Default sitemap variant'),
'#description' => $this->t('This sitemap variant will be available under <em>/sitemap.xml</em> in addition to its default path <em>/variant-name/sitemap.xml</em>.<br/>Variants can be configured <a href="@url">here</a>.', ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/variants']),
'#default_value' => isset($variants[$default_variant]) ? $default_variant : '',
'#options' => ['' => $this->t('- None -')] + $variants,
];
$form['simple_sitemap_settings']['advanced']['base_url'] = [
'#type' => 'textfield',
'#title' => $this->t('Default base URL'),
......@@ -171,7 +186,8 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'remove_duplicates',
'skip_untranslated',
'batch_process_limit',
'base_url',] as $setting_name) {
'base_url',
'default_variant'] as $setting_name) {
$this->generator->saveSetting($setting_name, $form_state->getValue($setting_name));
}
$this->generator->saveSetting('excluded_languages', array_filter($form_state->getValue('excluded_languages')));
......
......@@ -94,7 +94,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
$this->languageManager = $language_manager;
$this->time = $time;
$this->writer = $sitemap_writer;
$this->sitemapVariant = Simplesitemap::DEFAULT_SITEMAP_VARIANT;
$this->sitemapVariant = $this->settings['default_variant'];
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
......@@ -121,14 +121,16 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
/**
* @return bool
* @todo: Variant cannot be null
*/
protected function isDefaultVariant() {
return $this->sitemapVariant === Simplesitemap::DEFAULT_SITEMAP_VARIANT;
return $this->sitemapVariant === $this->settings['default_variant'];
}
/**
* @param array $links
* @return string
* @todo: Variant cannot be null
*/
abstract protected function getXml(array $links);
......@@ -145,6 +147,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
* Returns the sitemap index for all sitemap chunks of this type.
*
* @return string
* @todo: Variant cannot be null
*/
protected function getIndexXml(array $chunk_info) {
$this->writer->openMemory();
......@@ -193,6 +196,8 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
*
* @param array $links
* All links with their multilingual versions and settings.
*
* @todo Variant cannot be null
*/
public function generate(array $links) {
$highest_id = $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField();
......@@ -214,6 +219,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
/**
* @throws \Exception
* @todo: Variant cannot be null
*/
public function generateIndex() {
if (!empty($chunk_info = $this->getChunkInfo()) && count($chunk_info) > 1) {
......@@ -239,6 +245,10 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
return $this;
}
/**
* @return $this
* @todo: Variant cannot be null
*/
public function invalidateCache() {
Cache::invalidateTags(['simple_sitemap:' . $this->sitemapVariant]);
......
......@@ -261,7 +261,8 @@ class Simplesitemap {
* links setting. If a sitemap ID is provided, a sitemap chunk is returned.
* Returns false if the sitemap is not retrievable from the database.
*/
public function getSitemap($variant = self::DEFAULT_SITEMAP_VARIANT, $delta = NULL) {
public function getSitemap($variant = NULL, $delta = NULL) {
$variant = NULL !== $variant ? $variant : $this->getSetting('default_variant', self::DEFAULT_SITEMAP_VARIANT);
$chunk_info = $this->fetchSitemapVariantInfo($variant);
if (empty($delta) || !isset($chunk_info[$delta])) {
......@@ -460,6 +461,7 @@ class Simplesitemap {
$settings = [
'base_url' => $this->getSetting('base_url', ''),
'default_variant' => $this->getSetting('default_variant', NULL),
'batch_process_limit' => $this->getSetting('batch_process_limit', 1500),
'max_links' => $this->getSetting('max_links', 2000),
'skip_untranslated' => $this->getSetting('skip_untranslated', FALSE),
......
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