class ButtonsWidget

Plugin implementation of the 'options_buttons' widget.

Plugin annotation


@Plugin(
  id = "options_buttons",
  module = "options",
  label = @Translation("Check boxes/radio buttons"),
  field_types = {
    "list_integer",
    "list_float",
    "list_text",
    "list_boolean"
  },
  multiple_values = TRUE
)

Hierarchy

Expanded class hierarchy of ButtonsWidget

File

drupal/core/modules/options/lib/Drupal/options/Plugin/field/widget/ButtonsWidget.php, line 29
Contains \Drupal\options\Plugin\field\widget\ButtonsWidget.

Namespace

Drupal\options\Plugin\field\widget
View source
class ButtonsWidget extends OptionsWidgetBase {

  /**
   * {@inheritdoc}
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state);
    $options = $this
      ->getOptions();
    $selected = $this
      ->getSelectedOptions($items);

    // If required and there is one single option, preselect it.
    if ($this->required && count($options) == 1) {
      reset($options);
      $selected = array(
        key($options),
      );
    }
    if ($this->multiple) {
      $element += array(
        '#type' => 'checkboxes',
        '#default_value' => $selected,
        '#options' => $options,
      );
    }
    else {
      $element += array(
        '#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 getEmptyOption() {
    if (!$this->required && !$this->multiple) {
      return static::OPTIONS_EMPTY_NONE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ButtonsWidget::formElement public function Returns the form for a single field widget. Overrides OptionsWidgetBase::formElement
ButtonsWidget::getEmptyOption protected function Returns the empty option to add to the list of options, if any. Overrides OptionsWidgetBase::getEmptyOption
OptionsWidgetBase::$column protected property Abstract over the actual field columns, to allow different field types to reuse those widgets.
OptionsWidgetBase::flattenOptions protected function Flattens an array of allowed values.
OptionsWidgetBase::getOptions protected function Returns the array of options for the widget.
OptionsWidgetBase::getSelectedOptions protected function Determines selected options from the incoming field values.
OptionsWidgetBase::OPTIONS_EMPTY_NONE constant Identifies a 'None' option.
OptionsWidgetBase::OPTIONS_EMPTY_SELECT constant Identifies a 'Select a value' option.
OptionsWidgetBase::sanitizeLabel protected static function Sanitizes a string label to display as an option. 1
OptionsWidgetBase::supportsGroups protected function Indicates whether the widgets support optgroups. 1
OptionsWidgetBase::validateElement public static function Form validation handler for widget elements.
OptionsWidgetBase::__construct public function Constructs a WidgetBase object. Overrides WidgetBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
WidgetBase::$field protected property The field definition.
WidgetBase::$instance protected property The field instance definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::errorElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement(). Overrides WidgetInterface::errorElement 5
WidgetBase::extractFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues(). Overrides WidgetBaseInterface::extractFormValues
WidgetBase::flagErrors public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). Overrides WidgetBaseInterface::flagErrors
WidgetBase::form public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form(). Overrides WidgetBaseInterface::form
WidgetBase::formMultipleElements protected function Special handling to create form elements for multiple values. 1
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::massageFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() Overrides WidgetInterface::massageFormValues 2
WidgetBase::settingsForm public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). Overrides WidgetInterface::settingsForm 13
WidgetBase::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.