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.*.*.*:
label: 'Include images'
type: boolean
simple_sitemap.custom:
simple_sitemap.custom_links.*:
label: 'Custom links'
type: config_object
mapping:
......
......@@ -172,6 +172,26 @@ function simple_sitemap_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.
*/
......@@ -605,7 +625,6 @@ function simple_sitemap_update_8215() {
$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) {
......@@ -620,27 +639,11 @@ function simple_sitemap_update_8215() {
*/
function simple_sitemap_update_8216() {
$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) {
/** @var \Drupal\Core\Config\Config $config */
$config = $config_factory->getEditable($bundle_config_name);
$config_name_parts = explode('.', $bundle_config_name);
$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();
$config->delete();
......@@ -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.');
}
......@@ -280,14 +280,14 @@ class FormHelper {
}
// Variant
$variants = $this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE);
$variants = array_map(function($variant) { return $variant['label']; }, $variants);
$form_fragment[$prefix . 'simple_sitemap_variant'] = [
'#type' => 'select',
'#title' => $this->t('Sitemap variant'),
'#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,
'#states' => [
'visible' => !$multiple
......
......@@ -32,10 +32,25 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
$form['simple_sitemap_custom']['custom_links'] = [
'#type' => 'textarea',
'#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"),
];
$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'] = [
'#type' => 'select',
'#title' => $this->t('Include images'),
......@@ -49,10 +64,18 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
return parent::buildForm($form, $form_state);
}
protected function negotiateVariant() {
}
/**
* {@inheritdoc}
*/
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) {
$placeholders = [
'@line' => ++$i,
......@@ -90,10 +113,14 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->generator->removeCustomLinks();
foreach ($this->stringToCustomLinks($form_state->getValue('custom_links')) as $link_config) {
$this->generator->addCustomLink($link_config['path'], $link_config);
$this->generator->setVariants(TRUE)->removeCustomLinks();
if (!empty($variants = $form_state->getValue('variants')) && !empty($links = $form_state->getValue('custom_links'))) {
$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'));
parent::submitForm($form, $form_state);
......
......@@ -221,18 +221,14 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#open' => TRUE,
];
$variants = [];
foreach ($this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE) as $variant_name => $variant_definition) {
$variants[$variant_name] = $this->t($variant_definition['label']);
}
$variants = $this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE);
$default_variant = $this->generator->getSetting('default_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,
'#options' => ['' => $this->t('- None -')] + array_map(function($variant) { return $this->t($variant['label']); }, $variants),
];
$form['simple_sitemap_settings']['advanced']['base_url'] = [
......
......@@ -296,7 +296,6 @@ class Simplesitemap {
[':id' => $id])->fetchObject();
}
/**
* @return $this
* @throws \Drupal\Component\Plugin\Exception\PluginException
......@@ -304,10 +303,9 @@ class Simplesitemap {
* @todo document
*/
public function removeSitemap() {
$variants = $this->getVariants();
$saved_variants = $this->manager->getSitemapVariants();
$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))
: $saved_variants;
......@@ -324,7 +322,6 @@ class Simplesitemap {
return $this;
}
/**
* @param string $from
* @return $this
......@@ -512,7 +509,7 @@ class Simplesitemap {
* or for all entity types and their bundles.
* 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 = [];
......@@ -556,7 +553,7 @@ class Simplesitemap {
}
}
}
if ($multipleVariants) {
if ($multiple_variants) {
if (!empty($bundle_settings)) {
$all_bundle_settings[$variant] = $bundle_settings;
}
......@@ -782,9 +779,12 @@ class Simplesitemap {
* @return $this
*
* @todo Validate $settings and throw exceptions
* @todo: variants
*/
public function addCustomLink($path, $settings = []) {
if (empty($variants = $this->getVariants(FALSE))) {
return $this;
}
if (!(bool) $this->pathValidator->getUrlIfValidWithoutAccessCheck($path)) {
// todo: log error.
return $this;
......@@ -794,17 +794,25 @@ class Simplesitemap {
return $this;
}
$custom_links = $this->getCustomLinks(FALSE);
foreach ($custom_links as $key => $link) {
if ($link['path'] === $path) {
$link_key = $key;
break;
$variant_links = $this->getCustomLinks(NULL, FALSE, TRUE);
foreach ($variants as $variant) {
$links = [];
$link_key = 0;
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;
}
......@@ -812,71 +820,87 @@ class Simplesitemap {
/**
* Returns an array of custom paths and their sitemap settings.
*
* @param bool $supplement_default_settings
* @param bool $supplement_defaults
* @return array
*
* @todo: variants
*/
public function getCustomLinks($supplement_default_settings = TRUE) {
$custom_links = $this->configFactory
->get('simple_sitemap.custom')
->get('links');
public function getCustomLinks($path = NULL, $supplement_defaults = TRUE, $multiple_variants = FALSE) {
$all_custom_links = [];
foreach ($variants = $this->getVariants(FALSE) as $variant) {
$custom_links = $this->configFactory
->get("simple_sitemap.custom_links.$variant")
->get('links');
if ($supplement_default_settings) {
foreach ($custom_links as $i => $link_settings) {
self::supplementDefaultSettings('custom', $link_settings);
$custom_links[$i] = $link_settings;
$custom_links = !empty($custom_links) ? $custom_links : [];
if (!empty($custom_links) && $path !== NULL) {
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;
}
}
/**
* Returns settings for a custom path added to the sitemap settings.
*
* @param string $path
*
* @return array|false
*
* @todo: variants
*/
public function getCustomLink($path) {
foreach ($this->getCustomLinks() as $key => $link) {
if ($link['path'] === $path) {
return $link;
$custom_links = $path !== NULL && !empty($custom_links)
? array_values($custom_links)[0]
: array_values($custom_links);
if (!empty($custom_links)) {
if ($multiple_variants) {
$all_custom_links[$variant] = $custom_links;
}
else {
return $custom_links;
}
}
}
return FALSE;
return $all_custom_links;
}
/**
* Removes all custom paths from the sitemap settings.
*
* @return $this
*
* @todo: variants
*/
public function removeCustomLinks($paths = NULL) {
if (empty($variants = $this->getVariants(FALSE))) {
return $this;
}
if (NULL === $paths) {
$custom_links = [];
$save = TRUE;
foreach ($variants as $variant) {
$this->configFactory
->getEditable("simple_sitemap.custom_links.$variant")->delete();
}
}
else {
$custom_links = $this->getCustomLinks(FALSE);
foreach ((array) $paths as $path) {
foreach ($custom_links as $key => $link) {
if ($link['path'] === $path) {
unset($custom_links[$key]);
$save = TRUE;
$variant_links = $this->getCustomLinks(NULL, FALSE, TRUE);
foreach ($variant_links as $variant => $links) {
$custom_links = $links;
$save = FALSE;
foreach ((array) $paths as $path) {
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;
}
......
......@@ -216,7 +216,7 @@ class SimplesitemapManager {
if (NULL === $variant_names) {
// 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) {
$this->configFactory->getEditable($config_name)->delete();
}
......@@ -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_variants = [];
$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