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

Issue #2924080 by gbyte.co: Use queue API for cron generation instead of batch API

parent fb255e3b
max_links: 2000
cron_generate: true
cron_generate_interval: 0
generate_duration: 10000
remove_duplicates: true
skip_untranslated: true
batch_process_limit: 1500
base_url: ''
default_variant: 'default'
custom_links_include_images: false
......
......@@ -5,10 +5,13 @@ simple_sitemap.settings:
label: 'Max links'
type: integer
cron_generate:
label: 'Cron generate'
label: 'Cron generation'
type: boolean
cron_generate_interval:
label: 'Cron generate interval'
label: 'Cron generation interval'
type: integer
generate_duration:
label: 'Generation duration'
type: integer
remove_duplicates:
label: 'Remove duplicates'
......@@ -16,9 +19,6 @@ simple_sitemap.settings:
skip_untranslated:
label: 'Skip untranslated'
type: boolean
batch_process_limit:
label: 'Batch process limit'
type: integer
base_url:
label: 'Base URL'
type: string
......@@ -89,3 +89,6 @@ simple_sitemap.variants.*:
label:
label: 'Variant label'
type: string
weight:
label: 'Weight'
type: integer
......@@ -14,6 +14,13 @@ function simple_sitemap_drush_command() {
'callback' => 'drush_simple_sitemap_generate',
'drupal dependencies' => ['simple_sitemap'],
];
$items['simple_sitemap-rebuild-queue'] = [
'description' => 'Rebuild the sitemap queue for all sitemap variants.',
'callback' => 'drush_simple_sitemap_rebuild_queue',
'drupal dependencies' => ['simple_sitemap'],
];
return $items;
}
......@@ -25,3 +32,12 @@ function simple_sitemap_drush_command() {
function drush_simple_sitemap_generate() {
\Drupal::service('simple_sitemap.generator')->generateSitemap('drush');
}
/**
* Callback function for hook_drush_command().
*
* Rebuild the sitemap queue for all sitemap variants.
*/
function drush_simple_sitemap_rebuild_queue() {
\Drupal::service('simple_sitemap.generator')->rebuildQueue();
}
......@@ -5,8 +5,6 @@
* Module install and update procedures.
*/
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
/**
* Implements hook_requirements().
*
......@@ -30,7 +28,7 @@ function simple_sitemap_requirements($phase) {
case 'runtime':
$generator = \Drupal::service('simple_sitemap.generator');
$generated_ago = $generator->getGeneratedAgo(\Drupal\simple_sitemap\Simplesitemap::DEFAULT_SITEMAP_VARIANT); //todo
$generated_ago = $generator->getGeneratedAgo(\Drupal\simple_sitemap\SimplesitemapManager::DEFAULT_SITEMAP_VARIANT); //todo
$cron_generation = $generator->getSetting('cron_generate');
if (!$generated_ago) {
......@@ -104,9 +102,17 @@ function simple_sitemap_schema() {
'sitemap_created' => [
'description' => 'Timestamp of sitemap chunk generation.',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => 0,
],
'status' => [
'description' => "Flag indicating the publishing status of the chunk.",
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => 0,
],
],
'primary key' => ['id'],
......@@ -118,8 +124,8 @@ function simple_sitemap_schema() {
'id' => [
'description' => 'Override unique identifier.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'unsigned' => TRUE,
],
'entity_type' => [
'description' => 'Entity type of the overriding entity.',
......@@ -562,3 +568,39 @@ function simple_sitemap_update_8214() {
$config_factory->getEditable($type)->delete();
}
}
/**
* Adding 'status' field to simple_sitemap table and weight to variants.
*/
function simple_sitemap_update_8215() {
$database = \Drupal::database();
$database->truncate('simple_sitemap')->execute();
if (!$database->schema()->fieldExists('simple_sitemap', 'status')) {
$database->schema()->addField(
'simple_sitemap',
'status', [
'description' => "Flag indicating the publishing status of the chunk.",
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => 0,
]
);
}
$config_factory = \Drupal::service('config.factory');
foreach ($config_factory->listAll('simple_sitemap.variants.') as $type) {
/** @var \Drupal\Core\Config\Config $type */
$type = $config_factory->getEditable($type);
$variants = $type->get('variants');
foreach($variants as $i => $variant) {
$variants[$i]['weight'] = 0;
}
$type->set('variants', $variants)->save();
}
return t('The XML sitemaps need to be regenerated.');
}
......@@ -148,7 +148,7 @@ function simple_sitemap_cron() {
$requestTime = \Drupal::service('datetime.time')->getRequestTime();
if ($interval === 0 || ((\Drupal::state()->get('simple_sitemap.last_cron_generate', 0) + $interval) <= $requestTime)) {
\Drupal::state()->set('simple_sitemap.last_cron_generate', $requestTime);
$generator->generateSitemap('backend');
$generator->generateSitemap('cron');
}
}
}
......
......@@ -4,6 +4,8 @@ services:
public: true
arguments:
- '@simple_sitemap.entity_helper'
- '@simple_sitemap.settings'
- '@simple_sitemap.manager'
- '@config.factory'
- '@database'
- '@entity_type.manager'
......@@ -12,10 +14,40 @@ services:
- '@date.formatter'
- '@datetime.time'
- '@module_handler'
- '@simple_sitemap.batch'
- '@plugin.manager.simple_sitemap.url_generator'
- '@plugin.manager.simple_sitemap.sitemap_generator'
- '@plugin.manager.simple_sitemap.sitemap_type'
- '@simple_sitemap.queue_worker'
simple_sitemap.manager:
class: Drupal\simple_sitemap\SimplesitemapManager
public: true
arguments:
- '@config.factory'
- '@plugin.manager.simple_sitemap.sitemap_type'
- '@plugin.manager.simple_sitemap.url_generator'
- '@plugin.manager.simple_sitemap.sitemap_generator'
simple_sitemap.settings:
class: Drupal\simple_sitemap\SimplesitemapSettings
public: true
arguments:
- '@config.factory'
simple_sitemap.queue_worker:
class: Drupal\simple_sitemap\Queue\QueueWorker
public: true
arguments:
- '@simple_sitemap.settings'
- '@simple_sitemap.manager'
- '@state'
- '@module_handler'
- '@simple_sitemap.element_queue'
- '@lock'
simple_sitemap.element_queue:
class: Drupal\simple_sitemap\Queue\ElementQueue
public: false
arguments:
- 'simple_sitemap_elements'
- '@database'
simple_sitemap.sitemap_writer:
class: Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapWriter
......@@ -36,10 +68,6 @@ services:
- '@simple_sitemap.entity_helper'
- '@current_user'
simple_sitemap.batch:
class: Drupal\simple_sitemap\Batch
public: false
simple_sitemap.logger:
class: Drupal\simple_sitemap\Logger
public: true
......
<?php
namespace Drupal\simple_sitemap;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase;
/**
* Class Batch
* @package Drupal\simple_sitemap\Batch
*
* The services of this class are not injected, as this class looses its state
* on every method call because of how the batch API works.
*/
class Batch {
use StringTranslationTrait;
/**
* @var array
*/
protected $batch;
/**
* @var array
*/
protected $batchMeta;
const BATCH_TITLE = 'Generating XML sitemap';
const BATCH_INIT_MESSAGE = 'Initializing batch...';
const BATCH_ERROR_MESSAGE = 'An error has occurred. This may result in an incomplete XML sitemap.';
const BATCH_PROGRESS_MESSAGE = 'Running @current out of @total operations.';
const REGENERATION_FINISHED_MESSAGE = 'The XML sitemaps have been regenerated.';
const REGENERATION_FINISHED_ERROR_MESSAGE = 'The sitemap generation finished with an error.';
/**
* Batch constructor.
*/
public function __construct() {
$this->batch = [
'title' => $this->t(self::BATCH_TITLE),
'init_message' => $this->t(self::BATCH_INIT_MESSAGE),
'error_message' => $this->t(self::BATCH_ERROR_MESSAGE),
'progress_message' => $this->t(self::BATCH_PROGRESS_MESSAGE),
'operations' => [],
'finished' => [__CLASS__, 'finishGeneration'],
];
}
/**
* @param array $batch_meta
*/
public function setBatchMeta(array $batch_meta) {
$this->batchMeta = $batch_meta;
}
/**
* Adds an operation to the batch.
*
* @param string $operation_name
* @param array $arguments
*/
public function addOperation($operation_name, $arguments = []) {
$this->batch['operations'][] = [
__CLASS__ . '::' . $operation_name, [$arguments, $this->batchMeta]
];
}
/**
* Batch callback function which generates URLs.
*
* @param array $arguments
* @param array $batch_meta
* @param $context
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generateSitemap(array $arguments, array $batch_meta, &$context) {
/** @var UrlGeneratorBase $url_generator*/
$url_generator = \Drupal::service('plugin.manager.simple_sitemap.url_generator')
->createInstance($arguments['url_generator']);
$url_generator
->setContext($context)
->setSettings($arguments['settings'])
->setBatchMeta($batch_meta)
->setSitemapVariant($arguments['variant'])
->setSitemapGeneratorId($arguments['sitemap_generator'])
->generate($arguments['data_set']);
}
/**
* Batch callback function which generates URLs.
*
* @param array $arguments
* @param array $batch_meta
* @param $context
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generateIndex(array $arguments, array $batch_meta, &$context) {
/** @var SitemapGeneratorBase $sitemap_generator*/
$sitemap_generator = \Drupal::service('plugin.manager.simple_sitemap.sitemap_generator')
->createInstance($arguments['sitemap_generator']);
$sitemap_generator
->setSettings($arguments['settings'])
->setSitemapVariant($arguments['variant'])
->generateIndex();
}
/**
* Batch callback function which generates URLs.
*
* @param array $arguments
* @param array $batch_meta
* @param $context
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function removeSitemap(array $arguments, array $batch_meta, &$context) {
/** @var SitemapGeneratorBase $sitemap_generator*/
$sitemap_generator = \Drupal::service('plugin.manager.simple_sitemap.sitemap_generator')
->createInstance($arguments['sitemap_generator']);
$sitemap_generator
->setSitemapVariant($arguments['variant'])
->remove()
->invalidateCache();
}
/**
* Callback function called by the batch API when all operations are finished.
*
* @param $success
* @param $results
* @param $operations
*
* @return bool
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*
* @todo Display success/failure message in Drush > 9.
*/
public static function finishGeneration($success, $results, $operations) {
if ($success) {
\Drupal::service('simple_sitemap.logger')
->m(self::REGENERATION_FINISHED_MESSAGE)
// ['@url' => $this->sitemapGenerator->getCustomBaseUrl() . '/sitemap.xml']) //todo: Use actual base URL for message.
->display('status')
->log('info');
}
else {
\Drupal::service('simple_sitemap.logger')
->m(self::REGENERATION_FINISHED_ERROR_MESSAGE)
->display('error', 'administer sitemap settings')
->log('error');
}
return $success;
}
/**
* Starts the batch process depending on where it was requested from.
*
* @return bool
*/
public function start() {
// Update last operation info for each operation.
$this->addAdditionalMetaInfo();
switch ($this->batchMeta['from']) {
case 'form':
// Start batch process.
batch_set($this->batch);
return TRUE;
case 'drush':
// Start drush batch process.
batch_set($this->batch);
// See https://www.drupal.org/node/638712
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
drush_log($this->t(self::BATCH_INIT_MESSAGE), 'status');
drush_backend_batch_process();
return TRUE;
case 'backend':
// Start backend batch process.
batch_set($this->batch);
// See https://www.drupal.org/node/638712
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
// todo: Does not take advantage of batch API and eventually runs out of memory on very large sites. Use queue API instead?
batch_process();
return TRUE;
case 'nobatch':
// Call each batch operation the way the Drupal batch API would do, but
// within one process (so in fact not using batch API here, just
// mimicking it to avoid code duplication).
$context = [];
foreach ($this->batch['operations'] as $i => $operation) {
$operation[1][] = &$context;
call_user_func_array($operation[0], $operation[1]);
}
return $this->finishGeneration(TRUE, !empty($context['results']) ? $context['results'] : [], []);
}
return FALSE;
}
protected function addAdditionalMetaInfo() {
$last_operation_no = 0;
foreach ($this->batch['operations'] as $i => $operation) {
if ($operation[0] === __CLASS__ . '::generateSitemap') {
$this->batch['operations'][$i][1][1]['current_generate_sitemap_operation_no'] = $i;
$last_operation_no = $i;
}
}
foreach ($this->batch['operations'] as $i => $operation) {
if ($operation[0] === __CLASS__ . '::generateSitemap') {
$this->batch['operations'][$i][1][1]['last_generate_sitemap_operation_no'] = $last_operation_no;
}
}
}
}
......@@ -35,4 +35,15 @@ class SimplesitemapCommands extends DrushCommands {
$this->generator->generateSitemap('drush');
}
/**
* Rebuild the sitemap queue for all sitemap variants.
*
* @command simple-sitemap:rebuild-queue
* @validate-module-enabled simple_sitemap
* @aliases ss:rebuild-queue, ssr, simple_sitemap:rebuild-queue, simple_sitemap-rebuild-queue
*/
public function rebuildQueue() {
$this->generator->rebuildQueue();
}
}
......@@ -55,7 +55,7 @@ class SimplesitemapController extends ControllerBase {
* @param string $variant
* Optional name of sitemap variant.
* @see \hook_simple_sitemap_variants_alter()
* @see Simplesitemap::getSitemapVariants()
* @see SimplesitemapManager::getSitemapVariants()
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
......
......@@ -4,7 +4,7 @@ namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SimplesitemapManager;
/**
* Class SimplesitemapSettingsForm
......@@ -28,18 +28,43 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
$form['simple_sitemap_settings']['regenerate'] = [
'#type' => 'fieldset',
'#title' => $this->t('Regenerate sitemaps'),
'#markup' => '<p>' . $this->t('This will regenerate the XML sitemaps immediately.') . '</p>',
'#title' => $this->t('Generate sitemaps'),
'#markup' => '<p>' . $this->t('Sitemaps can be regenerated on demand here.') . '</p>',
];
$form['simple_sitemap_settings']['regenerate']['regenerate_submit'] = [
'#type' => 'submit',
'#value' => $this->t('Regenerate sitemaps'),
'#value' => $this->t('Generate from queue'),
'#submit' => ['::generateSitemap'],
// Skip form-level validator.
'#validate' => [],
];
// $form['simple_sitemap_settings']['regenerate']['regenerate_backend_submit'] = [
// '#type' => 'submit',
// '#value' => $this->t('Generate from queue (background)'),
// '#submit' => ['::generateSitemapBackend'],
// '#validate' => [],
// ];
$form['simple_sitemap_settings']['regenerate']['rebuild_queue_submit'] = [
'#type' => 'submit',
'#value' => $this->t('Rebuild queue'),
'#submit' => ['::rebuildQueue'],
'#validate' => [],
];
$queue_worker = $this->generator->getQueueWorker();
$indexed_count = $queue_worker->getProcessedElementCount();
$total_count = $queue_worker->getOriginalElementCount();
$index_progress = [
'#theme' => 'progress_bar',
'#percent' => $total_count ? (int) (100 * $indexed_count / $total_count) : 100,
'#message' => t('@indexed out of @total items have been indexed.', ['@indexed' => $indexed_count, '@total' => $total_count]),
];
$form['simple_sitemap_settings']['progress'] = [
'#markup' => render($index_progress),
];
$form['simple_sitemap_settings']['settings'] = [
'#type' => 'fieldset',
'#title' => $this->t('Settings'),
......@@ -78,6 +103,36 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
],
];
$form['simple_sitemap_settings']['settings']['languages'] = [
'#type' => 'details',
'#title' => $this->t('Language settings'),
'#open' => FALSE,
];
$language_options = [];
foreach ($this->languageManager->getLanguages() as $language) {
if (!$language->isDefault()) {
$language_options[$language->getId()] = $language->getName();
}
}
$form['simple_sitemap_settings']['settings']['languages']['skip_untranslated'] = [
'#type' => 'checkbox',
'#title' => $this->t('Skip non-existent translations'),
'#description' => $this->t('If checked, entity links are generated exclusively for languages the entity has been translated to as long as the language is not excluded below.<br/>Otherwise entity links are generated for every language installed on the site apart from languages excluded below.<br/>Bear in mind that non-entity paths like homepage will always be generated for every non-excluded language.'),
'#default_value' => $this->generator->getSetting('skip_untranslated', FALSE),
];
$form['simple_sitemap_settings']['settings']['languages']['excluded_languages'] = [
'#title' => $this->t('Exclude languages'),
'#type' => 'checkboxes',
'#options' => $language_options,
'#description' => !empty($language_options)
? $this->t('There will be no links generated for languages checked here.')
: $this->t('There are no languages other than the default language <a href="@url">available</a>.', ['@url' => $GLOBALS['base_url'] . '/admin/config/regional/language']),
'#default_value' => $this->generator->getSetting('excluded_languages', []),
];
$form['simple_sitemap_settings']['advanced'] = [
'#type' => 'details',
'#title' => $this->t('Advanced settings'),
......@@ -85,10 +140,10 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
];
$variants = [];
foreach ($this->generator->getSitemapVariants(NULL, FALSE) as $name => $info) {
foreach ($this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE) as $name => $info) {
$variants[$name] = $this->t($info['label']);
}
$default_variant = $this->generator->getSetting('default_variant', Simplesitemap::DEFAULT_SITEMAP_VARIANT);
$default_variant = $this->generator->getSetting('default_variant', SimplesitemapManager::DEFAULT_SITEMAP_VARIANT);
$form['simple_sitemap_settings']['advanced']['default_variant'] = [
'#type' => 'select',
......@@ -111,55 +166,26 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#title' => $this->t('Exclude duplicate links'),
'#description' => $this->t('Uncheck this to significantly speed up the sitemap generation process on a huge site (more than 20 000 indexed entities).'),
'#default_value' => $this->generator->getSetting('remove_duplicates', TRUE),
'#access' => FALSE,
];
$form['simple_sitemap_settings']['advanced']['max_links'] = [
'#type' => 'number',
'#title' => $this->t('Maximum links in a sitemap'),
'#min' => 1,
'#description' => $this->t('The maximum number of links one sitemap can hold. If more links are generated than set here, a sitemap index will be created and the links split into several sub-sitemaps.<br/>50 000 links is the maximum Google will parse per sitemap, however it is advisable to set this to a lower number. If left blank, all links will be shown on a single sitemap.'),
'#default_value' => $this->generator->getSetting('max_links', 2000),
'#description' => $this->t('The maximum number of links one sitemap can hold. If more links are generated than set here, a sitemap index will be created and the links split into several sub-sitemaps.<br/>50 000 links is the maximum Google will parse per sitemap, but an equally important consideration is generation performance: Splitting sitemaps into chunks <em>greatly</em> increases it.<br/>If left blank, all links will be shown on a single sitemap.'),
'#default_value' => $this->generator->getSetting('max_links'),
];
$form['simple_sitemap_settings']['advanced']['batch_process_limit'] = [
$form['simple_sitemap_settings']['advanced']['generate_duration'] = [
'#type' => 'number',
'#title' => $this->t('Refresh batch every n links'),
'#title' => $this->t('Sitemap generation max duration'),
'#min' => 1,
'#description' => $this->t('During sitemap generation, the batch process will issue a page refresh after n links processed to prevent PHP timeouts and memory exhaustion.<br/>Increasing this number will reduce the number of times Drupal has to bootstrap (thus speeding up the generation process), but will require more memory and less strict PHP timeout settings.'),
'#default_value' => $this->generator->getSetting('batch_process_limit', 1500),
'#description' => $this->t('The maximum duration in seconds the generation task can run during a single cron run or during one batch process iteration.<br/>The higher the number, the quicker the generation process, but higher the risk of PHP timeout errors.'),
'#default_value' => $this->generator->getSetting('generate_duration', 10000) / 1000,
'#required' => TRUE,
];
$form['simple_sitemap_settings']['advanced']['languages'] = [
'#type' => 'details',
'#title' => $this->t('Language settings'),
'#open' => FALSE,
];
$language_options = [];
foreach ($this->languageManager->getLanguages() as $language) {
if (!$language->isDefault()) {
$language_options[$language->getId()] = $language->getName();
}
}
$form['simple_sitemap_settings']['advanced']['languages']['skip_untranslated'] = [
'#type' => 'checkbox',
'#title' => $this->t('Skip non-existent translations'),
'#description' => $this->t('If checked, entity links are generated exclusively for languages the entity has been translated to as long as the language is not excluded below.<br/>Otherwise entity links are generated for every language installed on the site apart from languages excluded below.<br/>Bear in mind that non-entity paths like homepage will always be generated for every non-excluded language.'),
'#default_value' => $this->generator->getSetting('skip_untranslated', FALSE),
];
$form['simple_sitemap_settings']['advanced']['languages']['excluded_languages'] = [
'#title' => $this->t('Exclude languages'),
'#type' => 'checkboxes',
'#options' => $language_options,
'#description' => !empty($language_options)
? $this->t('There will be no links generated for languages checked here.')
: $this->t('There are no languages other than the default language <a href="@url">available</a>.', ['@url' => $GLOBALS['base_url'] . '/admin/config/regional/language']),
'#default_value' => $this->generator->getSetting('excluded_languages', []),
];
$this->formHelper->displayRegenerateNow($form['simple_sitemap_settings']);
return parent::buildForm($form, $form_state);
......@@ -185,12 +211,12 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'cron_generate_interval',
'remove_duplicates',
'skip_untranslated',
'batch_process_limit',
'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')));
$this->generator->saveSetting('generate_duration', $form_state->getValue('generate_duration') * 1000);
parent::submitForm($form, $form_state);
......@@ -205,7 +231,24 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function generateSitemap(array &$form, FormStateInterface $form_state) {
$this->generator->generateSitemap();
$this->generator->generateSitemap('form');
}
/**
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function generateSitemapBackend (array &$form, FormStateInterface $form_state) {
$this->generator->generateSitemap('backend');
}
/**
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function rebuildQueue(array &$form, FormStateInterface $form_state) {
$this->generator->rebuildQueue();
}
}
......@@ -3,7 +3,7 @@
namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SimplesitemapManager;
/**
* Class SimplesitemapVariantsForm
......@@ -33,11 +33,11 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
$form['simple_sitemap_variants']['variants'] = [
'#type' => 'textarea',
'#title' => $this->t('Variants'),
'#default_value' => $this->variantsToString($this->generator->getSitemapVariants(NULL, TRUE)),
'#description' => $this->t("Please specify sitemap variants, one per line.<br/>A variant definition consists of the variant name (used as the variant's path), the sitemap type it belongs to (optional) and the variant label (optional).<br/>These three values have to be separated by the | pipe | symbol.<br/><br/><strong>Examples:</strong><br/><em>default | default_hreflang | Default</em> -> variant of the <em>default_hreflang</em> sitemap type and <em>Default</em> as label; accessible under <em>/default/sitemap.xml</em><br/><em>test</em> -> variant of the <em>@default_sitemap_type</em> sitemap type and <em>test</em> as label; accessible under <em>/test/sitemap.xml</em><br/><br/><strong>Available sitemap types:</strong>", ['@default_sitemap_type' => Simplesitemap::DEFAULT_SITEMAP_TYPE]),
'#default_value' => $this->variantsToString($this->generator->getSitemapManager()->getSitemapVariants(NULL, TRUE)),
'#description' => $this->t("Please specify sitemap variants, one per line.<br/>A variant definition consists of the variant name (used as the variant's path), the sitemap type it belongs to (optional) and the variant label (optional).<br/>These three values have to be separated by the | pipe | symbol.<br/><br/><strong>Examples:</strong><br/><em>default | default_hreflang | Default</em> -> variant of the <em>default_hreflang</em> sitemap type and <em>Default</em> as label; accessible under <em>/default/sitemap.xml</em><br/><em>test</em> -> variant of the <em>@default_sitemap_type</em> sitemap type and <em>test</em> as label; accessible under <em>/test/sitemap.xml</em><br/><br/><strong>Available sitemap types:</strong>", ['@default_sitemap_type' => SimplesitemapManager::DEFAULT_SITEMAP_TYPE]),
];
foreach ($this->generator->getSitemapTypes() as $sitemap_type => $definition) {
foreach ($this->generator->getSitemapManager()->getSitemapTypes() as $sitemap_type => $definition) {
$form['simple_sitemap_variants']['variants']['#description'] .= '<br/>' . '<em>' . $sitemap_type . '</em>' . (!empty($definition['description']) ? (': ' . $definition['description']) : '');
}
......@@ -53,7 +53,7 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$line = 0;
$sitemap_types = $this->generator->getSitemapTypes();
$sitemap_types = $this->generator->getSitemapManager()->getSitemapTypes();
foreach ($this->stringToVariants($form_state->getValue('varian