Commit b980612a authored by catch's avatar catch
Browse files

Issue #3398974 by alexpott, Wim Leers, borisson_: Follow-up for #3382510:...

Issue #3398974 by alexpott, Wim Leers, borisson_: Follow-up for #3382510: FormStateInterface::setErrorByName() needs not #name but a variation

(cherry picked from commit 97e6267a)
parent ca47a29e
Loading
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -137,7 +137,6 @@ public function storeConfigKeyToFormElementMap(array $element, FormStateInterfac
      if (is_string($target)) {
        $target = ConfigTarget::fromString($target);
      }
      $target->elementName = $element['#name'];
      $target->elementParents = $element['#parents'];
      $map[$target->configName . ':' . $target->propertyPath] = $target;
      $form_state->set(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP, $map);
@@ -192,7 +191,20 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
          // will not have the sequence index in it.
          $property_path = rtrim($property_path, '0123456789.');
        }
        $form_element_name = $map["$config_name:$property_path"]->elementName;

        if ($property_path === '') {
          // There is a map to a non-existing config key. Try to work backwards.
          $property_path = $violation->getParameters()['@key'] ?? '';
        }

        if (isset($map["$config_name:$property_path"])) {
          $form_element_name = implode('][', $map["$config_name:$property_path"]->elementParents);
        }
        else {
          // We cannot determine where to place the violation. The only option
          // is the entire form.
          $form_element_name = '';
        }
        $violations_per_form_element[$form_element_name][$index] = $violation;
      }

+0 −12
Original line number Diff line number Diff line
@@ -9,18 +9,6 @@
 */
final class ConfigTarget {

  /**
   * The name of the form element which maps to this config property.
   *
   * @var string
   *
   * @see \Drupal\Core\Form\ConfigFormBase::storeConfigKeyToFormElementMap()
   *
   * @internal
   *   This property is for internal use only.
   */
  public string $elementName;

  /**
   * The parents of the form element which maps to this config property.
   *
+6 −0
Original line number Diff line number Diff line
@@ -5,3 +5,9 @@ form_test.object:
    bananas:
      type: string
      label: 'Bananas'
    favorite_vegetable:
      type: required_label
      label: 'Favorite vegetable'
    nemesis_vegetable:
      type: required_label
      label: 'Nemesis vegetable'
+14 −0
Original line number Diff line number Diff line
@@ -528,3 +528,17 @@ form_test.javascript_states_form:
    _form: '\Drupal\form_test\Form\JavascriptStatesForm'
  requirements:
    _access: 'TRUE'

form_test.tree_config_target:
  path: '/form-test/tree-config-target'
  defaults:
    _form: '\Drupal\form_test\Form\TreeConfigTargetForm'
  requirements:
    _access: 'TRUE'

form_test.incorrect_config_target:
  path: '/form-test/incorrect-config-target'
  defaults:
    _form: '\Drupal\form_test\Form\IncorrectConfigTargetForm'
  requirements:
    _access: 'TRUE'
+36 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\form_test\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class IncorrectConfigTargetForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['form_test.object'];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'form_test_incorrect_config_target_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['missing_key'] = [
      '#type' => 'textfield',
      '#title' => t('Missing key'),
      '#config_target' => 'form_test.object:does_not_exist',
    ];
    return parent::buildForm($form, $form_state);
  }

}
Loading