diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsButtonsWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsButtonsWidget.php index d993c676b61ae45b24205d6688951badb43250a8..4f51a2e207a540ca497db5a29d2e441c48d1d64b 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsButtonsWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsButtonsWidget.php @@ -66,9 +66,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen /** * {@inheritdoc} */ - protected function getEmptyOption() { + protected function getEmptyLabel() { if (!$this->required && !$this->multiple) { - return static::OPTIONS_EMPTY_NONE; + return t('N/A'); } } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php index 33956c424207838cd53e1c35bec1c3f527e1bf2e..d64d5f9deb3538e9b370fc0d9ab02c17d7b7549c 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php @@ -62,11 +62,11 @@ protected function supportsGroups() { /** * {@inheritdoc} */ - protected function getEmptyOption() { + protected function getEmptyLabel() { if ($this->multiple) { // Multiple select: add a 'none' option for non-required fields. if (!$this->required) { - return static::OPTIONS_EMPTY_NONE; + return t('- None -'); } } else { @@ -74,10 +74,10 @@ protected function getEmptyOption() { // and a 'select a value' option for required fields that do not come // with a value selected. if (!$this->required) { - return static::OPTIONS_EMPTY_NONE; + return t('- None -'); } if (!$this->has_value) { - return static::OPTIONS_EMPTY_SELECT; + return t('- Select a value -'); } } } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php index b155c3b24621aee87cfc4f5cc0e7af98cfb3bcfc..8b53b38f4ccc0018918a8985363fe938cd866132 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php @@ -26,16 +26,6 @@ */ abstract class OptionsWidgetBase extends WidgetBase { - /** - * Identifies a 'None' option. - */ - const OPTIONS_EMPTY_NONE = 'option_none'; - - /** - * Identifies a 'Select a value' option. - */ - const OPTIONS_EMPTY_SELECT = 'option_select'; - /** * Abstract over the actual field columns, to allow different field types to * reuse those widgets. @@ -130,18 +120,8 @@ protected function getOptions(FieldableEntityInterface $entity) { ->getSettableOptions(\Drupal::currentUser()); // Add an empty option if the widget needs one. - if ($empty_option = $this->getEmptyOption()) { - switch ($this->getPluginId()) { - case 'options_buttons': - $label = t('N/A'); - break; - - case 'options_select': - $label = ($empty_option == static::OPTIONS_EMPTY_NONE ? t('- None -') : t('- Select a value -')); - break; - } - - $options = array('_none' => $label) + $options; + if ($empty_label = $this->getEmptyLabel()) { + $options = ['_none' => $empty_label] + $options; } $module_handler = \Drupal::moduleHandler(); @@ -214,11 +194,11 @@ protected function sanitizeLabel(&$label) { } /** - * Returns the empty option to add to the list of options, if any. + * Returns the empty option label to add to the list of options, if any. * - * @return string|null - * Either static::OPTIONS_EMPTY_NONE, static::OPTIONS_EMPTY_SELECT, or NULL. + * @return string|NULL + * Either a label of the empty option, or NULL. */ - protected function getEmptyOption() { } + protected function getEmptyLabel() { } } diff --git a/core/modules/options/src/Tests/OptionsWidgetsTest.php b/core/modules/options/src/Tests/OptionsWidgetsTest.php index 0de54a5d5c7f5ee50032a04de2f47ea723dc63c9..b8cf90e5d76c8d0242abe0ab9f4ed6a3c760465b 100644 --- a/core/modules/options/src/Tests/OptionsWidgetsTest.php +++ b/core/modules/options/src/Tests/OptionsWidgetsTest.php @@ -450,4 +450,47 @@ function testSelectListMultiple() { $this->assertFieldValues($entity_init, 'card_2', array()); } + /** + * Tests the 'options_select' and 'options_button' widget for empty value. + */ + function testEmptyValue() { + // Create an instance of the 'single value' field. + $field = entity_create('field_config', [ + 'field_storage' => $this->card1, + 'bundle' => 'entity_test', + ]); + $field->save(); + + // Change it to the check boxes/radio buttons widget. + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($this->card1->getName(), [ + 'type' => 'options_buttons', + ]) + ->save(); + + // Create an entity. + $entity = entity_create('entity_test', [ + 'user_id' => 1, + 'name' => $this->randomMachineName(), + ]); + $entity->save(); + + // Display form: check that _none options are present and has label. + $this->drupalGet('entity_test/manage/' . $entity->id()); + $this->assertTrue($this->xpath('//div[@id=:id]//div[@class=:class]//input[@value=:value]', array(':id' => 'edit-card-1', ':class' => 'form-item form-type-radio form-item-card-1', ':value' => '_none')), 'A test radio button has a "None" choice.'); + $this->assertTrue($this->xpath('//div[@id=:id]//div[@class=:class]//label[@for=:for and text()=:label]', array(':id' => 'edit-card-1', ':class' => 'form-item form-type-radio form-item-card-1', ':for' => 'edit-card-1-none', ':label' => 'N/A')), 'A test radio button has a "N/A" choice.'); + + // Change it to the select widget. + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($this->card1->getName(), array( + 'type' => 'options_select', + )) + ->save(); + + // Display form: check that _none options are present and has label. + $this->drupalGet('entity_test/manage/' . $entity->id()); + // A required field without any value has a "none" option. + $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1', ':label' => t('- None -'))), 'A test select has a "None" choice.'); + } + }