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 @@ ...@@ -9,6 +9,8 @@
use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\AllowedValuesInterface;
use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\DataDefinition;
/** /**
...@@ -18,10 +20,21 @@ ...@@ -18,10 +20,21 @@
* id = "boolean", * id = "boolean",
* label = @Translation("Boolean"), * label = @Translation("Boolean"),
* description = @Translation("An entity field containing a boolean value."), * 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} * {@inheritdoc}
...@@ -48,4 +61,56 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) ...@@ -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 @@ ...@@ -2,27 +2,27 @@
/** /**
* @file * @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\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( * @FieldWidget(
* id = "options_onoff", * id = "boolean_checkbox",
* label = @Translation("Single on/off checkbox"), * label = @Translation("Single on/off checkbox"),
* field_types = { * field_types = {
* "list_boolean" * "boolean"
* }, * },
* multiple_values = TRUE * multiple_values = TRUE
* ) * )
*/ */
class OnOffWidget extends OptionsWidgetBase { class BooleanCheckboxWidget extends WidgetBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -39,7 +39,7 @@ public static function defaultSettings() { ...@@ -39,7 +39,7 @@ public static function defaultSettings() {
public function settingsForm(array $form, array &$form_state) { public function settingsForm(array $form, array &$form_state) {
$element['display_label'] = array( $element['display_label'] = array(
'#type' => 'checkbox', '#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'), '#default_value' => $this->getSetting('display_label'),
'#weight' => -1, '#weight' => -1,
); );
...@@ -62,22 +62,17 @@ public function settingsSummary() { ...@@ -62,22 +62,17 @@ public function settingsSummary() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) { public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state); $element['value'] = $element + array(
$options = $this->getOptions($items[$delta]);
$selected = $this->getSelectedOptions($items);
$element += array(
'#type' => 'checkbox', '#type' => 'checkbox',
'#default_value' => !empty($selected[0]), '#default_value' => !empty($items[0]->value),
); );
// Override the title from the incoming $element. // Override the title from the incoming $element.
if ($this->getSetting('display_label')) { if ($this->getSetting('display_label')) {
$element['#title'] = $this->fieldDefinition->getLabel(); $element['value']['#title'] = $this->fieldDefinition->getLabel();
} }
else { else {
$element['#title'] = isset($options[1]) ? $options[1] : ''; $element['value']['#title'] = $this->fieldDefinition->getSetting('on_label');
} }
return $element; return $element;
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\Core\TypedData; namespace Drupal\Core\TypedData;
use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/** /**
* The abstract base class for typed data. * The abstract base class for typed data.
...@@ -19,6 +20,8 @@ ...@@ -19,6 +20,8 @@
*/ */
abstract class TypedData implements TypedDataInterface, PluginInspectionInterface { abstract class TypedData implements TypedDataInterface, PluginInspectionInterface {
use StringTranslationTrait;
/** /**
* The data definition. * The data definition.
* *
......
...@@ -97,6 +97,34 @@ entity_form_display.field.hidden: ...@@ -97,6 +97,34 @@ entity_form_display.field.hidden:
sequence: sequence:
- type: string - 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. # Schema for the configuration files of the Email field type.
field.email.settings: field.email.settings:
...@@ -291,3 +319,15 @@ entity_form_display.field.number: ...@@ -291,3 +319,15 @@ entity_form_display.field.number:
placeholder: placeholder:
type: label type: label
label: 'Placeholder' 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 ...@@ -2,12 +2,10 @@ id: taxonomy_term.forum_container
status: true status: true
langcode: en langcode: en
name: forum_container name: forum_container
type: list_boolean type: boolean
settings: settings:
allowed_values: on_label: Yes
- '' off_label: No
- ''
allowed_values_function: ''
module: options module: options
entity_type: taxonomy_term entity_type: taxonomy_term
locked: true locked: true
......
...@@ -9,10 +9,10 @@ description: '' ...@@ -9,10 +9,10 @@ description: ''
required: true required: true
default_value: default_value:
- -
value: false value: 0
default_value_function: '' default_value_function: ''
settings: { } settings: { }
field_type: list_boolean field_type: boolean
dependencies: dependencies:
entity: entity:
- field.field.taxonomy_term.forum_container - field.field.taxonomy_term.forum_container
......
...@@ -44,7 +44,7 @@ process: ...@@ -44,7 +44,7 @@ process:
filefield_widget: file_generic filefield_widget: file_generic
imagefield_widget: image_image imagefield_widget: image_image
phone_textfield: telephone_default phone_textfield: telephone_default
optionwidgets_onoff: options_onoff optionwidgets_onoff: checkbox
optionwidgets_buttons: options_buttons optionwidgets_buttons: options_buttons
optionwidgets_select: options_select optionwidgets_select: options_select
'options/settings': 'options/settings':
......
...@@ -20,7 +20,7 @@ process: ...@@ -20,7 +20,7 @@ process:
plugin: static_map plugin: static_map
source: type source: type
map: map:
checkbox: options_onoff checkbox: boolean_checkbox
date: datetime_default date: datetime_default
list: text_textfield list: text_textfield
selection: options_select selection: options_select
......
...@@ -13,7 +13,7 @@ process: ...@@ -13,7 +13,7 @@ process:
plugin: static_map plugin: static_map
source: type source: type
map: map:
checkbox: list_boolean checkbox: boolean
date: datetime date: datetime
list: text list: text
selection: list_text selection: list_text
......
...@@ -53,7 +53,7 @@ protected function setUp() { ...@@ -53,7 +53,7 @@ protected function setUp() {
entity_create('field_config', array( entity_create('field_config', array(
'entity_type' => 'user', 'entity_type' => 'user',
'name' => 'profile_sell_address', 'name' => 'profile_sell_address',
'type' => 'list_boolean', 'type' => 'boolean',
))->save(); ))->save();
entity_create('field_config', array( entity_create('field_config', array(
'entity_type' => 'user', 'entity_type' => 'user',
...@@ -85,7 +85,7 @@ protected function setUp() { ...@@ -85,7 +85,7 @@ protected function setUp() {
entity_create('field_config', array( entity_create('field_config', array(
'entity_type' => 'user', 'entity_type' => 'user',
'name' => 'profile_love_migrations', 'name' => 'profile_love_migrations',
'type' => 'list_boolean', 'type' => 'boolean',
))->save(); ))->save();
// Create the field instances. // Create the field instances.
......
...@@ -45,7 +45,7 @@ protected function setUp() { ...@@ -45,7 +45,7 @@ protected function setUp() {
entity_create('field_config', array( entity_create('field_config', array(
'entity_type' => 'user', 'entity_type' => 'user',
'name' => 'profile_sell_address', 'name' => 'profile_sell_address',
'type' => 'list_boolean',