Commit 1528d5a4 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #3002223 by gbyte.co: Make custom links compatible with variants

parent 2b394974
...@@ -56,7 +56,7 @@ simple_sitemap.bundle_settings.*.*.*: ...@@ -56,7 +56,7 @@ simple_sitemap.bundle_settings.*.*.*:
label: 'Include images' label: 'Include images'
type: boolean type: boolean
simple_sitemap.custom: simple_sitemap.custom_links.*:
label: 'Custom links' label: 'Custom links'
type: config_object type: config_object
mapping: mapping:
......
...@@ -172,6 +172,26 @@ function simple_sitemap_schema() { ...@@ -172,6 +172,26 @@ function simple_sitemap_schema() {
return $schema; return $schema;
} }
function _simple_sitemap_update_8216_get_default_variant() {
$config_factory = \Drupal::service('config.factory');
$default_variant = $config_factory->get('simple_sitemap.settings')->get('default_variant');
if (empty($default_variant)) {
$default_variant = 'default';
$config_factory->getEditable('simple_sitemap.settings')
->set('default_variant', $default_variant)
->save();
}
/** @var \Drupal\simple_sitemap\SimplesitemapManager $manager */
$manager = \Drupal::service('simple_sitemap.manager');
$variants = $manager->getSitemapVariants();
if (!isset($variants[$default_variant])) {
$manager->addSitemapVariant($default_variant);
}
return $default_variant;
}
/** /**
* Changing the data structure of the module's configuration. * Changing the data structure of the module's configuration.
*/ */
...@@ -605,7 +625,6 @@ function simple_sitemap_update_8215() { ...@@ -605,7 +625,6 @@ function simple_sitemap_update_8215() {
$config_factory = \Drupal::service('config.factory'); $config_factory = \Drupal::service('config.factory');
foreach ($config_factory->listAll('simple_sitemap.variants.') as $type) { foreach ($config_factory->listAll('simple_sitemap.variants.') as $type) {
/** @var \Drupal\Core\Config\Config $type */
$type = $config_factory->getEditable($type); $type = $config_factory->getEditable($type);
$variants = $type->get('variants'); $variants = $type->get('variants');
foreach($variants as $i => $variant) { foreach($variants as $i => $variant) {
...@@ -620,27 +639,11 @@ function simple_sitemap_update_8215() { ...@@ -620,27 +639,11 @@ function simple_sitemap_update_8215() {
*/ */
function simple_sitemap_update_8216() { function simple_sitemap_update_8216() {
$config_factory = \Drupal::service('config.factory'); $config_factory = \Drupal::service('config.factory');
$default_variant = $config_factory->get('simple_sitemap.settings')->get('default_variant');
if (empty($default_variant)) {
$default_variant = 'default';
$config_factory->getEditable('simple_sitemap.settings')
->set('default_variant', $default_variant)
->save();
}
/** @var \Drupal\simple_sitemap\SimplesitemapManager $manager */
$manager = \Drupal::service('simple_sitemap.manager');
$variants = $manager->getSitemapVariants();
if (!isset($variants[$default_variant])) {
$manager->addSitemapVariant($default_variant);
}
foreach ($config_factory->listAll('simple_sitemap.bundle_settings.') as $bundle_config_name) { foreach ($config_factory->listAll('simple_sitemap.bundle_settings.') as $bundle_config_name) {
/** @var \Drupal\Core\Config\Config $config */
$config = $config_factory->getEditable($bundle_config_name); $config = $config_factory->getEditable($bundle_config_name);
$config_name_parts = explode('.', $bundle_config_name); $config_name_parts = explode('.', $bundle_config_name);
$config_factory->getEditable($config_name_parts[0] . '.' . $config_name_parts[1] $config_factory->getEditable($config_name_parts[0] . '.' . $config_name_parts[1]
. '.' . $default_variant . '.' . $config_name_parts[2] . '.' . $config_name_parts[3]) . '.' . _simple_sitemap_update_8216_get_default_variant() . '.' . $config_name_parts[2] . '.' . $config_name_parts[3])
->setData($config->get())->save(); ->setData($config->get())->save();
$config->delete(); $config->delete();
...@@ -659,6 +662,17 @@ function simple_sitemap_update_8216() { ...@@ -659,6 +662,17 @@ function simple_sitemap_update_8216() {
] ]
); );
} }
}
/**
* Adding per-variant custom link configuration.
*/
function simple_sitemap_update_8217() {
$config_factory = \Drupal::service('config.factory');
$old_config = $config_factory->getEditable('simple_sitemap.custom');
$config_factory->getEditable('simple_sitemap.custom_links.' . _simple_sitemap_update_8216_get_default_variant())
->setData($old_config->get())->save();
$old_config->delete();
return t('The XML sitemaps need to be regenerated.'); return t('The XML sitemaps need to be regenerated.');
} }
...@@ -280,14 +280,14 @@ class FormHelper { ...@@ -280,14 +280,14 @@ class FormHelper {
} }
// Variant // Variant
$variants = $this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE);
$variants = array_map(function($variant) { return $variant['label']; }, $variants);
$form_fragment[$prefix . 'simple_sitemap_variant'] = [ $form_fragment[$prefix . 'simple_sitemap_variant'] = [
'#type' => 'select', '#type' => 'select',
'#title' => $this->t('Sitemap variant'), '#title' => $this->t('Sitemap variant'),
'#description' => $this->t('The sitemap variant entities of this type are to be indexed in.'), '#description' => $this->t('The sitemap variant entities of this type are to be indexed in.'),
'#options' => $variants, '#options' => array_map(
function($variant) { return $this->t($variant['label']); },
$this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE)
),
'#default_value' => $this->variant, '#default_value' => $this->variant,
'#states' => [ '#states' => [
'visible' => !$multiple 'visible' => !$multiple
......
...@@ -32,10 +32,25 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase { ...@@ -32,10 +32,25 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
$form['simple_sitemap_custom']['custom_links'] = [ $form['simple_sitemap_custom']['custom_links'] = [
'#type' => 'textarea', '#type' => 'textarea',
'#title' => $this->t('Relative Drupal paths'), '#title' => $this->t('Relative Drupal paths'),
'#default_value' => $this->customLinksToString($this->generator->getCustomLinks(FALSE)), '#default_value' => $this->customLinksToString($this->generator->setVariants(TRUE)->getCustomLinks(NULL, FALSE)),
'#description' => $this->t("Please specify drupal internal (relative) paths, one per line. Do not forget to prepend the paths with a '/'.<br/>Optionally link priority <em>(0.0 - 1.0)</em> can be added by appending it after a space.<br/> Optionally link change frequency <em>(always / hourly / daily / weekly / monthly / yearly / never)</em> can be added by appending it after a space.<br/><br/><strong>Examples:</strong><br/><em>/ 1.0 daily</em> -> home page with the highest priority and daily change frequency<br/><em>/contact</em> -> contact page with the default priority and no change frequency information"), '#description' => $this->t("Please specify drupal internal (relative) paths, one per line. Do not forget to prepend the paths with a '/'.<br/>Optionally link priority <em>(0.0 - 1.0)</em> can be added by appending it after a space.<br/> Optionally link change frequency <em>(always / hourly / daily / weekly / monthly / yearly / never)</em> can be added by appending it after a space.<br/><br/><strong>Examples:</strong><br/><em>/ 1.0 daily</em> -> home page with the highest priority and daily change frequency<br/><em>/contact</em> -> contact page with the default priority and no change frequency information"),
]; ];
$form['simple_sitemap_custom']['variants'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#title' => $this->t('Sitemap variants'),
'#description' => $this->t('The sitemap variants to include the above links in.<br/>Variants can be configured <a href="@url">here</a>.', ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/variants']),
'#options' => array_map(
function($variant) { return $this->t($variant['label']); },
$this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE)
),
'#default_value' => array_keys(array_filter(
$this->generator->setVariants(TRUE)->getCustomLinks(NULL, FALSE, TRUE),
function($e) { return !empty($e);})
),
];
$form['simple_sitemap_custom']['include_images'] = [ $form['simple_sitemap_custom']['include_images'] = [
'#type' => 'select', '#type' => 'select',
'#title' => $this->t('Include images'), '#title' => $this->t('Include images'),
...@@ -49,10 +64,18 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase { ...@@ -49,10 +64,18 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
return parent::buildForm($form, $form_state); return parent::buildForm($form, $form_state);
} }
protected function negotiateVariant() {
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateForm(array &$form, FormStateInterface $form_state) { public function validateForm(array &$form, FormStateInterface $form_state) {
if (!empty($form_state->getValue('custom_links')) && empty($form_state->getValue('variants'))) {
$form_state->setErrorByName('variants', $this->t('Custom links must be assigned to at least one sitemap variant.'));
}
foreach ($this->stringToCustomLinks($form_state->getValue('custom_links')) as $i => $link_config) { foreach ($this->stringToCustomLinks($form_state->getValue('custom_links')) as $i => $link_config) {
$placeholders = [ $placeholders = [
'@line' => ++$i, '@line' => ++$i,
...@@ -90,10 +113,14 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase { ...@@ -90,10 +113,14 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$this->generator->removeCustomLinks(); $this->generator->setVariants(TRUE)->removeCustomLinks();
foreach ($this->stringToCustomLinks($form_state->getValue('custom_links')) as $link_config) { if (!empty($variants = $form_state->getValue('variants')) && !empty($links = $form_state->getValue('custom_links'))) {
$this->generator->addCustomLink($link_config['path'], $link_config); $this->generator->setVariants(array_values($variants));
foreach ($this->stringToCustomLinks($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')); $this->generator->saveSetting('custom_links_include_images', (bool) $form_state->getValue('include_images'));
parent::submitForm($form, $form_state); parent::submitForm($form, $form_state);
......
...@@ -221,18 +221,14 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ...@@ -221,18 +221,14 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#open' => TRUE, '#open' => TRUE,
]; ];
$variants = []; $variants = $this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE);
foreach ($this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE) as $variant_name => $variant_definition) {
$variants[$variant_name] = $this->t($variant_definition['label']);
}
$default_variant = $this->generator->getSetting('default_variant'); $default_variant = $this->generator->getSetting('default_variant');
$form['simple_sitemap_settings']['advanced']['default_variant'] = [ $form['simple_sitemap_settings']['advanced']['default_variant'] = [
'#type' => 'select', '#type' => 'select',
'#title' => $this->t('Default sitemap variant'), '#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']), '#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 : '', '#default_value' => isset($variants[$default_variant]) ? $default_variant : '',
'#options' => ['' => $this->t('- None -')] + $variants, '#options' => ['' => $this->t('- None -')] + array_map(function($variant) { return $this->t($variant['label']); }, $variants),
]; ];
$form['simple_sitemap_settings']['advanced']['base_url'] = [ $form['simple_sitemap_settings']['advanced']['base_url'] = [
......
...@@ -296,7 +296,6 @@ class Simplesitemap { ...@@ -296,7 +296,6 @@ class Simplesitemap {
[':id' => $id])->fetchObject(); [':id' => $id])->fetchObject();
} }
/** /**
* @return $this * @return $this
* @throws \Drupal\Component\Plugin\Exception\PluginException * @throws \Drupal\Component\Plugin\Exception\PluginException
...@@ -304,10 +303,9 @@ class Simplesitemap { ...@@ -304,10 +303,9 @@ class Simplesitemap {
* @todo document * @todo document
*/ */
public function removeSitemap() { public function removeSitemap() {
$variants = $this->getVariants();
$saved_variants = $this->manager->getSitemapVariants(); $saved_variants = $this->manager->getSitemapVariants();
$this->moduleHandler->alter('simple_sitemap_variants', $saved_variants); $this->moduleHandler->alter('simple_sitemap_variants', $saved_variants);
$remove_variants = NULL !== $variants $remove_variants = NULL !== ($variants = $this->getVariants(FALSE))
? array_intersect_key($saved_variants, array_flip((array) $variants)) ? array_intersect_key($saved_variants, array_flip((array) $variants))
: $saved_variants; : $saved_variants;
...@@ -324,7 +322,6 @@ class Simplesitemap { ...@@ -324,7 +322,6 @@ class Simplesitemap {
return $this; return $this;
} }
/** /**
* @param string $from * @param string $from
* @return $this * @return $this
...@@ -512,7 +509,7 @@ class Simplesitemap { ...@@ -512,7 +509,7 @@ class Simplesitemap {
* or for all entity types and their bundles. * or for all entity types and their bundles.
* False if entity type does not exist. * False if entity type does not exist.
*/ */
public function getBundleSettings($entity_type_id = NULL, $bundle_name = NULL, $supplement_defaults = TRUE, $multipleVariants = FALSE) { public function getBundleSettings($entity_type_id = NULL, $bundle_name = NULL, $supplement_defaults = TRUE, $multiple_variants = FALSE) {
$all_bundle_settings = []; $all_bundle_settings = [];
...@@ -556,7 +553,7 @@ class Simplesitemap { ...@@ -556,7 +553,7 @@ class Simplesitemap {
} }
} }
} }
if ($multipleVariants) { if ($multiple_variants) {
if (!empty($bundle_settings)) { if (!empty($bundle_settings)) {
$all_bundle_settings[$variant] = $bundle_settings; $all_bundle_settings[$variant] = $bundle_settings;
} }
...@@ -782,9 +779,12 @@ class Simplesitemap { ...@@ -782,9 +779,12 @@ class Simplesitemap {
* @return $this * @return $this
* *
* @todo Validate $settings and throw exceptions * @todo Validate $settings and throw exceptions
* @todo: variants
*/ */
public function addCustomLink($path, $settings = []) { public function addCustomLink($path, $settings = []) {
if (empty($variants = $this->getVariants(FALSE))) {
return $this;
}
if (!(bool) $this->pathValidator->getUrlIfValidWithoutAccessCheck($path)) { if (!(bool) $this->pathValidator->getUrlIfValidWithoutAccessCheck($path)) {
// todo: log error. // todo: log error.
return $this; return $this;
...@@ -794,17 +794,25 @@ class Simplesitemap { ...@@ -794,17 +794,25 @@ class Simplesitemap {
return $this; return $this;
} }
$custom_links = $this->getCustomLinks(FALSE); $variant_links = $this->getCustomLinks(NULL, FALSE, TRUE);
foreach ($custom_links as $key => $link) { foreach ($variants as $variant) {
if ($link['path'] === $path) { $links = [];
$link_key = $key; $link_key = 0;
break; if (isset($variant_links[$variant])) {
$links = $variant_links[$variant];
$link_key = count($links);
foreach ($links as $key => $link) {
if ($link['path'] === $path) {
$link_key = $key;
break;
}
}
} }
$links[$link_key] = ['path' => $path] + $settings;
$this->configFactory->getEditable("simple_sitemap.custom_links.$variant")
->set('links', $links)->save();
} }
$link_key = isset($link_key) ? $link_key : count($custom_links);
$custom_links[$link_key] = ['path' => $path] + $settings;
$this->configFactory->getEditable('simple_sitemap.custom')
->set('links', $custom_links)->save();
return $this; return $this;
} }
...@@ -812,71 +820,87 @@ class Simplesitemap { ...@@ -812,71 +820,87 @@ class Simplesitemap {
/** /**
* Returns an array of custom paths and their sitemap settings. * Returns an array of custom paths and their sitemap settings.
* *
* @param bool $supplement_default_settings * @param bool $supplement_defaults
* @return array * @return array
*
* @todo: variants
*/ */
public function getCustomLinks($supplement_default_settings = TRUE) { public function getCustomLinks($path = NULL, $supplement_defaults = TRUE, $multiple_variants = FALSE) {
$custom_links = $this->configFactory $all_custom_links = [];
->get('simple_sitemap.custom') foreach ($variants = $this->getVariants(FALSE) as $variant) {
->get('links'); $custom_links = $this->configFactory
->get("simple_sitemap.custom_links.$variant")
->get('links');
if ($supplement_default_settings) { $custom_links = !empty($custom_links) ? $custom_links : [];
foreach ($custom_links as $i => $link_settings) {
self::supplementDefaultSettings('custom', $link_settings); if (!empty($custom_links) && $path !== NULL) {
$custom_links[$i] = $link_settings; foreach ($custom_links as $key => $link) {
if ($link['path'] !== $path) {
unset($custom_links[$key]);
}
}
} }
}
return !empty($custom_links) ? $custom_links : []; if (!empty($custom_links) && $supplement_defaults) {
} foreach ($custom_links as $i => $link_settings) {
self::supplementDefaultSettings('custom', $link_settings);
$custom_links[$i] = $link_settings;
}
}
/** $custom_links = $path !== NULL && !empty($custom_links)
* Returns settings for a custom path added to the sitemap settings. ? array_values($custom_links)[0]
* : array_values($custom_links);
* @param string $path
*
* @return array|false if (!empty($custom_links)) {
* if ($multiple_variants) {
* @todo: variants $all_custom_links[$variant] = $custom_links;
*/ }
public function getCustomLink($path) { else {
foreach ($this->getCustomLinks() as $key => $link) { return $custom_links;
if ($link['path'] === $path) { }
return $link;
} }
} }
return FALSE;
return $all_custom_links;
} }
/** /**
* Removes all custom paths from the sitemap settings. * Removes all custom paths from the sitemap settings.
* *
* @return $this * @return $this
*
* @todo: variants
*/ */
public function removeCustomLinks($paths = NULL) { public function removeCustomLinks($paths = NULL) {
if (empty($variants = $this->getVariants(FALSE))) {
return $this;
}
if (NULL === $paths) { if (NULL === $paths) {
$custom_links = []; foreach ($variants as $variant) {
$save = TRUE; $this->configFactory
->getEditable("simple_sitemap.custom_links.$variant")->delete();
}
} }
else { else {
$custom_links = $this->getCustomLinks(FALSE); $variant_links = $this->getCustomLinks(NULL, FALSE, TRUE);
foreach ((array) $paths as $path) { foreach ($variant_links as $variant => $links) {
foreach ($custom_links as $key => $link) { $custom_links = $links;
if ($link['path'] === $path) { $save = FALSE;
unset($custom_links[$key]); foreach ((array) $paths as $path) {
$save = TRUE; foreach ($custom_links as $key => $link) {
if ($link['path'] === $path) {
unset($custom_links[$key]);
$save = TRUE;
break 2;
}
} }
} }
if ($save) {
$this->configFactory->getEditable("simple_sitemap.custom_links.$variant")
->set('links', array_values($custom_links))->save();
}
} }
} }
if (!empty($save)) {
$this->configFactory->getEditable('simple_sitemap.custom')
->set('links', array_values($custom_links))->save();
}
return $this; return $this;
} }
......
...@@ -216,7 +216,7 @@ class SimplesitemapManager { ...@@ -216,7 +216,7 @@ class SimplesitemapManager {
if (NULL === $variant_names) { if (NULL === $variant_names) {
// Remove all variants and their bundle settings. // Remove all variants and their bundle settings.
foreach(['variants', 'bundle_settings'] as $config_name_part) { foreach(['variants', 'bundle_settings', 'custom_links'] as $config_name_part) {
foreach ($this->configFactory->listAll("simple_sitemap.$config_name_part.") as $config_name) { foreach ($this->configFactory->listAll("simple_sitemap.$config_name_part.") as $config_name) {
$this->configFactory->getEditable($config_name)->delete(); $this->configFactory->getEditable($config_name)->delete();
} }
...@@ -230,6 +230,13 @@ class SimplesitemapManager { ...@@ -230,6 +230,13 @@ class SimplesitemapManager {
} }
} }
// Remove custom links for specific variants.
foreach ((array) $variant_names as $variant_name) {
foreach ($this->configFactory->listAll("simple_sitemap.custom_links.$variant_name") as $config_name) {
$this->configFactory->getEditable($config_name)->delete();
}
}
// Remove specific variants from configuration. // Remove specific variants from configuration.
$remove_variants = []; $remove_variants = [];
$variants = $this->getSitemapVariants(); $variants = $this->getSitemapVariants();
......
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