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

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

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