class OnOffWidget

Plugin implementation of the 'options_onoff' widget.

Plugin annotation


@Plugin(
  id = "options_onoff",
  module = "options",
  label = @Translation("Single on/off checkbox"),
  field_types = {
    "list_boolean"
  },
  settings = {
    "display_label" = FALSE,
  },
  multiple_values = TRUE
)

Hierarchy

Expanded class hierarchy of OnOffWidget

File

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

Namespace

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

  /**
   * {@inheritdoc}
   */
  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'),
      '#default_value' => $this
        ->getSetting('display_label'),
      '#weight' => -1,
    );
    return $element;
  }

  /**
   * {@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);
    $element += array(
      '#type' => 'checkbox',
      '#default_value' => !empty($selected[0]),
    );

    // Override the title from the incoming $element.
    if ($this
      ->getSetting('display_label')) {
      $element['#title'] = $this->instance['label'];
    }
    else {
      $element['#title'] = isset($options[1]) ? $options[1] : '';
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OnOffWidget::formElement public function Returns the form for a single field widget. Overrides OptionsWidgetBase::formElement
OnOffWidget::settingsForm public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). Overrides WidgetBase::settingsForm
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::getEmptyOption protected function Returns the empty option to add to the list of options, if any. 2
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::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.