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

Deduplicate code and make sure to set default variant on views form

parent 0f6c533e
......@@ -141,8 +141,8 @@ class SimpleSitemapDisplayExtender extends DisplayExtenderPluginBase {
'#type' => 'select',
'#title' => $this->t('Sitemap variant'),
'#description' => $this->t('The sitemap variant display is to be indexed in.'),
'#default_value' => $settings['variant'],
'#options' => $this->getVariantOptions(),
'#options' => $this->formHelper->getVariantSelectValues(),
'#default_value' => $this->formHelper->getVariantSelectValuesDefault($settings['variant']),
'#required' => TRUE,
];
// The sitemap priority.
......@@ -316,20 +316,6 @@ class SimpleSitemapDisplayExtender extends DisplayExtenderPluginBase {
return !empty($settings['index']);
}
/**
* Returns available sitemap variant options.
*
* @return array
* Sitemap variants labels keyed by variant ID.
*/
protected function getVariantOptions() {
$variants = $this->sitemapManager->getSitemapVariants(NULL, FALSE);
foreach ($variants as $id => &$variant) {
$variant = $this->t($variant['label']);
}
return $variants;
}
/**
* Returns available view arguments options.
*
......
......@@ -295,10 +295,8 @@ class FormHelper {
'#type' => 'select',
'#title' => $this->t('Sitemap variant'),
'#description' => $this->t('The sitemap variant entities of this type are to be indexed in.'),
'#options' => array_map(
function($variant) { return $this->t($variant['label']); },
$this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE)
),
'#options' => $this->getVariantSelectValues(),
'#default_value' => $this->getVariantSelectValuesDefault($this->variant),
'#states' => [
'visible' => !$multiple
? [':input[name="' . $prefix . 'simple_sitemap_index_content"]' => ['value' => 1]]
......@@ -307,19 +305,8 @@ class FormHelper {
? [':input[name="' . $prefix . 'simple_sitemap_index_content"]' => ['value' => 1]]
: [':input[name="' . $prefix . 'enabled"]' => ['checked' => TRUE]],
],
'#disabled' => $this->getEntityCategory() === 'instance'
'#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'] = [
......@@ -503,11 +490,47 @@ class FormHelper {
return FALSE;
}
/**
* Gets the values needed to display the variant dropdown setting.
*
* @return array
*/
public function getVariantSelectValues() {
return array_map(
function($variant) { return $this->t($variant['label']); },
$this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE)
);
}
/**
* Returns correct default value for variant select list.
*
* If only one variant is available, return it, otherwise check if a default
* variant is provided and return it.
*
* @param string|null $default_value
* Actual default value from the database.
*
* @return string|null
* Value to be set on form.
*/
public function getVariantSelectValuesDefault($default_value) {
$options = $this->getVariantSelectValues();
return NULL === $default_value
? (1 === count($options)
? array_keys($options)[0]
: (!empty($default = $this->generator->getSetting('default_variant'))
? $default
: $default_value
)
)
: $default_value;
}
/**
* Gets the values needed to display the priority dropdown setting.
*
* @return array
* Select options.
*/
public function getPrioritySelectValues() {
$options = [];
......@@ -523,7 +546,6 @@ class FormHelper {
* Gets the values needed to display the changefreq dropdown setting.
*
* @return array
* Select options.
*/
public function getChangefreqSelectValues() {
$options = ['' => $this->t('- Not specified -')];
......
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