class OptionsButtonsWidget
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsButtonsWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsButtonsWidget
- 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsButtonsWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsButtonsWidget
Plugin implementation of the 'options_buttons' widget.
Plugin annotation
@FieldWidget(
id = "options_buttons",
label = @Translation("Check boxes/radio buttons"),
field_types = {
"boolean",
"entity_reference",
"list_integer",
"list_float",
"list_string",
},
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\OptionsWidgetBase implements \Drupal\Core\Field\WidgetBase
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsButtonsWidget implements \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase 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 OptionsButtonsWidget
File
-
core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldWidget/ OptionsButtonsWidget.php, line 24
Namespace
Drupal\Core\Field\Plugin\Field\FieldWidgetView source
class OptionsButtonsWidget extends OptionsWidgetBase {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$options = $this->getOptions($items->getEntity());
$selected = $this->getSelectedOptions($items);
// If required and there is one single option, preselect it.
if ($this->required && count($options) == 1) {
reset($options);
$selected = [
key($options),
];
}
if ($this->multiple) {
$element += [
'#type' => 'checkboxes',
'#default_value' => $selected,
'#options' => $options,
];
}
else {
$element += [
'#type' => 'radios',
// Radio buttons need a scalar value. Take the first default value, or
// default to NULL so that the form element is properly recognized as
// not having a default value.
'#default_value' => $selected ? reset($selected) : NULL,
'#options' => $options,
];
}
return $element;
}
/**
* {@inheritdoc}
*/
protected function getEmptyLabel() {
if (!$this->required && !$this->multiple) {
return $this->t('N/A');
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.