Issue #2226063 by Berdir, plopesc: Merge ListBooleanItem from options module into BooleanItem.

8.0.x
Nathaniel Catchpole 2014-07-17 11:07:54 +01:00
parent 518e3c9f7f
commit a780b52e2c
26 changed files with 447 additions and 434 deletions

View File

@ -0,0 +1,39 @@
<?php
/**
* @file
* Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\BooleanFormatter.
*/
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
/**
* Plugin implementation of the 'boolean' formatter.
*
* @FieldFormatter(
* id = "boolean",
* label = @Translation("Boolean"),
* field_types = {
* "boolean",
* }
* )
*/
class BooleanFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
foreach ($items as $delta => $item) {
$elements[$delta] = array('#markup' => $item->value ? $this->getFieldSetting('on_label') : $this->getFieldSetting('off_label'));
}
return $elements;
}
}

View File

@ -9,6 +9,8 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\AllowedValuesInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
@ -18,10 +20,21 @@ use Drupal\Core\TypedData\DataDefinition;
* id = "boolean",
* label = @Translation("Boolean"),
* description = @Translation("An entity field containing a boolean value."),
* no_ui = TRUE
* default_widget = "boolean_checkbox",
* default_formatter = "boolean",
* )
*/
class BooleanItem extends FieldItemBase {
class BooleanItem extends FieldItemBase implements AllowedValuesInterface {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'on_label' => t('On'),
'off_label' => t('Off'),
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
@ -48,4 +61,56 @@ class BooleanItem extends FieldItemBase {
);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array &$form, array &$form_state, $has_data) {
$element['on_label'] = array(
'#type' => 'textfield',
'#title' => $this->t('"On" label'),
'#default_value' => $this->getSetting('on_label'),
'#required' => TRUE,
);
$element['off_label'] = array(
'#type' => 'textfield',
'#title' => $this->t('"Off" label'),
'#default_value' => $this->getSetting('off_label'),
'#required' => TRUE,
);
return $element;
}
/**
* {@inheritdoc}
*/
public function getPossibleValues(AccountInterface $account = NULL) {
return array(0, 1);
}
/**
* {@inheritdoc}
*/
public function getPossibleOptions(AccountInterface $account = NULL) {
return array(
0 => $this->getSetting('off_label'),
1 => $this->getSetting('on_label'),
);
}
/**
* {@inheritdoc}
*/
public function getSettableValues(AccountInterface $account = NULL) {
return array(0, 1);
}
/**
* {@inheritdoc}
*/
public function getSettableOptions(AccountInterface $account = NULL) {
return $this->getPossibleOptions($account);
}
}

View File

