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

Improve variant deleting

parent 74425106
......@@ -90,9 +90,8 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$custom_links = $this->stringToCustomLinks($form_state->getValue('custom_links'));
$this->generator->removeCustomLinks();
foreach ($custom_links as $link_config) {
foreach ($this->stringToCustomLinks($form_state->getValue('custom_links')) as $link_config) {
$this->generator->addCustomLink($link_config['path'], $link_config);
}
$this->generator->saveSetting('custom_links_include_images', (bool) $form_state->getValue('include_images'));
......
......@@ -30,18 +30,17 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
'#prefix' => $this->getDonationText(),
];
$type_descriptions = [];
$sitemap_types = $this->generator->getSitemapTypes();
foreach ($sitemap_types as $sitemap_type => $definition) {
$type_descriptions[] = $sitemap_type . (!empty($definition['description']) ? (': ' . $definition['description']) : '');
}
$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><br/>@sitemap_types", ['@default_sitemap_type' => Simplesitemap::DEFAULT_SITEMAP_TYPE, '@sitemap_types' => implode('<br/>', $type_descriptions)]),
'#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]),
];
foreach ($this->generator->getSitemapTypes() as $sitemap_type => $definition) {
$form['simple_sitemap_variants']['variants']['#description'] .= '<br/>' . '<em>' . $sitemap_type . '</em>' . (!empty($definition['description']) ? (': ' . $definition['description']) : '');
}
$this->formHelper->displayRegenerateNow($form['simple_sitemap_custom']);
return parent::buildForm($form, $form_state);
......@@ -79,15 +78,10 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
/**
* {@inheritdoc}
*
* @todo improve code
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($this->generator->getSitemapVariants() as $variant_name => $variant_definition) {
$this->generator->removeSitemapVariant($variant_name);
}
$variants = $this->stringToVariants($form_state->getValue('variants'));
foreach ($variants as $variant_name => $variant_definition) {
$this->generator->removeSitemapVariants();
foreach ($this->stringToVariants($form_state->getValue('variants')) as $variant_name => $variant_definition) {
$this->generator->addSitemapVariant($variant_name, $variant_definition);
}
parent::submitForm($form, $form_state);
......
......@@ -390,22 +390,26 @@ class Simplesitemap {
return $this;
}
/**
* @param $name
* @return $this
*
* @todo document
*/
public function removeSitemapVariant($variant_name) {
$this->removeSitemap($variant_name);
$variants = $this->getSitemapVariants();
foreach($variants as $saved_variant_name => $variant_definition) {
if ($variant_name === $saved_variant_name) {
unset($variants[$variant_name]);
$this->configFactory->getEditable('simple_sitemap.variants.' . $variant_definition['type'])
->set('variants', $this->detachSitemapTypeFromVariants($variants))
public function removeSitemapVariants($variant_names = NULL) {
$this->removeSitemap($variant_names);
if (NULL === $variant_names) {
foreach ($this->configFactory->listAll('simple_sitemap.variants.') as $config_name) {
$this->configFactory->getEditable($config_name)->delete();
}
}
else {
$remove_variants = [];
$variants = $this->getSitemapVariants();
foreach ((array) $variant_names as $variant_name) {
if (isset($variants[$variant_name])) {
$remove_variants[$variants[$variant_name]['type']][$variant_name] = $variant_name;
}
}
foreach ($remove_variants as $type => $variants_per_type) {
$this->configFactory->getEditable("simple_sitemap.variants.$type")
->set('variants', array_diff_key($this->getSitemapVariants($type, FALSE), $variants_per_type))
->save();
break;
}
}
......
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