Commit 14bf457f authored by catch's avatar catch
Browse files

Issue #3398891 by alexpott, Wim Leers: Do not require the config in...

Issue #3398891 by alexpott, Wim Leers: Do not require the config in #config_target to be listed in getEditableConfigNames()

(cherry picked from commit c1a42fda)
parent 86c32df7
Loading
Loading
Loading
Loading
Loading
+17 −38
Original line number Diff line number Diff line
@@ -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 {
    foreach (array_keys($map) as $config_name) {
      $config = $this->configFactory()->getEditable($config_name);
      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;
      }
      $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,19 +253,12 @@ 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 {
    $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();
    }
      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;
      }
    }
    $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) {
+19 −0
Original line number Diff line number Diff line
<?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 [];
  }

}
+1 −12
Original line number Diff line number Diff line
@@ -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();
}

/**
+2 −7
Original line number Diff line number Diff line
@@ -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}
   */
+2 −7
Original line number Diff line number Diff line
@@ -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}
   */
Loading