Issue #3411419 by catch, lauriii, larowlan, kevinquillen: Regression from #2521800: using machine name element for ListStringItem breaks with existing data

(cherry picked from commit 7ca3f9e2e0)
merge-requests/6452/head
Alex Pott 2023-12-30 16:26:05 +00:00
parent 0d0bcc3bb0
commit 35b420b9ee
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
3 changed files with 47 additions and 3 deletions

View File

@ -100,6 +100,10 @@ class ListStringItem extends ListItemBase {
// 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 @@ class ListStringItem extends ListItemBase {
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;
}

View File

@ -306,14 +306,15 @@ class OptionsFieldUITest extends FieldTestBase {
$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 = [

View File

@ -375,6 +375,37 @@ JS;
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.
*