diff --git a/core/lib/Drupal/Core/Field/FieldItemBase.php b/core/lib/Drupal/Core/Field/FieldItemBase.php index 72fa503..e4771b4 100644 --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -262,6 +262,8 @@ public static function simplifyAllowedValues(array $structured_values) { $values = array(); foreach ($structured_values as $item) { if (is_array($item['label'])) { + // Nested elements are embedded in the label. + // See structureAllowedValues(). $item['label'] = static::simplifyAllowedValues($item['label']); } $values[$item['value']] = $item['label']; diff --git a/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php b/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php index 76da11e..ada4733 100644 --- a/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php +++ b/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php @@ -93,6 +93,8 @@ protected static function validateAllowedValue($option) { public static function simplifyAllowedValues(array $structured_values) { $values = array(); foreach ($structured_values as $item) { + // Nested elements are embedded in the label. + // See FieldItemBase::structureAllowedValues(). if (is_array($item['label'])) { $item['label'] = static::simplifyAllowedValues($item['label']); } diff --git a/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php b/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php index 186a61d..39d9637 100644 --- a/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php +++ b/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php @@ -129,11 +129,17 @@ public static function validateAllowedValues($element, &$form_state) { } else { // Check that keys are valid for the field type. + $keys_in_use = array(); foreach ($values as $item) { if ($error = static::validateAllowedValue($item['value'])) { \Drupal::formBuilder()->setError($element, $form_state, $error); break; } + if (in_array($item['value'], $keys_in_use)) { + \Drupal::formBuilder()->setError($element, $form_state, t('One key can only be used once.')); + break; + } + $keys_in_use[] = $item['value']; } // Prevent removing values currently in use. diff --git a/core/modules/options/src/Tests/OptionsFieldUITest.php b/core/modules/options/src/Tests/OptionsFieldUITest.php index 0712329..bf01335 100644 --- a/core/modules/options/src/Tests/OptionsFieldUITest.php +++ b/core/modules/options/src/Tests/OptionsFieldUITest.php @@ -118,6 +118,10 @@ function testOptionsAllowedValuesInteger() { array('value' => 0, 'label' => 'Zero'), ); $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.'); + + // Check that the same key can only be used once. + $string = "0|Zero\n0|One"; + $this->assertAllowedValuesInput($string, 'One key can only be used once.', 'Same value cannot be used multiple times.'); } /** @@ -187,6 +191,14 @@ function testOptionsAllowedValuesFloat() { array('value' => (float) 0, 'label' => 'Zero'), ); $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.'); + + // Check that the same key can only be used once. + $string = "0.5|Point five\n0.5|Half"; + $this->assertAllowedValuesInput($string, 'One key can only be used once.', 'Same value cannot be used multiple times.'); + + // Check that different forms of the same float value cannot be used. + $string = "0|Zero\n.5|Point five\n0.5|Half"; + $this->assertAllowedValuesInput($string, 'One key can only be used once.', 'Different forms of the same value cannot be used.'); } /** @@ -266,6 +278,19 @@ function testOptionsAllowedValuesText() { array('value' => 'Zero', 'label' => 'Zero'), ); $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.'); + + // Check that string values with dots can be used. + $node->delete(); + $string = "Zero\nexample.com|Example"; + $array = array( + array('value' => 'Zero', 'label' => 'Zero'), + array('value' => 'example.com', 'label' => 'Example'), + ); + $this->assertAllowedValuesInput($string, $array, 'String value with dot is supported.'); + + // Check that the same key can only be used once. + $string = "Zero\nZero"; + $this->assertAllowedValuesInput($string, 'One key can only be used once.', 'Same value cannot be used multiple times.'); } /** @@ -313,6 +338,10 @@ function testOptionsTrimmedValuesText() { array('value' => 'one', 'label' => 'One'), ); $this->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted and trimmed.'); + + // Check that the same key can only be used once. + $string = " Zero \n Zero "; + $this->assertAllowedValuesInput($string, 'One key can only be used once.', 'Same value cannot be used multiple times.'); } /**