Commit 12f35dd8 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #3024818 by guvCoder, gbyte.co: Set default value for variant dropdown to default variant

parent 578bc40c
......@@ -103,12 +103,12 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
$settings = [
'index' => (bool) $values['simple_sitemap_index_content'],
'priority' => $values['simple_sitemap_priority'],
'changefreq' => $values['simple_sitemap_changefreq'],
'include_images' => (bool) $values['simple_sitemap_include_images'],
];
// Only save other values if indexing is enabled.
if ($settings['index'] = (bool) $values['simple_sitemap_index_content']) {
$settings['priority'] = $values['simple_sitemap_priority'];
$settings['changefreq'] = $values['simple_sitemap_changefreq'];
$settings['include_images'] = (bool) $values['simple_sitemap_include_images'];
}
// Deleting bundle settings for old bundle.
// See SimplesitemapEntitiesForm::submitForm().
......
......@@ -120,6 +120,7 @@ class FormHelper {
$this->cleanUpFormInfo();
$this->getEntityDataFromFormEntity();
$this->negotiateVariant();
return $this->supports();
}
......@@ -222,6 +223,7 @@ class FormHelper {
/**
* @param array $form_fragment
* @return $this
*/
public function displayRegenerateNow(&$form_fragment) {
$form_fragment['simple_sitemap_regenerate_now'] = [
......@@ -233,6 +235,8 @@ class FormHelper {
if ($this->generator->getSetting('cron_generate')) {
$form_fragment['simple_sitemap_regenerate_now']['#description'] .= '<br/>' . $this->t('Otherwise the sitemap will be regenerated during a future cron run.');
}
return $this;
}
protected function negotiateVariant() {
......@@ -248,6 +252,8 @@ class FormHelper {
* @param array $form_fragment
* @param bool $multiple
* @return $this
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function displayEntitySettings(&$form_fragment, $multiple = FALSE) {
$prefix = $multiple ? $this->getEntityTypeId() . '_' : '';
......@@ -288,7 +294,6 @@ class FormHelper {
function($variant) { return $this->t($variant['label']); },
$this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE)
),
'#default_value' => $this->variant,
'#states' => [
'visible' => !$multiple
? [':input[name="' . $prefix . 'simple_sitemap_index_content"]' => ['value' => 1]]
......@@ -299,6 +304,17 @@ class FormHelper {
],
'#disabled' => $this->getEntityCategory() === 'instance'
];
// If only one variant is available, set it, otherwise check if a default
// variant is provided and set it.
$form_fragment[$prefix . 'simple_sitemap_variant']['#default_value'] = NULL === $this->variant
? (1 === count($form_fragment[$prefix . 'simple_sitemap_variant']['#options'])
? array_keys($form_fragment[$prefix . 'simple_sitemap_variant']['#options'])[0]
: (!empty($default = $this->generator->getSetting('default_variant'))
? $default
: $this->variant
)
)
: $this->variant;
// Priority
$form_fragment[$prefix . 'simple_sitemap_priority'] = [
......@@ -437,7 +453,7 @@ class FormHelper {
* Needed because this service may contain form info from the previous
* operation when revived from the container.
*/
protected function cleanUpFormInfo() {
public function cleanUpFormInfo() {
$this->entityCategory = NULL;
$this->entityTypeId = NULL;
$this->bundleName = NULL;
......@@ -474,6 +490,7 @@ class FormHelper {
return TRUE;
}
}
return FALSE;
}
......@@ -489,6 +506,7 @@ class FormHelper {
$value = $this->formatPriority($value / self::PRIORITY_DIVIDER);
$options[$value] = $value;
}
return $options;
}
......@@ -503,6 +521,7 @@ class FormHelper {
foreach (self::getChangefreqOptions() as $setting) {
$options[$setting] = $this->t($setting);
}
return $options;
}
......
......@@ -166,6 +166,7 @@ class Simplesitemap {
*/
public function saveSetting($name, $setting) {
$this->settings->saveSetting($name, $setting);
return $this;
}
......@@ -194,7 +195,9 @@ class Simplesitemap {
*/
public function setVariants($variants = NULL) {
if (NULL === $variants) {
$this->variants = FALSE !== ($default_variant = $this->getSetting('default_variant')) ? [$default_variant] : [];
$this->variants = !empty($default_variant = $this->getSetting('default_variant', ''))
? [$default_variant]
: [];
}
elseif ($variants === TRUE) {
$this->variants = array_keys(
......@@ -269,15 +272,20 @@ class Simplesitemap {
* variant set the above keyed by sitemap delta.
*/
protected function fetchSitemapVariantInfo() {
$result = $this->db->select('simple_sitemap', 's')
->fields('s', ['id', 'delta', 'sitemap_created', 'type'])
->condition('s.status', 1)
->condition('s.type', $this->getVariants(), 'IN')
->execute();
return count($this->getVariants()) > 1
? $result->fetchAllAssoc('type')
: $result->fetchAllAssoc('delta');
if (!empty($this->getVariants())) {
$result = $this->db->select('simple_sitemap', 's')
->fields('s', ['id', 'delta', 'sitemap_created', 'type'])
->condition('s.status', 1)
->condition('s.type', $this->getVariants(), 'IN')
->execute();
return count($this->getVariants()) > 1
? $result->fetchAllAssoc('type')
: $result->fetchAllAssoc('delta');
}
else {
return [];
}
}
/**
......@@ -381,6 +389,7 @@ class Simplesitemap {
$enabled_entity_types[] = $entity_type_id;
$this->saveSetting('enabled_entity_types', $enabled_entity_types);
}
return $this;
}
......@@ -728,11 +737,11 @@ class Simplesitemap {
return unserialize($results);
}
else {
$entity = $this->entityTypeManager->getStorage($entity_type_id)
->load($id);
return $this->getBundleSettings(
$entity_type_id,
$this->entityHelper->getEntityInstanceBundleName($entity)
$this->entityHelper->getEntityInstanceBundleName(
$this->entityTypeManager->getStorage($entity_type_id)->load($id)
)
);
}
}
......@@ -783,6 +792,7 @@ class Simplesitemap {
*/
public function bundleIsIndexed($entity_type_id, $bundle_name = NULL) {
$settings = $this->getBundleSettings($entity_type_id, $bundle_name);
return !empty($settings['index']);
}
......
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