@ -2,27 +2,27 @@
/**
* @file
* Contains \Drupal\options\Plugin\Field\FieldWidget\OnOffWidget.
* Contains \Drupal\Core\Field\Plugin\Field\FieldWidget\CheckboxWidget.
*/
namespace Drupal\options\Plugin\Field\FieldWidget;
namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\options\Plugin\Field\FieldWidget\OptionsWidgetBase;
use Drupal\Core\Field\WidgetBase;
/**
* Plugin implementation of the 'options_onoff' widget.
* Plugin implementation of the 'boolean_checkbox' widget.
*
* @FieldWidget(
* id = "options_onoff",
* id = "boolean_checkbox",
* label = @Translation("Single on/off checkbox"),
* field_types = {
* "list_boolean"
* "boolean"
* },
* multiple_values = TRUE
* )
*/
class OnOffWidget extends OptionsWidgetBase {
class BooleanCheckboxWidget extends WidgetBase {
/**
* {@inheritdoc}
@ -39,7 +39,7 @@ class OnOffWidget extends OptionsWidgetBase {
public function settingsForm(array $form, array &$form_state) {
$element['display_label'] = array(
'#type' => 'checkbox',
'#title' => t('Use field label instead of the "On value" as label'),
'#title' => t('Use field label instead of the "On label" as label'),
'#default_value' => $this->getSetting('display_label'),
'#weight' => -1,
);
@ -62,22 +62,17 @@ class OnOffWidget extends OptionsWidgetBase {
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$options = $this->getOptions($items[$delta]);
$selected = $this->getSelectedOptions($items);
$element += array(
$element['value'] = $element + array(
'#type' => 'checkbox',
'#default_value' => !empty($selected[0]),
'#default_value' => !empty($items[0]->value),
);
// Override the title from the incoming $element.
if ($this->getSetting('display_label')) {
$element['#title'] = $this->fieldDefinition->getLabel();
$element['value']['#title'] = $this->fieldDefinition->getLabel();
}
else {
$element['#title'] = isset($options[1]) ? $options[1] : '';
$element['value']['#title'] = $this->fieldDefinition->getSetting('on_label');
}
return $element;

View File

@ -8,6 +8,7 @@
namespace Drupal\Core\TypedData;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* The abstract base class for typed data.
@ -19,6 +20,8 @@ use Drupal\Component\Plugin\PluginInspectionInterface;
*/
abstract class TypedData implements TypedDataInterface, PluginInspectionInterface {
use StringTranslationTrait;
/**
* The data definition.
*

View File

@ -97,6 +97,34 @@ entity_form_display.field.hidden:
sequence:
- type: string
# Schema for the configuration files of the Boolean field type.
field.boolean.settings:
type: mapping
label: 'Boolean settings'
mapping:
on_label:
type: string
label: 'On label'
off_label:
type: string
label: 'Off label'
field.boolean.instance_settings:
label: 'Boolean settings'
type: mapping
mapping: { }
field.boolean.value:
type: sequence
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
value:
type: integer
label: 'Value'
# Schema for the configuration files of the Email field type.
field.email.settings:
@ -291,3 +319,15 @@ entity_form_display.field.number:
placeholder:
type: label
label: 'Placeholder'
entity_form_display.field.checkbox:
type: entity_field_form_display_base
label: 'Single on/off checkbox format settings'
mapping:
settings:
type: mapping
label: 'Settings'
mapping:
display_label:
type: boolean
label: 'Use field label instead of the "On value" as label'

View File

@ -0,0 +1,177 @@
<?php
/**
* @file
* Contains \Drupal\field\Tests\Boolean\BooleanFieldTest.
*/
namespace Drupal\field\Tests\Boolean;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldInstanceConfig;
use Drupal\simpletest\WebTestBase;
/**
* Tests boolean field functionality.
*
* @group field
*/
class BooleanFieldTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test', 'field_ui', 'options');
/**
* A field to use in this test class.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* The instance used in this test class.
*
* @var \Drupal\field\Entity\FieldInstanceConfig
*/
protected $instance;
/**
* {@inheritdoc}
*/
function setUp() {
parent::setUp();
$this->web_user = $this->drupalCreateUser(array(
'view test entity',
'administer entity_test content',
'administer entity_test form display',
'administer entity_test fields',
));
$this->drupalLogin($this->web_user);
}
/**
* Tests boolean field.
*/
function testBooleanField() {
$on = $this->randomName();
$off = $this->randomName();
$label = $this->randomName();
// Create a field with settings to validate.
$field_name = drupal_strtolower($this->randomName());
$this->field = FieldConfig::create(array(
'name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'boolean',
'settings' => array(
'on_label' => $on,
'off_label' => $off,
),
));
$this->field->save();
$this->instance = FieldInstanceConfig::create(array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => $label,
'required' => TRUE,
));
$this->instance->save();
// Create a form display for the default form mode.
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($field_name, array(
'type' => 'boolean_checkbox',
))
->save();
// Create a display for the full view mode.
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($field_name, array(
'type' => 'boolean',
))
->save();
// Display creation form.
$this->drupalGet('entity_test/add');
$this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
$this->assertRaw($on);
// Submit and ensure it is accepted.
$edit = array(
'user_id' => 1,
'name' => $this->randomName(),
"{$field_name}[value]" => 1,
);
$this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
$id = $match[1];
$this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
// Verify that boolean value is displayed.
$entity = entity_load('entity_test', $id);
$display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
$content = $display->build($entity);
$this->drupalSetContent(drupal_render($content));
$this->assertRaw('<div class="field-item">' . $on . '</div>');
// Test the display_label option.
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($field_name, array(
'type' => 'boolean_checkbox',
'settings' => array(
'display_label' => TRUE,
)
))
->save();
$this->drupalGet('entity_test/add');
$this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
$this->assertNoRaw($on);
$this->assertText($this->instance->label());
// Go to the form display page and check if the default settings works as
// expected.
$fieldEditUrl = 'entity_test/structure/entity_test/form-display';
$this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form.
$this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
$this->assertText(
'Use field label instead of the "On label" as label',
t('Display setting checkbox available.')
);
// Enable setting.
$edit = array('fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1);
$this->drupalPostAjaxForm(NULL, $edit, $field_name . "_plugin_settings_update");
$this->drupalPostForm(NULL, NULL, 'Save');
// Go again to the form display page and check if the setting
// is stored and has the expected effect.
$this->drupalGet($fieldEditUrl);
$this->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.');
$this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
$this->assertText(
'Use field label instead of the "On label" as label',
t('Display setting checkbox is available')
);
$this->assertFieldByXPath(
'*//input[@id="edit-fields-' . $field_name . '-settings-edit-form-settings-display-label" and @value="1"]',
TRUE,
t('Display label changes label of the checkbox')
);
// Test the boolean field settings.
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name . '/field');
$this->assertFieldById('edit-field-settings-on-label', $on);
$this->assertFieldById('edit-field-settings-off-label', $off);
}
}

View File

@ -0,0 +1,77 @@
<?php
/**
* @file
* Contains \Drupal\field\Tests\Boolean\BooleanItemTest.
*/
namespace Drupal\field\Tests\Boolean;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\field\Tests\FieldUnitTestBase;
/**
* Tests the new entity API for the boolean field type.
*
* @group field
*/
class BooleanItemTest extends FieldUnitTestBase {
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Create an boolean field and instance for validation.
entity_create('field_config', array(
'name' => 'field_boolean',
'entity_type' => 'entity_test',
'type' => 'boolean',
))->save();
entity_create('field_instance_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_boolean',
'bundle' => 'entity_test',
))->save();
// Create a form display for the default form mode.
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent('field_boolean', array(
'type' => 'boolean',
))
->save();
}
/**
* Tests using entity fields of the boolean field type.
*/
public function testBooleanItem() {
// Verify entity creation.
$entity = entity_create('entity_test');
$value = '1';
$entity->field_boolean = $value;
$entity->name->value = $this->randomName();
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
$this->assertTrue($entity->field_boolean instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->field_boolean[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_boolean->value, $value);
$this->assertEqual($entity->field_boolean[0]->value, $value);
// Verify changing the boolean value.
$new_value = 0;
$entity->field_boolean->value = $new_value;
$this->assertEqual($entity->field_boolean->value, $new_value);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->field_boolean->value, $new_value);
}
}

View File

@ -2,12 +2,10 @@ id: taxonomy_term.forum_container
status: true
langcode: en
name: forum_container
type: list_boolean
type: boolean
settings:
allowed_values:
- ''
- ''
allowed_values_function: ''
on_label: Yes
off_label: No
module: options
entity_type: taxonomy_term
locked: true

View File

@ -9,10 +9,10 @@ description: ''
required: true
default_value:
-
value: false
value: 0
default_value_function: ''
settings: { }
field_type: list_boolean
field_type: boolean
dependencies:
entity:
- field.field.taxonomy_term.forum_container

View File

@ -44,7 +44,7 @@ process:
filefield_widget: file_generic
imagefield_widget: image_image
phone_textfield: telephone_default
optionwidgets_onoff: options_onoff
optionwidgets_onoff: checkbox
optionwidgets_buttons: options_buttons
optionwidgets_select: options_select
'options/settings':

View File

@ -20,7 +20,7 @@ process:
plugin: static_map
source: type
map:
checkbox: options_onoff
checkbox: boolean_checkbox
date: datetime_default
list: text_textfield
selection: options_select

View File

@ -13,7 +13,7 @@ process:
plugin: static_map
source: type
map:
checkbox: list_boolean
checkbox: boolean
date: datetime
list: text
selection: list_text

