Commit a1b6fbac authored by gbyte.co's avatar gbyte.co

Simplify API

parent ddaeedd0
......@@ -132,7 +132,7 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
// Regenerate sitemaps according to user setting.
if ($values['simple_sitemap_regenerate_now']) {
$generator->setVariants(TRUE)->rebuildQueue()->generateSitemap();
$generator->rebuildQueue()->generateSitemap();
}
}
}
......@@ -159,7 +159,7 @@ function simple_sitemap_cron() {
$state->set('simple_sitemap.last_cron_generate', $request_time);
}
$generator->setVariants(TRUE)->generateSitemap('cron');
$generator->generateSitemap('cron');
}
}
}
......
......@@ -24,6 +24,7 @@ services:
- '@plugin.manager.simple_sitemap.sitemap_type'
- '@plugin.manager.simple_sitemap.url_generator'
- '@plugin.manager.simple_sitemap.sitemap_generator'
- '@simple_sitemap.settings'
simple_sitemap.settings:
class: Drupal\simple_sitemap\SimplesitemapSettings
......
......@@ -32,7 +32,7 @@ class SimplesitemapCommands extends DrushCommands {
* @aliases ss:generate, ssg, simple_sitemap:generate, simple_sitemap-generate
*/
public function generate() {
$this->generator->setVariants(TRUE)->generateSitemap('drush');
$this->generator->generateSitemap('drush');
}
/**
......
......@@ -99,7 +99,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
// Regenerate sitemaps according to user setting.
if ($form_state->getValue('simple_sitemap_regenerate_now')) {
$this->generator->setVariants(TRUE)->rebuildQueue()->generateSitemap();
$this->generator->rebuildQueue()->generateSitemap();
}
}
......
......@@ -135,7 +135,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
// Regenerate sitemaps according to user setting.
if ($form_state->getValue('simple_sitemap_regenerate_now')) {
$this->generator->setVariants(TRUE)->generateSitemap();
$this->generator->generateSitemap();
}
}
......
......@@ -231,7 +231,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
// Regenerate sitemaps according to user setting.
if ($form_state->getValue('simple_sitemap_regenerate_now')) {
$this->generator->setVariants(TRUE)->rebuildQueue()->generateSitemap();
$this->generator->rebuildQueue()->generateSitemap();
}
}
......@@ -240,7 +240,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function generateSitemap(array &$form, FormStateInterface $form_state) {
$this->generator->setVariants(TRUE)->generateSitemap();
$this->generator->generateSitemap();
}
/**
......@@ -248,7 +248,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function generateSitemapBackend (array &$form, FormStateInterface $form_state) {
$this->generator->setVariants(TRUE)->generateSitemap('backend');
$this->generator->generateSitemap('backend');
}
......
......@@ -80,17 +80,31 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Removing all variants will clear the default variant setting, may need to
// restore this setting later.
$default_variant = $this->generator->getSetting('default_variant');
$this->generator->getSitemapManager()->removeSitemapVariants();
$new_variants = $this->stringToVariants($form_state->getValue('variants'));
$weight = 0;
foreach ($this->stringToVariants($form_state->getValue('variants')) as $variant_name => $variant_definition) {
foreach ($new_variants as $variant_name => $variant_definition) {
$this->generator->getSitemapManager()->addSitemapVariant($variant_name, $variant_definition + ['weight' => $weight]);
$weight++;
}
// Restoring the default variant setting in case the default variant has
// not been deleted.
if (isset($new_variants[$default_variant])) {
$this->generator->saveSetting('default_variant', $default_variant);
}
parent::submitForm($form, $form_state);
// Regenerate sitemaps according to user setting.
if ($form_state->getValue('simple_sitemap_regenerate_now')) {
$this->generator->setVariants(TRUE)->rebuildQueue()->generateSitemap();
$this->generator->rebuildQueue()->generateSitemap();
}
}
......
......@@ -122,10 +122,6 @@ class QueueWorker {
return $this;
}
public function getQueue() {
return $this->queue;
}
/**
* @param null $variants
* @return $this
......
......@@ -221,7 +221,7 @@ class Simplesitemap {
*/
protected function getVariants() {
if (NULL === $this->variants) {
$this->setVariants();
$this->setVariants(TRUE);
}
return $this->variants;
......
......@@ -40,6 +40,11 @@ class SimplesitemapManager {
*/
protected $sitemapGeneratorManager;
/**
* @var \Drupal\simple_sitemap\SimplesitemapSettings
*/
protected $settings;
/**
* @var SitemapTypeBase[] $sitemapTypes
*/
......@@ -55,24 +60,26 @@ class SimplesitemapManager {
*/
protected $sitemapGenerators = [];
/**
* SimplesitemapManager constructor.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* @param \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeManager $sitemap_type_manager
* @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $url_generator_manager
* @param \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorManager $sitemap_generator_manager
* @param \Drupal\simple_sitemap\SimplesitemapSettings $settings
*/
public function __construct(
ConfigFactory $config_factory,
SitemapTypeManager $sitemap_type_manager,
UrlGeneratorManager $url_generator_manager,
SitemapGeneratorManager $sitemap_generator_manager
SitemapGeneratorManager $sitemap_generator_manager,
SimplesitemapSettings $settings
) {
$this->configFactory = $config_factory;
$this->sitemapTypeManager = $sitemap_type_manager;
$this->urlGeneratorManager = $url_generator_manager;
$this->sitemapGeneratorManager = $sitemap_generator_manager;
$this->settings = $settings;
}
/**
......@@ -196,6 +203,7 @@ class SimplesitemapManager {
foreach ($this->configFactory->listAll('simple_sitemap.variants.') as $config_name) {
$this->configFactory->getEditable($config_name)->delete();
}
$this->settings->saveSetting('default_variant', '');
}
else {
$remove_variants = [];
......@@ -211,6 +219,9 @@ class SimplesitemapManager {
->set('variants', array_diff_key($this->getSitemapVariants($type, FALSE), $variants_per_type))
->save();
}
if (in_array($this->settings->getSetting('default_variant', ''), $variant_names)) {
$this->settings->saveSetting('default_variant', '');
}
}
return $this;
......
......@@ -475,7 +475,6 @@ class SimplesitemapTest extends SimplesitemapTestBase {
$this->generator->getSitemapManager()->addSitemapVariant('test');
$this->generator
->setVariants(TRUE)
->setBundleSettings('node', 'page')
->generateSitemap('backend');
......
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