Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldWidget / StringTextfieldWidget.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Plugin implementation of the 'string_textfield' widget.
11  *
12  * @FieldWidget(
13  *   id = "string_textfield",
14  *   label = @Translation("Textfield"),
15  *   field_types = {
16  *     "string"
17  *   }
18  * )
19  */
20 class StringTextfieldWidget extends WidgetBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function defaultSettings() {
26     return [
27       'size' => 60,
28       'placeholder' => '',
29     ] + parent::defaultSettings();
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function settingsForm(array $form, FormStateInterface $form_state) {
36     $element['size'] = [
37       '#type' => 'number',
38       '#title' => t('Size of textfield'),
39       '#default_value' => $this->getSetting('size'),
40       '#required' => TRUE,
41       '#min' => 1,
42     ];
43     $element['placeholder'] = [
44       '#type' => 'textfield',
45       '#title' => t('Placeholder'),
46       '#default_value' => $this->getSetting('placeholder'),
47       '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
48     ];
49     return $element;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function settingsSummary() {
56     $summary = [];
57
58     $summary[] = t('Textfield size: @size', ['@size' => $this->getSetting('size')]);
59     $placeholder = $this->getSetting('placeholder');
60     if (!empty($placeholder)) {
61       $summary[] = t('Placeholder: @placeholder', ['@placeholder' => $placeholder]);
62     }
63
64     return $summary;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
71     $element['value'] = $element + [
72       '#type' => 'textfield',
73       '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
74       '#size' => $this->getSetting('size'),
75       '#placeholder' => $this->getSetting('placeholder'),
76       '#maxlength' => $this->getFieldSetting('max_length'),
77       '#attributes' => ['class' => ['js-text-full', 'text-full']],
78     ];
79
80     return $element;
81   }
82
83 }