View File

@ -53,7 +53,7 @@ class MigrateProfileValuesTest extends MigrateDrupalTestBase {
entity_create('field_config', array(
'entity_type' => 'user',
'name' => 'profile_sell_address',
'type' => 'list_boolean',
'type' => 'boolean',
))->save();
entity_create('field_config', array(
'entity_type' => 'user',
@ -85,7 +85,7 @@ class MigrateProfileValuesTest extends MigrateDrupalTestBase {
entity_create('field_config', array(
'entity_type' => 'user',
'name' => 'profile_love_migrations',
'type' => 'list_boolean',
'type' => 'boolean',
))->save();
// Create the field instances.

View File

@ -45,7 +45,7 @@ class MigrateUserProfileEntityDisplayTest extends MigrateDrupalTestBase {
entity_create('field_config', array(
'entity_type' => 'user',
'name' => 'profile_sell_address',
'type' => 'list_boolean',
'type' => 'boolean',
))->save();
entity_create('field_config', array(
'entity_type' => 'user',
@ -71,7 +71,7 @@ class MigrateUserProfileEntityDisplayTest extends MigrateDrupalTestBase {
entity_create('field_config', array(
'entity_type' => 'user',
'name' => 'profile_love_migrations',
'type' => 'list_boolean',
'type' => 'boolean',
))->save();
$field_data = Drupal6UserProfileFields::getData('profile_fields');
foreach ($field_data as $field) {

View File

@ -40,7 +40,7 @@ class MigrateUserProfileEntityFormDisplayTest extends MigrateDrupalTestBase {
entity_create('field_config', array(
'entity_type' => 'user',
'name' => 'profile_sell_address',
'type' => 'list_boolean',
'type' => 'boolean',
))->save();
entity_create('field_config', array(
'entity_type' => 'user',
@ -66,7 +66,7 @@ class MigrateUserProfileEntityFormDisplayTest extends MigrateDrupalTestBase {
entity_create('field_config', array(
'entity_type' => 'user',
'name' => 'profile_love_migrations',
'type' => 'list_boolean',
'type' => 'boolean',
))->save();
$field_data = Drupal6UserProfileFields::getData('profile_fields');
foreach ($field_data as $field) {
@ -116,7 +116,7 @@ class MigrateUserProfileEntityFormDisplayTest extends MigrateDrupalTestBase {
// Test that a checkbox field has the proper display label setting.
$component = $display->getComponent('profile_love_migrations');
$this->assertEqual($component['type'], 'options_onoff');
$this->assertEqual($component['type'], 'boolean_checkbox');
$this->assertEqual($component['settings']['display_label'], true);
}

View File

@ -98,12 +98,12 @@ class MigrateUserProfileFieldInstanceTest extends MigrateDrupalTestBase {
$fields = array(
'profile_color' => 'text',
'profile_biography' => 'text_long',
'profile_sell_address' => 'list_boolean',
'profile_sell_address' => 'boolean',
'profile_sold_to' => 'list_text',
'profile_bands' => 'text',
'profile_blog' => 'link',
'profile_birthdate' => 'datetime',
'profile_love_migrations' => 'list_boolean',
'profile_love_migrations' => 'boolean',
);
foreach ($fields as $name => $type) {
entity_create('field_config', array(

View File

@ -49,7 +49,7 @@ class MigrateUserProfileFieldTest extends MigrateDrupalTestBase {
// Migrated checkbox field.
$field = entity_load('field_config', 'user.profile_sell_address');
$this->assertEqual($field->type, 'list_boolean', 'Field type is list_boolean.');
$this->assertEqual($field->type, 'boolean', 'Field type is boolean.');
// Migrated selection field.
$field = entity_load('field_config', 'user.profile_sold_to');

View File

@ -53,11 +53,12 @@ class NodeAccessLanguageAwareCombinationTest extends NodeTestBase {
$field_private = entity_create('field_config', array(
'name' => 'field_private',
'entity_type' => 'node',
'type' => 'list_boolean',
'type' => 'boolean',
'cardinality' => 1,
'translatable' => TRUE,
'settings' => array(
'allowed_values' => array(0 => 'Not private', 1 => 'Private'),
'on_label' => 'Private',
'off_label' => 'Not private',
),
));
$field_private->save();

View File

@ -46,12 +46,13 @@ class NodeAccessLanguageAwareTest extends NodeTestBase {
$field_private = entity_create('field_config', array(
'name' => 'field_private',
'entity_type' => 'node',
'type' => 'list_boolean',
'type' => 'boolean',
'cardinality' => 1,
'translatable' => TRUE,
'settings' => array(
'allowed_values' => array(0 => 'Not private', 1 => 'Private'),
),
'settings' => array(
'on_label' => 'Private',
'off_label' => 'Not private',
),
));
$field_private->save();

View File

@ -87,36 +87,6 @@ field.list_text.value:
type: string
label: 'Value'
field.list_boolean.settings:
type: mapping
label: 'List (boolean) settings'
mapping:
allowed_values:
type: sequence
label: 'Allowed values list'
sequence:
- type: string
label: 'Value'
allowed_values_function:
type: string
label: 'Allowed values function'
field.list_boolean.instance_settings:
label: 'List (boolean)'
type: mapping
mapping: { }
field.list_boolean.value:
type: sequence
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
value:
type: boolean
label: 'Value'
entity_view_display.field.list_default:
type: entity_field_view_display_base
label: 'Options list default display settings'
@ -147,18 +117,6 @@ entity_form_display.field.options_buttons:
sequence:
- type: string
entity_form_display.field.options_onoff:
type: entity_field_form_display_base
label: 'Single on/off checkbox format settings'
mapping:
settings:
type: mapping
label: 'Settings'
mapping:
display_label:
type: boolean
label: 'Use field label instead of the "On value" as label'
entity_form_display.field.options_select:
type: entity_field_form_display_base
label: 'Select list format settings'

View File

@ -20,7 +20,6 @@ use Drupal\Core\Field\FieldItemListInterface;
* "list_integer",
* "list_float",
* "list_text",
* "list_boolean"
* }
* )
*/

View File

@ -20,7 +20,6 @@ use Drupal\Core\Field\FieldItemListInterface;
* "list_integer",
* "list_float",
* "list_text",
* "list_boolean"
* }
* )
*/

View File

@ -1,166 +0,0 @@
<?php
/**
* @file
* Contains \Drupal\options\Type\ListBooleanItem.
*/
namespace Drupal\options\Plugin\Field\FieldType;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\AllowedValuesInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Plugin implementation of the 'list_boolean' field type.
*
* @FieldType(
* id = "list_boolean",
* label = @Translation("Boolean"),
* description = @Translation("This field stores simple on/off or yes/no options."),
* default_widget = "options_buttons",
* default_formatter = "list_default",
* )
*/
class ListBooleanItem extends FieldItemBase implements AllowedValuesInterface {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'allowed_values' => array(),
'allowed_values_function' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function getPossibleValues(AccountInterface $account = NULL) {
return array_keys($this->getSettableOptions($account));
}
/**
* {@inheritdoc}
*/
public function getPossibleOptions(AccountInterface $account = NULL) {
return $this->getSettableOptions($account);
}
/**
* {@inheritdoc}
*/
public function getSettableValues(AccountInterface $account = NULL) {
return array_keys($this->getSettableOptions($account));
}
/**
* {@inheritdoc}
*/
public function getSettableOptions(AccountInterface $account = NULL) {
return options_allowed_values($this->getFieldDefinition(), $this->getEntity());
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('boolean')
->setLabel(t('Boolean value'));
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'value' => array(
'type' => 'int',
'not null' => FALSE,
),
),
'indexes' => array(
'value' => array('value'),
),
);
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
return empty($this->value) && (string) $this->value !== '0';
}
/**
* {@inheritdoc}
*/
public function settingsForm(array &$form, array &$form_state, $has_data) {
$allowed_values = $this->getSetting('allowed_values');
$allowed_values_function = $this->getSetting('allowed_values_function');
$values = $allowed_values;
$off_value = array_shift($values);
$on_value = array_shift($values);
$element['allowed_values'] = array(
'#type' => 'value',
'#description' => '',
'#value_callback' => array(get_class($this), 'optionsBooleanAllowedValues'),
'#access' => empty($allowed_values_function),
);
$element['allowed_values']['on'] = array(
'#type' => 'textfield',
'#title' => t('On value'),
'#default_value' => $on_value,
'#required' => FALSE,
'#description' => t('If left empty, "1" will be used.'),
// Change #parents to make sure the element is not saved into field
// settings.
'#parents' => array('on'),
);
$element['allowed_values']['off'] = array(
'#type' => 'textfield',
'#title' => t('Off value'),
'#default_value' => $off_value,
'#required' => FALSE,
'#description' => t('If left empty, "0" will be used.'),
// Change #parents to make sure the element is not saved into field
// settings.
'#parents' => array('off'),
);
// Link the allowed value to the on / off elements to prepare for the rare
// case of an alter changing #parents.
$element['allowed_values']['#on_parents'] = &$element['allowed_values']['on']['#parents'];
$element['allowed_values']['#off_parents'] = &$element['allowed_values']['off']['#parents'];
$element['allowed_values_function'] = array(
'#type' => 'item',
'#title' => t('Allowed values list'),
'#markup' => t('The value of this field is being determined by the %function function and may not be changed.', array('%function' => $allowed_values_function)),
'#access' => !empty($allowed_values_function),
'#value' => $allowed_values_function,
);
return $element;
}
/**
* Form element #value_callback: assembles the allowed values for 'boolean'
* fields.
*/
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);
}
}

View File

@ -8,7 +8,6 @@
namespace Drupal\options\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\options\Plugin\Field\FieldWidget\OptionsWidgetBase;
/**
* Plugin implementation of the 'options_buttons' widget.
@ -17,10 +16,10 @@ use Drupal\options\Plugin\Field\FieldWidget\OptionsWidgetBase;
* id = "options_buttons",
* label = @Translation("Check boxes/radio buttons"),
* field_types = {
* "boolean",
* "list_integer",
* "list_float",
* "list_text",
* "list_boolean"
* },
* multiple_values = TRUE
* )

View File

@ -201,34 +201,6 @@ class OptionsFieldUITest extends FieldTestBase {
$this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
}
/**
* Options (boolean) : test 'On/Off' values input.
*/
function testOptionsAllowedValuesBoolean() {
$this->field_name = 'field_options_boolean';
$this->createOptionsField('list_boolean');
// Check that the separate 'On' and 'Off' form fields work.
$on = $this->randomName();
$off = $this->randomName();
$allowed_values = array(1 => $on, 0 => $off);
$edit = array(
'on' => $on,
'off' => $off,
);
$this->drupalPostForm($this->admin_path, $edit, t('Save field settings'));
$this->assertRaw(t('Updated field %label field settings.', array('%label' => $this->field_name)));
// Test the allowed_values on the field settings form.
$this->drupalGet($this->admin_path);
$this->assertFieldByName('on', $on, t("The 'On' value is stored correctly."));
$this->assertFieldByName('off', $off, t("The 'Off' value is stored correctly."));
$field = FieldConfig::loadByName('node', $this->field_name);
$this->assertEqual($field->getSetting('allowed_values'), $allowed_values, 'The allowed value is correct');
$this->assertNull($field->getSetting('on'), 'The on value is not saved into settings');
$this->assertNull($field->getSetting('off'), 'The off value is not saved into settings');
}
/**
* Options (text) : test 'trimmed values' input.
*/
@ -246,7 +218,7 @@ class OptionsFieldUITest extends FieldTestBase {
* Helper function to create list field of a given type.
*
* @param string $type
* 'list_integer', 'list_float', 'list_text' or 'list_boolean'
* 'list_integer', 'list_float' or 'list_text'
*/
protected function createOptionsField($type) {
// Create a test field and instance.
@ -296,14 +268,15 @@ class OptionsFieldUITest extends FieldTestBase {
*/
function testNodeDisplay() {
$this->field_name = strtolower($this->randomName());
$this->createOptionsField('list_boolean');
$this->createOptionsField('list_integer');
$node = $this->drupalCreateNode(array('type' => $this->type));
$on = $this->randomName();
$off = $this->randomName();
$edit = array(
'on' => $on,
'off' => $off,
'field[settings][allowed_values]' =>
"1|$on
0|$off",
);
$this->drupalPostForm($this->admin_path, $edit, t('Save field settings'));

View File

@ -37,13 +37,6 @@ class OptionsWidgetsTest extends FieldTestBase {
*/
protected $card_2;
/**
* A boolean field to use in this test class.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $bool;
/**
* A user with permission to view and manage entities.
*
@ -93,23 +86,6 @@ class OptionsWidgetsTest extends FieldTestBase {
));
$this->card_2->save();
// Boolean field.
$this->bool = entity_create('field_config', array(
'name' => 'bool',
'entity_type' => 'entity_test',
'type' => 'list_boolean',
'cardinality' => 1,
'settings' => array(
'allowed_values' => array(
// Make sure that 0 works as an option.
0 => 'Zero',
// Make sure that option text is properly sanitized.
1 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
),
),
));
$this->bool->save();
// Create a web user.
$this->web_user = $this->drupalCreateUser(array('view test entity', 'administer entity_test content'));
$this->drupalLogin($this->web_user);
@ -480,125 +456,4 @@ class OptionsWidgetsTest extends FieldTestBase {
$this->assertFieldValues($entity_init, 'card_2', array());
}
/**
* Tests the 'options_onoff' widget.
*/
function testOnOffCheckbox() {
// Create an instance of the 'boolean' field.
entity_create('field_instance_config', array(
'field' => $this->bool,
'bundle' => 'entity_test',
))->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->bool->getName(), array(
'type' => 'options_onoff',
))
->save();
// Create an entity.
$entity = entity_create('entity_test', array(
'user_id' => 1,
'name' => $this->randomName(),
));
$entity->save();
$entity_init = clone $entity;
// Display form: with no field data, option is unchecked.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertNoFieldChecked('edit-bool');
$this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
// Submit form: check the option.
$edit = array('bool' => TRUE);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'bool', array(1));
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertFieldChecked('edit-bool');
// Submit form: uncheck the option.
$edit = array('bool' => FALSE);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'bool', array(0));
// Display form: with 'off' value, option is unchecked.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertNoFieldChecked('edit-bool');
}
/**
* Tests that the 'options_onoff' widget has a 'display_label' setting.
*/
function testOnOffCheckboxLabelSetting() {
// Create Basic page node type.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
// Create admin user.
$admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer node fields', 'administer node form display', 'administer taxonomy'));
$this->drupalLogin($admin_user);
// Create a test field instance.
$field_name = 'bool';
entity_create('field_config', array(
'name' => $field_name,
'entity_type' => 'node',
'type' => 'list_boolean',
'cardinality' => 1,
'settings' => array(
'allowed_values' => array(
// Make sure that 0 works as an option.
0 => 'Zero',
// Make sure that option text is properly sanitized.
1 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
),
),
))->save();
entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => 'page',
))->save();
entity_get_form_display('node', 'page', 'default')
->setComponent($field_name, array(
'type' => 'options_onoff',
))
->save();
// Go to the form display page and check if the default settings works as
// expected.
$fieldEditUrl = 'admin/structure/types/manage/page/form-display';
$this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form.
$this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
$this->assertText(
'Use field label instead of the "On value" as label',
t('Display setting checkbox available.')
);
// Enable setting.
$edit = array('fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1);
$this->drupalPostAjaxForm(NULL, $edit, $field_name . "_plugin_settings_update");
$this->drupalPostForm(NULL, NULL, 'Save');
// Go again to the form display page and check if the setting
// is stored and has the expected effect.
$this->drupalGet($fieldEditUrl);
$this->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.');
$this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
$this->assertText(
'Use field label instead of the "On value" as label',
t('Display setting checkbox is available')
);
$this->assertFieldByXPath(
'*//input[@id="edit-fields-' . $field_name . '-settings-edit-form-settings-display-label" and @value="1"]',
TRUE,
t('Display label changes label of the checkbox')
);
}
}