class TextWidget

Same name in this branch
  1. 8.x-1.x field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php \Drupal\field_permission_example\Plugin\Field\FieldWidget\TextWidget
Same name in other branches
  1. 3.x modules/field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php \Drupal\field_permission_example\Plugin\Field\FieldWidget\TextWidget
  2. 3.x modules/field_example/src/Plugin/Field/FieldWidget/TextWidget.php \Drupal\field_example\Plugin\Field\FieldWidget\TextWidget
  3. 4.0.x modules/field_permission_example/src/Plugin/Field/FieldWidget/TextWidget.php \Drupal\field_permission_example\Plugin\Field\FieldWidget\TextWidget
  4. 4.0.x modules/field_example/src/Plugin/Field/FieldWidget/TextWidget.php \Drupal\field_example\Plugin\Field\FieldWidget\TextWidget

Plugin implementation of the 'field_example_text' widget.

Plugin annotation


@FieldWidget(
  id = "field_example_text",
  module = "field_example",
  label = @Translation("RGB value as #ffffff"),
  field_types = {
    "field_example_rgb"
  }
)

Hierarchy

  • class \Drupal\field_example\Plugin\Field\FieldWidget\TextWidget extends \Drupal\Core\Field\WidgetBase

Expanded class hierarchy of TextWidget

File

field_example/src/Plugin/Field/FieldWidget/TextWidget.php, line 22

Namespace

Drupal\field_example\Plugin\Field\FieldWidget
View source
class TextWidget extends WidgetBase {
    
    /**
     * {@inheritdoc}
     */
    public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
        $value = isset($items[$delta]->value) ? $items[$delta]->value : '';
        $element += [
            '#type' => 'textfield',
            '#default_value' => $value,
            '#size' => 7,
            '#maxlength' => 7,
            '#element_validate' => [
                [
                    $this,
                    'validate',
                ],
            ],
        ];
        return [
            'value' => $element,
        ];
    }
    
    /**
     * Validate the color text field.
     */
    public function validate($element, FormStateInterface $form_state) {
        $value = $element['#value'];
        if (strlen($value) === 0) {
            $form_state->setValueForElement($element, '');
            return;
        }
        if (!Color::validateHex($value)) {
            $form_state->setError($element, $this->t('Color must be a 3- or 6-digit hexadecimal value, suitable for CSS.'));
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
TextWidget::formElement public function 1
TextWidget::validate public function Validate the color text field.