Issue #3398891 by alexpott, Wim Leers: Do not require the config in #config_target to be listed in getEditableConfigNames()
parent
d4c6b3217f
commit
c1a42fda67
|
@ -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) {
|
||||
|
|
|
@ -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 [];
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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}
|
||||
*/
|
||||
|
|
|
@ -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}
|
||||
*/
|
||||
|
|
|
@ -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}
|
||||
*/
|
||||
|
|
|
@ -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}
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue