class BooleanCheckboxWidget
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/BooleanCheckboxWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\BooleanCheckboxWidget
Plugin implementation of the 'boolean_checkbox' widget.
Plugin annotation
@FieldWidget(
id = "boolean_checkbox",
label = @Translation("Single on/off checkbox"),
field_types = {
"boolean"
},
multiple_values = TRUE
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Field\WidgetBase extends \Drupal\Core\Field\WidgetInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface implements \Drupal\Core\Field\PluginSettingsBase
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\BooleanCheckboxWidget implements \Drupal\Core\Field\WidgetBase
- class \Drupal\Core\Field\WidgetBase extends \Drupal\Core\Field\WidgetInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface implements \Drupal\Core\Field\PluginSettingsBase
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of BooleanCheckboxWidget
File
-
core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldWidget/ BooleanCheckboxWidget.php, line 21
Namespace
Drupal\Core\Field\Plugin\Field\FieldWidgetView source
class BooleanCheckboxWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'display_label' => TRUE,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['display_label'] = [
'#type' => 'checkbox',
'#title' => $this->t('Use field label instead of the "On" label as the label.'),
'#default_value' => $this->getSetting('display_label'),
'#weight' => -1,
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$display_label = $this->getSetting('display_label');
$summary[] = $this->t('Use field label: @display_label', [
'@display_label' => $display_label ? $this->t('Yes') : $this->t('No'),
]);
return $summary;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['value'] = $element + [
'#type' => 'checkbox',
'#default_value' => !empty($items[0]->value),
];
// Override the title from the incoming $element.
if ($this->getSetting('display_label')) {
$element['value']['#title'] = $this->fieldDefinition
->getLabel();
}
else {
$element['value']['#title'] = $this->fieldDefinition
->getSetting('on_label');
}
return $element;
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.