function ColorPickerWidget::formElement

Same name in other branches
  1. 8.x-1.x field_example/src/Plugin/Field/FieldWidget/ColorPickerWidget.php \Drupal\field_example\Plugin\Field\FieldWidget\ColorPickerWidget::formElement()
  2. 4.0.x modules/field_example/src/Plugin/Field/FieldWidget/ColorPickerWidget.php \Drupal\field_example\Plugin\Field\FieldWidget\ColorPickerWidget::formElement()

Overrides TextWidget::formElement

File

modules/field_example/src/Plugin/Field/FieldWidget/ColorPickerWidget.php, line 25

Class

ColorPickerWidget
Plugin implementation of the 'field_example_colorpicker' widget.

Namespace

Drupal\field_example\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
    $element['value'] += [
        '#suffix' => '<div class="field-example-colorpicker"></div>',
        '#attributes' => [
            'class' => [
                'edit-field-example-colorpicker',
            ],
        ],
        '#attached' => [
            // Add Farbtastic color picker and javascript file to trigger the
            // colorpicker.
'library' => [
                'field_example/colorpicker',
            ],
        ],
    ];
    return $element;
}