Commit 6a002ab5 authored by alexpott's avatar alexpott
Browse files

Issue #2426781 by jibran: Custom OptionWidget have no empty option label

parent e55bba7f
......@@ -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');
}
}
......
......@@ -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 -');
}
}
}
......
......@@ -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() { }
}
......@@ -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.');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment