Commit a780b52e authored by catch's avatar catch

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

parent 518e3c9f
<?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;
}
}
......@@ -9,6 +9,8 @@
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 @@
* 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 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
);
}
/**
* {@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);
}
}
......@@ -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 @@ public static function defaultSettings() {
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 @@ public function settingsSummary() {
* {@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;
......
......@@ -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 @@
*/
abstract class TypedData implements TypedDataInterface, PluginInspectionInterface {
use StringTranslationTrait;
/**
* The data definition.
*
......
......@@ -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'
<?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);
}
}
<?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);
}
}
......@@ -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
......
......@@ -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
......
......@@ -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':
......
......@@ -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
......
......@@ -13,7 +13,7 @@ process:
plugin: static_map
source: type
map:
checkbox: list_boolean
checkbox: boolean
date: datetime
list: text
selection: list_text
......
......@@ -53,7 +53,7 @@ protected function setUp() {
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 @@ protected function setUp() {
entity_create('field_config', array(
'entity_type' => 'user',
'name' => 'profile_love_migrations',
'type' => 'list_boolean',
'type' => 'boolean',
))->save();
// Create the field instances.
......
......@@ -45,7 +45,7 @@ protected function setUp() {
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 @@ protected function setUp() {
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) {
......
......@@ -40,7 +40,7 @@ protected function setUp() {
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 @@ protected function setUp() {
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 @@ public function testUserProfileEntityFormDisplay() {
// 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);
}
......
......@@ -98,12 +98,12 @@ protected function createFields() {
$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(
......
......@@ -49,7 +49,7 @@ public function testUserProfileFields() {
// 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');
......
......@@ -53,11 +53,12 @@ public function setUp() {
$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();
......
......@@ -46,12 +46,13 @@ public function setUp() {
$field_private = entity_create('field_config', array(
'name' => 'field_private',
'entity_type' => 'node',
'type' => 'list_boolean',
'type' => 'boolean',
'cardinality' => 1,
'translatable' => TRUE,