diff --git a/core/lib/Drupal/Core/Form/ConfigFormBase.php b/core/lib/Drupal/Core/Form/ConfigFormBase.php index 0d4460970c9..bc0b50380f3 100644 --- a/core/lib/Drupal/Core/Form/ConfigFormBase.php +++ b/core/lib/Drupal/Core/Form/ConfigFormBase.php @@ -97,7 +97,7 @@ abstract class ConfigFormBase extends FormBase { $target = ConfigTarget::fromString($target); } - $value = $this->config($target->configName)->get($target->propertyPath); + $value = $this->configFactory()->getEditable($target->configName)->get($target->propertyPath); if ($target->fromConfig) { $value = ($target->fromConfig)($value); } @@ -133,11 +133,12 @@ abstract class ConfigFormBase extends FormBase { if (array_key_exists('#config_target', $element)) { $map = $form_state->get(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP) ?? []; + /** @var \Drupal\Core\Form\ConfigTarget|string $target */ $target = $element['#config_target']; - if ($target instanceof ConfigTarget) { - $target = $target->configName . ':' . $target->propertyPath; + if (is_string($target)) { + $target = ConfigTarget::fromString($target); } - $map[$target] = $element['#array_parents']; + $map[$target->configName][$target->propertyPath] = $element['#array_parents']; $form_state->set(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP, $map); } foreach (Element::children($element) as $key) { @@ -153,18 +154,9 @@ abstract class ConfigFormBase extends FormBase { assert($this->typedConfigManager instanceof TypedConfigManagerInterface); $map = $form_state->get(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP) ?? []; - - foreach ($this->getEditableConfigNames() as $config_name) { - $config = $this->config($config_name); - try { - static::copyFormValuesToConfig($config, $form_state, $form); - } - catch (\BadMethodCallException $e) { - // Nothing to do: this config form does not yet use validation - // constraints. Continue trying the other editable config, to allow - // partial adoption. - continue; - } + foreach (array_keys($map) as $config_name) { + $config = $this->configFactory()->getEditable($config_name); + static::copyFormValuesToConfig($config, $form_state, $form); $typed_config = $this->typedConfigManager->createFromNameAndData($config_name, $config->getRawData()); $violations = $typed_config->validate(); @@ -191,8 +183,8 @@ abstract class ConfigFormBase extends FormBase { $property_path = rtrim($property_path, '0123456789.'); } - if (isset($map["$config_name:$property_path"])) { - $config_target = ConfigTarget::fromForm($map["$config_name:$property_path"], $form); + if (isset($map[$config_name][$property_path])) { + $config_target = ConfigTarget::fromForm($map[$config_name][$property_path], $form); $form_element_name = implode('][', $config_target->elementParents); } else { @@ -261,18 +253,11 @@ abstract class ConfigFormBase extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - foreach ($this->getEditableConfigNames() as $config_name) { - $config = $this->config($config_name); - try { - static::copyFormValuesToConfig($config, $form_state, $form); - $config->save(); - } - catch (\BadMethodCallException $e) { - // Nothing to do: this config form does not yet use validation - // constraints. Continue trying the other editable config, to allow - // partial adoption. - continue; - } + $map = $form_state->get(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP) ?? []; + foreach (array_keys($map) as $config_name) { + $config = $this->configFactory()->getEditable($config_name); + static::copyFormValuesToConfig($config, $form_state, $form); + $config->save(); } $this->messenger()->addStatus($this->t('The configuration options have been saved.')); } @@ -294,15 +279,9 @@ abstract class ConfigFormBase extends FormBase { */ private static function copyFormValuesToConfig(Config $config, FormStateInterface $form_state, array $form): void { $map = $form_state->get(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP); - // If there's no map of config keys to form elements, this form does not - // yet support config validation. - // @see ::validateForm() - if ($map === NULL) { - throw new \BadMethodCallException(); - } - foreach ($map as $element_parents) { - $target = ConfigTarget::fromForm($element_parents, $form); + foreach ($map[$config->getName()] as $array_parents) { + $target = ConfigTarget::fromForm($array_parents, $form); if ($target->configName === $config->getName()) { $value = $form_state->getValue($target->elementParents); if ($target->toConfig) { diff --git a/core/lib/Drupal/Core/Form/RedundantEditableConfigNamesTrait.php b/core/lib/Drupal/Core/Form/RedundantEditableConfigNamesTrait.php new file mode 100644 index 00000000000..91b8cf604cc --- /dev/null +++ b/core/lib/Drupal/Core/Form/RedundantEditableConfigNamesTrait.php @@ -0,0 +1,19 @@ + 'select', '#title' => t('Database log messages to keep'), - '#default_value' => \Drupal::configFactory()->getEditable('dblog.settings')->get('row_limit'), + '#config_target' => 'dblog.settings:row_limit', '#options' => [0 => t('All')] + array_combine($row_limits, $row_limits), '#description' => t('The maximum number of messages to keep in the database log. Requires a cron maintenance task.', [':cron' => Url::fromRoute('system.status')->toString()]), ]; - - $form['#submit'][] = 'dblog_logging_settings_submit'; -} - -/** - * Form submission handler for system_logging_settings(). - * - * @see dblog_form_system_logging_settings_alter() - */ -function dblog_logging_settings_submit($form, FormStateInterface $form_state) { - \Drupal::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save(); } /** diff --git a/core/modules/jsonapi/src/Form/JsonApiSettingsForm.php b/core/modules/jsonapi/src/Form/JsonApiSettingsForm.php index d47a9df1df4..3e72fbd41d6 100644 --- a/core/modules/jsonapi/src/Form/JsonApiSettingsForm.php +++ b/core/modules/jsonapi/src/Form/JsonApiSettingsForm.php @@ -5,6 +5,7 @@ namespace Drupal\jsonapi\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\ConfigTarget; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Form\RedundantEditableConfigNamesTrait; /** * Configure JSON:API settings for this site. @@ -12,6 +13,7 @@ use Drupal\Core\Form\FormStateInterface; * @internal */ class JsonApiSettingsForm extends ConfigFormBase { + use RedundantEditableConfigNamesTrait; /** * {@inheritdoc} @@ -20,13 +22,6 @@ class JsonApiSettingsForm extends ConfigFormBase { return 'jsonapi_settings'; } - /** - * {@inheritdoc} - */ - protected function getEditableConfigNames() { - return ['jsonapi.settings']; - } - /** * {@inheritdoc} */ diff --git a/core/modules/system/src/Form/FileSystemForm.php b/core/modules/system/src/Form/FileSystemForm.php index e275d8f7470..d5aa07b7ce2 100644 --- a/core/modules/system/src/Form/FileSystemForm.php +++ b/core/modules/system/src/Form/FileSystemForm.php @@ -7,6 +7,7 @@ use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Form\RedundantEditableConfigNamesTrait; use Drupal\Core\StreamWrapper\AssetsStream; use Drupal\Core\StreamWrapper\PrivateStream; use Drupal\Core\StreamWrapper\PublicStream; @@ -21,6 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * @internal */ class FileSystemForm extends ConfigFormBase { + use RedundantEditableConfigNamesTrait; /** * The date formatter service. @@ -84,13 +86,6 @@ class FileSystemForm extends ConfigFormBase { return 'system_file_system_settings'; } - /** - * {@inheritdoc} - */ - protected function getEditableConfigNames() { - return ['system.file']; - } - /** * {@inheritdoc} */ diff --git a/core/modules/system/src/Form/LoggingForm.php b/core/modules/system/src/Form/LoggingForm.php index d385437f652..9d88a54662f 100644 --- a/core/modules/system/src/Form/LoggingForm.php +++ b/core/modules/system/src/Form/LoggingForm.php @@ -4,6 +4,7 @@ namespace Drupal\system\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Form\RedundantEditableConfigNamesTrait; /** * Configure logging settings for this site. @@ -11,6 +12,7 @@ use Drupal\Core\Form\FormStateInterface; * @internal */ class LoggingForm extends ConfigFormBase { + use RedundantEditableConfigNamesTrait; /** * {@inheritdoc} @@ -19,13 +21,6 @@ class LoggingForm extends ConfigFormBase { return 'system_logging_settings'; } - /** - * {@inheritdoc} - */ - protected function getEditableConfigNames() { - return ['system.logging']; - } - /** * {@inheritdoc} */ diff --git a/core/modules/views_ui/src/Form/BasicSettingsForm.php b/core/modules/views_ui/src/Form/BasicSettingsForm.php index dff144d9b4a..7fc04584bec 100644 --- a/core/modules/views_ui/src/Form/BasicSettingsForm.php +++ b/core/modules/views_ui/src/Form/BasicSettingsForm.php @@ -7,6 +7,7 @@ use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\Extension\ThemeHandlerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Form\RedundantEditableConfigNamesTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -15,6 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * @internal */ class BasicSettingsForm extends ConfigFormBase { + use RedundantEditableConfigNamesTrait; /** * The theme handler. @@ -57,13 +59,6 @@ class BasicSettingsForm extends ConfigFormBase { return 'views_ui_admin_settings_basic'; } - /** - * {@inheritdoc} - */ - protected function getEditableConfigNames() { - return ['views.settings']; - } - /** * {@inheritdoc} */