diff --git a/core/modules/forum/config/install/field.field.taxonomy_term.forum_container.yml b/core/modules/forum/config/install/field.field.taxonomy_term.forum_container.yml index a666915..d9f5631 100644 --- a/core/modules/forum/config/install/field.field.taxonomy_term.forum_container.yml +++ b/core/modules/forum/config/install/field.field.taxonomy_term.forum_container.yml @@ -4,9 +4,7 @@ langcode: en name: forum_container type: list_boolean settings: - allowed_values: - - '' - - '' + allowed_values: { } allowed_values_function: '' module: options entity_type: taxonomy_term diff --git a/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php b/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php index 5c0b0d2..bbcf8eb 100644 --- a/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php +++ b/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php @@ -9,6 +9,7 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\options\Plugin\Field\FieldType\ListItemBase; /** * Plugin implementation of the 'list_default' formatter. @@ -33,7 +34,7 @@ public function viewElements(FieldItemListInterface $items) { $elements = array(); $entity = $items->getEntity(); - $allowed_values = options_allowed_values($this->fieldDefinition, $entity); + $allowed_values = ListItemBase::simplifyAllowedValues(options_allowed_values($this->fieldDefinition, $entity)); foreach ($items as $delta => $item) { if (isset($allowed_values[$item->value])) { diff --git a/core/modules/options/src/Plugin/Field/FieldType/ListBooleanItem.php b/core/modules/options/src/Plugin/Field/FieldType/ListBooleanItem.php index 5d16585..4a9e98c 100644 --- a/core/modules/options/src/Plugin/Field/FieldType/ListBooleanItem.php +++ b/core/modules/options/src/Plugin/Field/FieldType/ListBooleanItem.php @@ -62,7 +62,7 @@ public function getSettableValues(AccountInterface $account = NULL) { * {@inheritdoc} */ public function getSettableOptions(AccountInterface $account = NULL) { - return options_allowed_values($this->getFieldDefinition(), $this->getEntity()); + return ListItemBase::simplifyAllowedValues(options_allowed_values($this->getFieldDefinition(), $this->getEntity())); } /** @@ -107,8 +107,8 @@ public function settingsForm(array &$form, array &$form_state, $has_data) { $allowed_values_function = $this->getSetting('allowed_values_function'); $values = $allowed_values; - $off_value = array_shift($values); - $on_value = array_shift($values); + $off_item = array_shift($values); + $on_item = array_shift($values); $element['allowed_values'] = array( '#type' => 'value', @@ -119,7 +119,7 @@ public function settingsForm(array &$form, array &$form_state, $has_data) { $element['allowed_values']['on'] = array( '#type' => 'textfield', '#title' => t('On value'), - '#default_value' => $on_value, + '#default_value' => $on_item['label'], '#required' => FALSE, '#description' => t('If left empty, "1" will be used.'), // Change #parents to make sure the element is not saved into field @@ -129,7 +129,7 @@ public function settingsForm(array &$form, array &$form_state, $has_data) { $element['allowed_values']['off'] = array( '#type' => 'textfield', '#title' => t('Off value'), - '#default_value' => $off_value, + '#default_value' => $off_item['label'], '#required' => FALSE, '#description' => t('If left empty, "0" will be used.'), // Change #parents to make sure the element is not saved into field @@ -160,7 +160,7 @@ public function settingsForm(array &$form, array &$form_state, $has_data) { public static function optionsBooleanAllowedValues($element, $input, $form_state) { $on = NestedArray::getValue($form_state['input'], $element['#on_parents']); $off = NestedArray::getValue($form_state['input'], $element['#off_parents']); - return array($off, $on); + return array(array('value' => false, 'label' => $off), array('value' => true, 'label' => $on)); } } diff --git a/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php b/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php index 30cd382..bc91f64 100644 --- a/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php +++ b/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php @@ -69,16 +69,13 @@ protected function allowedValuesDescription() { protected static function extractAllowedValues($string, $has_data) { $values = parent::extractAllowedValues($string, $has_data); if ($values) { - $keys = array_keys($values); - $labels = array_values($values); - $keys = array_map(function ($key) { + foreach ($values as $item) { // Float keys are represented as strings and need to be disambiguated // ('.5' is '0.5'). - return is_numeric($key) ? (string) (float) $key : $key; - }, $keys); - - return array_combine($keys, $labels); + $item['value'] = is_numeric($item['value']) ? (string) (float) $item['value'] : $item['value']; + } } + return $values; } /** diff --git a/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php b/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php index ea1b6e5..750b448 100644 --- a/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php +++ b/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php @@ -59,7 +59,7 @@ public function getSettableValues(AccountInterface $account = NULL) { */ public function getSettableOptions(AccountInterface $account = NULL) { $allowed_options = options_allowed_values($this->getFieldDefinition(), $this->getEntity()); - return $allowed_options; + return static::simplifyAllowedValues($allowed_options); } /** diff --git a/core/modules/options/src/Tests/OptionsFieldTest.php b/core/modules/options/src/Tests/OptionsFieldTest.php index ed775b4..aad59bf 100644 --- a/core/modules/options/src/Tests/OptionsFieldTest.php +++ b/core/modules/options/src/Tests/OptionsFieldTest.php @@ -45,7 +45,7 @@ function testUpdateAllowedValues() { $entity = entity_create('entity_test'); $entity->{$this->fieldName}->value = 1; $entity->save(); - $this->field->settings['allowed_values'] = array(2 => 'Two'); + $this->field->settings['allowed_values'] = array(array('value' => 2, 'label' => 'Two')); try { $this->field->save(); $this->fail(t('Cannot update a list field to not include keys with existing data.')); @@ -58,7 +58,7 @@ function testUpdateAllowedValues() { $entity->save(); // Removed options do not appear. - $this->field->settings['allowed_values'] = array(2 => 'Two'); + $this->field->settings['allowed_values'] = array(array('value' => 2, 'label' => 'Two')); $this->field->save(); $entity = entity_create('entity_test'); $form = \Drupal::service('entity.form_builder')->getForm($entity); @@ -67,7 +67,7 @@ function testUpdateAllowedValues() { $this->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist'); // Completely new options appear. - $this->field->settings['allowed_values'] = array(10 => 'Update', 20 => 'Twenty'); + $this->field->settings['allowed_values'] = array(array('value' => 10, 'label' => 'Update'), array('value' => 20, 'label' => 'Twenty')); $this->field->save(); // The entity holds an outdated field object with the old allowed values // setting, so we need to reintialize the entity object. diff --git a/core/modules/options/src/Tests/OptionsFieldUITest.php b/core/modules/options/src/Tests/OptionsFieldUITest.php index 84f5256..ee85076 100644 --- a/core/modules/options/src/Tests/OptionsFieldUITest.php +++ b/core/modules/options/src/Tests/OptionsFieldUITest.php @@ -61,22 +61,22 @@ function testOptionsAllowedValuesInteger() { // Flat list of textual values. $string = "Zero\nOne"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '1', 'label' => 'One'), + array('value' => 0, 'label' => 'Zero'), + array('value' => 1, 'label' => 'One'), ); $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.'); // Explicit integer keys. $string = "0|Zero\n2|Two"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '2', 'label' => 'Two'), + array('value' => 0, 'label' => 'Zero'), + array('value' => 2, 'label' => 'Two'), ); $this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.'); // Check that values can be added and removed. $string = "0|Zero\n1|One"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '1', 'label' => 'One'), + array('value' => 0, 'label' => 'Zero'), + array('value' => 1, 'label' => 'One'), ); $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.'); // Non-integer keys. @@ -98,15 +98,15 @@ function testOptionsAllowedValuesInteger() { // Check that values can be added but values in use cannot be removed. $string = "0|Zero\n1|One\n2|Two"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '1', 'label' => 'One'), - array('value' => '2', 'label' => 'Two'), + array('value' => 0, 'label' => 'Zero'), + array('value' => 1, 'label' => 'One'), + array('value' => 2, 'label' => 'Two'), ); $this->assertAllowedValuesInput($string, $array, 'Values can be added.'); $string = "0|Zero\n1|One"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '1', 'label' => 'One'), + array('value' => 0, 'label' => 'Zero'), + array('value' => 1, 'label' => 'One'), ); $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.'); $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.'); @@ -115,7 +115,7 @@ function testOptionsAllowedValuesInteger() { $node->delete(); $string = "0|Zero"; $array = array( - array('value' => '0', 'label' => 'Zero'), + array('value' => 0, 'label' => 'Zero'), ); $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.'); } @@ -130,23 +130,23 @@ function testOptionsAllowedValuesFloat() { // Flat list of textual values. $string = "Zero\nOne"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '1', 'label' => 'One'), + array('value' => (float) 0, 'label' => 'Zero'), + array('value' => (float) 1, 'label' => 'One'), ); $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.'); // Explicit numeric keys. $string = "0|Zero\n.5|Point five"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '0.5', 'label' => 'Point five'), + array('value' => (float) 0, 'label' => 'Zero'), + array('value' => (float) 0.5, 'label' => 'Point five'), ); $this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.'); // Check that values can be added and removed. $string = "0|Zero\n.5|Point five\n1.0|One"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '0.5', 'label' => 'Point five'), - array('value' => '1', 'label' => 'One'), + array('value' => (float) 0, 'label' => 'Zero'), + array('value' => (float) 0.5, 'label' => 'Point five'), + array('value' => (float) 1, 'label' => 'One'), ); $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.'); // Non-numeric keys. @@ -167,15 +167,15 @@ function testOptionsAllowedValuesFloat() { // Check that values can be added but values in use cannot be removed. $string = "0|Zero\n.5|Point five\n2|Two"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '0.5', 'label' => 'Point five'), - array('value' => '2', 'label' => 'Two'), + array('value' => (float) 0, 'label' => 'Zero'), + array('value' => (float) 0.5, 'label' => 'Point five'), + array('value' => (float) 2, 'label' => 'Two'), ); $this->assertAllowedValuesInput($string, $array, 'Values can be added.'); $string = "0|Zero\n.5|Point five"; $array = array( - array('value' => '0', 'label' => 'Zero'), - array('value' => '0.5', 'label' => 'Point five'), + array('value' => (float) 0, 'label' => 'Zero'), + array('value' => (float) 0.5, 'label' => 'Point five'), ); $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.'); $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.'); @@ -184,7 +184,7 @@ function testOptionsAllowedValuesFloat() { $node->delete(); $string = "0|Zero"; $array = array( - array('value' => '0', 'label' => 'Zero'), + array('value' => (float) 0, 'label' => 'Zero'), ); $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.'); } @@ -278,7 +278,10 @@ function testOptionsAllowedValuesBoolean() { // Check that the separate 'On' and 'Off' form fields work. $on = $this->randomName(); $off = $this->randomName(); - $allowed_values = array(1 => $on, 0 => $off); + $allowed_values = array( + array('value' => false, 'label' => $off), + array('value' => true, 'label' => $on), + ); $edit = array( 'on' => $on, 'off' => $off,