diff --git a/core/lib/Drupal/Core/Form/ConfigFormBase.php b/core/lib/Drupal/Core/Form/ConfigFormBase.php index 0d4460970c903764dccfbbc3ef923fc05a7ac7ca..bc0b50380f3a45e44905ef0c9b1ef01262e5a277 100644 --- a/core/lib/Drupal/Core/Form/ConfigFormBase.php +++ b/core/lib/Drupal/Core/Form/ConfigFormBase.php @@ -97,7 +97,7 @@ public function loadDefaultValuesFromConfig(array $element): array { $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 @@ public function storeConfigKeyToFormElementMap(array $element, FormStateInterfac 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 @@ public function validateForm(array &$form, FormStateInterface $form_state) { 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 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $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 @@ protected function formatMultipleViolationsMessage(string $form_element_name, ar * {@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 @@ public function submitForm(array &$form, FormStateInterface $form_state) { */ 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 0000000000000000000000000000000000000000..91b8cf604cc2ce4dcc1f4d82ac2975d810e4490e --- /dev/null +++ b/core/lib/Drupal/Core/Form/RedundantEditableConfigNamesTrait.php @@ -0,0 +1,19 @@ +<?php + +namespace Drupal\Core\Form; + +/** + * Implements ::getEditableConfigNames() for forms using #config_target. + */ +trait RedundantEditableConfigNamesTrait { + use ConfigFormBaseTrait; + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + // This form uses #config_target instead. + return []; + } + +} diff --git a/core/modules/dblog/dblog.module b/core/modules/dblog/dblog.module index aa3f910f9f3b670a40ca50fdfd8b46a8b99d1eb3..a99b48ec0e8675d877d527fe147aad1773e171ef 100644 --- a/core/modules/dblog/dblog.module +++ b/core/modules/dblog/dblog.module @@ -104,21 +104,10 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo $form['dblog_row_limit'] = [ '#type' => '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 <a href=":cron">cron maintenance task</a>.', [':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 d47a9df1df4ec2aeeab8c503909507af562f765b..3e72fbd41d6b9ac507fcd9b5cc05522131eafa3b 100644 --- a/core/modules/jsonapi/src/Form/JsonApiSettingsForm.php +++ b/core/modules/jsonapi/src/Form/JsonApiSettingsForm.php @@ -5,6 +5,7 @@ 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 @@ * @internal */ class JsonApiSettingsForm extends ConfigFormBase { + use RedundantEditableConfigNamesTrait; /** * {@inheritdoc} @@ -20,13 +22,6 @@ public function getFormId() { 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 e275d8f747017dd72f22db36982c58d49c238fb6..d5aa07b7ce28180286fdb548ee739175e5792815 100644 --- a/core/modules/system/src/Form/FileSystemForm.php +++ b/core/modules/system/src/Form/FileSystemForm.php @@ -7,6 +7,7 @@ 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 @@ * @internal */ class FileSystemForm extends ConfigFormBase { + use RedundantEditableConfigNamesTrait; /** * The date formatter service. @@ -84,13 +86,6 @@ public function getFormId() { 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 d385437f652b84cb0bd6401d29e88eed8be9fb31..9d88a54662f747828c679cec5bb33f08f5af461b 100644 --- a/core/modules/system/src/Form/LoggingForm.php +++ b/core/modules/system/src/Form/LoggingForm.php @@ -4,6 +4,7 @@ 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 @@ * @internal */ class LoggingForm extends ConfigFormBase { + use RedundantEditableConfigNamesTrait; /** * {@inheritdoc} @@ -19,13 +21,6 @@ public function getFormId() { 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 dff144d9b4ab5ab91a3ec5cdce0020232937a1a5..7fc04584bec18373cf59cfd48a258e276b5c4dfe 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\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 @@ * @internal */ class BasicSettingsForm extends ConfigFormBase { + use RedundantEditableConfigNamesTrait; /** * The theme handler. @@ -57,13 +59,6 @@ public function getFormId() { return 'views_ui_admin_settings_basic'; } - /** - * {@inheritdoc} - */ - protected function getEditableConfigNames() { - return ['views.settings']; - } - /** * {@inheritdoc} */