function BooleanCheckboxWidget::formElement

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/BooleanCheckboxWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\BooleanCheckboxWidget::formElement()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/BooleanCheckboxWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\BooleanCheckboxWidget::formElement()
  3. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/BooleanCheckboxWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\BooleanCheckboxWidget::formElement()

Overrides WidgetBase::formElement

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/BooleanCheckboxWidget.php, line 59

Class

BooleanCheckboxWidget
Plugin implementation of the 'boolean_checkbox' widget.

Namespace

Drupal\Core\Field\Plugin\Field\FieldWidget

Code

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.