function BooleanCheckboxWidget::formElement

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

Overrides WidgetInterface::formElement

File

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

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.