Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldWidget / NumberWidget.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldFilteredMarkup;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\WidgetBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\Validator\ConstraintViolationInterface;
10
11 /**
12  * Plugin implementation of the 'number' widget.
13  *
14  * @FieldWidget(
15  *   id = "number",
16  *   label = @Translation("Number field"),
17  *   field_types = {
18  *     "integer",
19  *     "decimal",
20  *     "float"
21  *   }
22  * )
23  */
24 class NumberWidget extends WidgetBase {
25
26   /**
27    * {@inheritdoc}
28    */
29   public static function defaultSettings() {
30     return [
31       'placeholder' => '',
32     ] + parent::defaultSettings();
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function settingsForm(array $form, FormStateInterface $form_state) {
39     $element['placeholder'] = [
40       '#type' => 'textfield',
41       '#title' => t('Placeholder'),
42       '#default_value' => $this->getSetting('placeholder'),
43       '#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.'),
44     ];
45     return $element;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function settingsSummary() {
52     $summary = [];
53
54     $placeholder = $this->getSetting('placeholder');
55     if (!empty($placeholder)) {
56       $summary[] = t('Placeholder: @placeholder', ['@placeholder' => $placeholder]);
57     }
58     else {
59       $summary[] = t('No placeholder');
60     }
61
62     return $summary;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
69     $value = isset($items[$delta]->value) ? $items[$delta]->value : NULL;
70     $field_settings = $this->getFieldSettings();
71
72     $element += [
73       '#type' => 'number',
74       '#default_value' => $value,
75       '#placeholder' => $this->getSetting('placeholder'),
76     ];
77
78     // Set the step for floating point and decimal numbers.
79     switch ($this->fieldDefinition->getType()) {
80       case 'decimal':
81         $element['#step'] = pow(0.1, $field_settings['scale']);
82         break;
83
84       case 'float':
85         $element['#step'] = 'any';
86         break;
87     }
88
89     // Set minimum and maximum.
90     if (is_numeric($field_settings['min'])) {
91       $element['#min'] = $field_settings['min'];
92     }
93     if (is_numeric($field_settings['max'])) {
94       $element['#max'] = $field_settings['max'];
95     }
96
97     // Add prefix and suffix.
98     if ($field_settings['prefix']) {
99       $prefixes = explode('|', $field_settings['prefix']);
100       $element['#field_prefix'] = FieldFilteredMarkup::create(array_pop($prefixes));
101     }
102     if ($field_settings['suffix']) {
103       $suffixes = explode('|', $field_settings['suffix']);
104       $element['#field_suffix'] = FieldFilteredMarkup::create(array_pop($suffixes));
105     }
106
107     return ['value' => $element];
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
114     return $element['value'];
115   }
116
117 }