Verified Commit 7ca3f9e2 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3411419 by catch, lauriii, larowlan, kevinquillen: Regression from...

Issue #3411419 by catch, lauriii, larowlan, kevinquillen: Regression from #2521800: using machine name element for ListStringItem breaks with existing data
parent 39fb7eb7
Loading
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -100,6 +100,10 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state
        // Workaround for https://drupal.org/i/1300290#comment-12873635.
        \Drupal::service('plugin.manager.element_info')->getInfoProperty('machine_name', '#process', []),
      );
      // Remove #element_validate from the machine name so that any value can be
      // used as a key, while keeping the widget's behavior for generating
      // defaults the same.
      $element['allowed_values']['table'][$delta]['item']['key']['#element_validate'] = [];
    }

    return $element;
@@ -113,6 +117,14 @@ public static function processAllowedValuesKey(array &$element): array {
    array_pop($parents);
    $parents[] = 'label';
    $element['#machine_name']['source'] = $parents;

    // Override the default description which is not applicable to this use of
    // the machine name element given that it allows users to manually enter
    // characters usually not allowed in machine names.
    if (!isset($element['#description'])) {
      $element['#description'] = '';
    }

    return $element;
  }

+4 −3
Original line number Diff line number Diff line
@@ -306,14 +306,15 @@ public function testOptionsAllowedValuesText() {
    $field_storage = FieldStorageConfig::loadByName('node', $this->fieldName);
    $this->assertSame($field_storage->getSetting('allowed_values'), ['zero' => 'Zero']);

    // Check that string values with dots can not be used.
    // Check that string values with special characters can be used.
    $input = [
      'field_storage[subform][settings][allowed_values][table][0][item][key]' => 'zero',
      'field_storage[subform][settings][allowed_values][table][0][item][label]' => 'Zero',
      'field_storage[subform][settings][allowed_values][table][1][item][key]' => 'example.com',
      'field_storage[subform][settings][allowed_values][table][1][item][key]' => '.example #example',
      'field_storage[subform][settings][allowed_values][table][1][item][label]' => 'Example',
    ];
    $this->assertAllowedValuesInput($input, 'The machine-readable name must contain only lowercase letters, numbers, and underscores.', 'String value with dot is not supported.');
    $array = ['zero' => 'Zero', '.example #example' => 'Example'];
    $this->assertAllowedValuesInput($input, $array, '');

    // Check that the same key can only be used once.
    $input = [
+31 −0
Original line number Diff line number Diff line
@@ -375,6 +375,37 @@ public function providerTestOptionsAllowedValues() {
    return $test_cases;
  }

  /**
   * Tests `list_string` machine name with special characters.
   */
  public function testMachineNameSpecialCharacters() {
    $this->fieldName = 'field_options_text';
    $this->createOptionsField('list_string');
    $this->drupalGet($this->adminPath);

    $label_element_name = "field_storage[subform][settings][allowed_values][table][0][item][label]";
    $this->getSession()->getPage()->fillField($label_element_name, 'Hello world');
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->exposeOptionMachineName(1);

    $key_element_name = "field_storage[subform][settings][allowed_values][table][0][item][key]";

    // Ensure that the machine name was generated correctly.
    $this->assertSession()->fieldValueEquals($key_element_name, 'hello_world');

    // Ensure that the machine name can be overridden with a value that includes
    // special characters.
    $this->getSession()->getPage()->fillField($key_element_name, '.hello #world');
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->getSession()->getPage()->pressButton('Save settings');
    $this->assertSession()->statusMessageContains("Saved {$this->fieldName} configuration.");

    // Ensure that the machine name was saved correctly.
    $allowed_values = FieldStorageConfig::loadByName('node', $this->fieldName)
      ->getSetting('allowed_values');
    $this->assertSame(['.hello #world'], array_keys($allowed_values));
  }

  /**
   * Assert the count of the allowed values rows.
   *