Commit ad011743 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #2968979 by gbyte.co: Setting values programmatically does not reflect in form

parent 7ff6c510
......@@ -42,7 +42,7 @@ simple_sitemap.bundle_settings.*.*:
mapping:
index:
label: 'Index'
type: integer
type: boolean
priority:
label: 'Priority'
type: string
......@@ -51,7 +51,7 @@ simple_sitemap.bundle_settings.*.*:
type: string
include_images:
label: 'Include images'
type: integer
type: boolean
simple_sitemap.custom:
label: 'Custom links'
......
......@@ -385,12 +385,12 @@ function simple_sitemap_update_8209() {
// Update existing bundle settings.
$config_factory = \Drupal::service('config.factory');
$entity_types = $config_factory->listAll('simple_sitemap.bundle_settings.');
$all_bundle_settings = $config_factory->listAll('simple_sitemap.bundle_settings.');
foreach ($entity_types as $entity_type) {
$config = $config_factory->get($entity_type)->get();
foreach ($all_bundle_settings as $bundle_settings) {
$config = $config_factory->get($bundle_settings)->get();
if (!isset($config['include_images'])) {
$config_factory->getEditable($entity_type)
$config_factory->getEditable($bundle_settings)
->setData($config + ['include_images' => 0])
->save();
}
......@@ -482,4 +482,50 @@ function simple_sitemap_update_8211() {
])->save();
}
/**
* Changing storage data type of 'index' and 'include_images' from integer to boolean.
*/
function simple_sitemap_update_8212() {
// Update existing bundle settings.
$config_factory = \Drupal::service('config.factory');
$all_bundle_settings = $config_factory->listAll('simple_sitemap.bundle_settings.');
foreach ($all_bundle_settings as $bundle_settings) {
$config = $config_factory->get($bundle_settings)->get();
$config['include_images'] = isset($config['include_images'])
? (bool) $config['include_images']
: FALSE;
$config['index'] = isset($config['index'])
? (bool) $config['index']
: FALSE;
$config_factory->getEditable($bundle_settings)->setData($config)->save();
}
// Update existing entity override data.
$results = \Drupal::database()->select('simple_sitemap_entity_overrides', 'o')
->fields('o', ['id', 'inclusion_settings'])
->execute()->fetchAll(\PDO::FETCH_OBJ);
foreach ($results as $row) {
$settings = unserialize($row->inclusion_settings);
if (isset($settings['index'])) {
$settings['index'] = (bool) $settings['index'];
}
if (isset($settings['include_images'])) {
$settings['include_images'] = (bool) $settings['include_images'];
}
\Drupal::database()->update('simple_sitemap_entity_overrides')
->fields(['inclusion_settings' => serialize($settings)])
->condition('id', $row->id)
->execute();
}
}
......@@ -108,10 +108,10 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
$generator = \Drupal::service('simple_sitemap.generator');
$settings = [
'index' => $values['simple_sitemap_index_content'],
'index' => (bool) $values['simple_sitemap_index_content'],
'priority' => $values['simple_sitemap_priority'],
'changefreq' => $values['simple_sitemap_changefreq'],
'include_images' => $values['simple_sitemap_include_images'],
'include_images' => (bool) $values['simple_sitemap_include_images'],
];
switch ($f->getEntityCategory()) {
......
......@@ -237,7 +237,7 @@ class FormHelper {
else {
$settings = $this->generator->getBundleSettings($this->getEntityTypeId(), $this->getBundleName());
}
Simplesitemap::supplementDefaultSettings('entity', $settings, ['index' => 0]);
Simplesitemap::supplementDefaultSettings('entity', $settings);
$bundle_name = !empty($this->getBundleName()) ? $this->getBundleName() : $this->t('undefined');
......@@ -245,7 +245,7 @@ class FormHelper {
if (!$multiple) {
$form_fragment[$prefix . 'simple_sitemap_index_content'] = [
'#type' => 'radios',
'#default_value' => $settings['index'],
'#default_value' => (int) $settings['index'],
'#options' => [
0 => $this->getEntityCategory() === 'instance'
? $this->t('Do not index this @bundle entity', ['@bundle' => $bundle_name])
......@@ -257,7 +257,7 @@ class FormHelper {
];
if ($this->getEntityCategory() === 'instance' && isset($bundle_settings['index'])) {
$form_fragment[$prefix . 'simple_sitemap_index_content']['#options'][$bundle_settings['index']] .= ' <em>(' . $this->t('default') . ')</em>';
$form_fragment[$prefix . 'simple_sitemap_index_content']['#options'][(int) $bundle_settings['index']] .= ' <em>(' . $this->t('default') . ')</em>';
}
}
......@@ -298,12 +298,12 @@ class FormHelper {
'#description' => $this->getEntityCategory() === 'instance'
? $this->t('Determines if images referenced by this @bundle entity should be included in the sitemap.', ['@bundle' => $bundle_name])
: $this->t('Determines if images referenced by entities of this type should be included in the sitemap.'),
'#default_value' => $settings['include_images'],
'#default_value' => (int) $settings['include_images'],
'#options' => [0 => $this->t('No'), 1 => $this->t('Yes')],
];
if ($this->getEntityCategory() === 'instance' && isset($bundle_settings['include_images'])) {
$form_fragment[$prefix . 'simple_sitemap_include_images']['#options'][$bundle_settings['include_images']] .= ' (' . $this->t('default') . ')';
$form_fragment[$prefix . 'simple_sitemap_include_images']['#options'][(int) $bundle_settings['include_images']] .= ' (' . $this->t('default') . ')';
}
return $this;
......
......@@ -40,7 +40,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
'#type' => 'select',
'#title' => $this->t('Include images'),
'#description' => $this->t('If a custom link points to an entity, include its referenced images in the sitemap.'),
'#default_value' => $this->generator->getSetting('custom_links_include_images', 0),
'#default_value' => $this->generator->getSetting('custom_links_include_images', FALSE),
'#options' => [0 => $this->t('No'), 1 => $this->t('Yes')],
];
......@@ -95,7 +95,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
foreach ($custom_links as $link_config) {
$this->generator->addCustomLink($link_config['path'], $link_config);
}
$this->generator->saveSetting('custom_links_include_images', $form_state->getValue('include_images'));
$this->generator->saveSetting('custom_links_include_images', (bool) $form_state->getValue('include_images'));
parent::submitForm($form, $form_state);
// Regenerate sitemaps according to user setting.
......
......@@ -68,8 +68,8 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
$bundle_info = '';
$indexed_bundles = isset($bundle_settings[$entity_type_id])
? implode(array_keys(array_filter($bundle_settings[$entity_type_id], function ($val) {return $val['index'];})), ', ') :
'';
? implode(array_keys(array_filter($bundle_settings[$entity_type_id], function ($val) {return $val['index'];})), ', ')
: '';
if (!$atomic_entity_type) {
$bundle_info .= '<div id="indexed-bundles-' . $css_entity_type_id . '">'
......@@ -122,7 +122,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
'index' => TRUE,
'priority' => $values[$entity_type_id . '_simple_sitemap_priority'],
'changefreq' => $values[$entity_type_id . '_simple_sitemap_changefreq'],
'include_images' => $values[$entity_type_id . '_simple_sitemap_include_images'],
'include_images' => (bool) $values[$entity_type_id . '_simple_sitemap_include_images'],
]);
}
}
......
......@@ -112,7 +112,7 @@ class EntityMenuLinkContentUrlGenerator extends UrlGeneratorBase {
// Skip this bundle if it is to be generated in a different sitemap variant.
if (NULL !== $this->sitemapVariant && isset($bundle_settings['variant'])
&& $bundle_settings['variant'] !== $this->sitemapVariant) {
$bundle_settings['index'] = 0;
$bundle_settings['index'] = FALSE;
}
unset($bundle_settings['variant']);
......@@ -123,7 +123,7 @@ class EntityMenuLinkContentUrlGenerator extends UrlGeneratorBase {
$sitemap_variant = $this->sitemapVariant;
$this->moduleHandler->alter('simple_sitemap_bundle_settings', $bundle_settings, $bundle_context, $sitemap_variant);
if ($bundle_settings['index']) {
if (!empty($bundle_settings['index'])) {
$data_sets[] = $bundle_name;
}
}
......
......@@ -118,7 +118,7 @@ class EntityUrlGenerator extends UrlGeneratorBase {
// Skip this bundle if it is to be generated in a different sitemap variant.
if (NULL !== $this->sitemapVariant && isset($bundle_settings['variant'])
&& $bundle_settings['variant'] !== $this->sitemapVariant) {
$bundle_settings['index'] = 0;
$bundle_settings['index'] = FALSE;
}
unset($bundle_settings['variant']);
......@@ -129,7 +129,7 @@ class EntityUrlGenerator extends UrlGeneratorBase {
$sitemap_variant = $this->sitemapVariant;
$this->moduleHandler->alter('simple_sitemap_bundle_settings', $bundle_settings, $bundle_context, $sitemap_variant);
if ($bundle_settings['index']) {
if (!empty($bundle_settings['index'])) {
$data_sets[] = [
'bundle_settings' => $bundle_settings,
'bundle_name' => $bundle_name,
......
......@@ -96,10 +96,10 @@ class Simplesitemap {
* @var array
*/
protected static $linkSettingDefaults = [
'index' => 1,
'index' => FALSE,
'priority' => 0.5,
'changefreq' => '',
'include_images' => 0,
'include_images' => FALSE,
];
/**
......@@ -550,7 +550,7 @@ class Simplesitemap {
*
* @todo: enableEntityType automatically
*/
public function setBundleSettings($entity_type_id, $bundle_name = NULL, $settings = []) {
public function setBundleSettings($entity_type_id, $bundle_name = NULL, $settings = ['index' => TRUE]) {
$bundle_name = NULL !== $bundle_name ? $bundle_name : $entity_type_id;
if (!empty($old_settings = $this->getBundleSettings($entity_type_id, $bundle_name))) {
......@@ -563,9 +563,6 @@ class Simplesitemap {
$bundle_settings = $this->configFactory
->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name");
foreach ($settings as $setting_key => $setting) {
if ($setting_key === 'index') {
$setting = (int) $setting;
}
$bundle_settings->set($setting_key, $setting);
}
$bundle_settings->save();
......@@ -645,7 +642,7 @@ class Simplesitemap {
if (empty($bundle_settings)) {
if ($this->entityTypeIsEnabled($entity_type_id)
&& isset($this->entityTypeBundleInfo->getBundleInfo($entity_type_id)[$bundle_name])) {
self::supplementDefaultSettings('entity', $bundle_settings, ['index' => 0]);
self::supplementDefaultSettings('entity', $bundle_settings);
}
else {
$bundle_settings = FALSE;
......@@ -666,7 +663,7 @@ class Simplesitemap {
if ($this->entityTypeIsEnabled($type_id)) {
foreach($this->entityTypeBundleInfo->getBundleInfo($type_id) as $bundle => $bundle_definition) {
if (!isset($bundle_settings[$type_id][$bundle])) {
self::supplementDefaultSettings('entity', $bundle_settings[$type_id][$bundle], ['index' => 0]);
self::supplementDefaultSettings('entity', $bundle_settings[$type_id][$bundle]);
}
}
}
......
......@@ -278,19 +278,19 @@ class SimplesitemapTest extends SimplesitemapTestBase {
->generateSitemap('nobatch')
->getSitemap();
$sitemap_chunk = $this->generator->getSitemap(1);
$sitemap_chunk = $this->generator->getSitemap('default', 1);
$sitemap_index2 = $this->generator->saveSetting('batch_process_limit', 1)
->generateSitemap('nobatch')
->getSitemap();
$sitemap_chunk2 = $this->generator->getSitemap(1);
$sitemap_chunk2 = $this->generator->getSitemap('default', 1);
$sitemap_index3 = $this->generator->saveSetting('batch_process_limit', 10)
->generateSitemap('nobatch')
->getSitemap();
$sitemap_chunk3 = $this->generator->getSitemap(1);
$sitemap_chunk3 = $this->generator->getSitemap('default', 1);
$this->assertSame($sitemap_index2, $sitemap_index);
$this->assertSame($sitemap_chunk2, $sitemap_chunk);
......
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