af8493de0837bc05e801e13e023354e58e9f6f0d
[yaffs-website] / web / core / modules / text / src / Plugin / Field / FieldWidget / TextfieldWidget.php
1 <?php
2
3 namespace Drupal\text\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\Validator\ConstraintViolationInterface;
9
10 /**
11  * Plugin implementation of the 'text_textfield' widget.
12  *
13  * @FieldWidget(
14  *   id = "text_textfield",
15  *   label = @Translation("Text field"),
16  *   field_types = {
17  *     "text"
18  *   },
19  * )
20  */
21 class TextfieldWidget extends StringTextfieldWidget {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
27     $main_widget = parent::formElement($items, $delta, $element, $form, $form_state);
28
29     $element = $main_widget['value'];
30     $element['#type'] = 'text_format';
31     $element['#format'] = isset($items[$delta]->format) ? $items[$delta]->format : NULL;
32     $element['#base_type'] = $main_widget['value']['#type'];
33     return $element;
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
40     if ($violation->arrayPropertyPath == ['format'] && isset($element['format']['#access']) && !$element['format']['#access']) {
41       // Ignore validation errors for formats if formats may not be changed,
42       // i.e. when existing formats become invalid. See filter_process_format().
43       return FALSE;
44     }
45     return $element;
46   }
47
48 }