TextfieldWidget.php

Same filename in other branches
  1. 9 core/modules/text/src/Plugin/Field/FieldWidget/TextfieldWidget.php
  2. 10 core/modules/text/src/Plugin/Field/FieldWidget/TextfieldWidget.php
  3. 11.x core/modules/text/src/Plugin/Field/FieldWidget/TextfieldWidget.php

Namespace

Drupal\text\Plugin\Field\FieldWidget

File

core/modules/text/src/Plugin/Field/FieldWidget/TextfieldWidget.php

View source
<?php

namespace Drupal\text\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;

/**
 * Plugin implementation of the 'text_textfield' widget.
 *
 * @FieldWidget(
 *   id = "text_textfield",
 *   label = @Translation("Text field"),
 *   field_types = {
 *     "text"
 *   },
 * )
 */
class TextfieldWidget extends StringTextfieldWidget {
    
    /**
     * {@inheritdoc}
     */
    public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
        $main_widget = parent::formElement($items, $delta, $element, $form, $form_state);
        $element = $main_widget['value'];
        $element['#type'] = 'text_format';
        $element['#format'] = isset($items[$delta]->format) ? $items[$delta]->format : NULL;
        $element['#base_type'] = $main_widget['value']['#type'];
        return $element;
    }
    
    /**
     * {@inheritdoc}
     */
    public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
        if ($violation->arrayPropertyPath == [
            'format',
        ] && isset($element['format']['#access']) && !$element['format']['#access']) {
            // Ignore validation errors for formats that may not be changed,
            // such as when existing formats become invalid.
            // See \Drupal\filter\Element\TextFormat::processFormat().
            return FALSE;
        }
        return $element;
    }

}

Classes

Title Deprecated Summary
TextfieldWidget Plugin implementation of the 'text_textfield' widget.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.