50eb767f9971118204c6660e4b9c063041041cf6
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldType / NumericItemBase.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldItemBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Base class for numeric configurable field types.
10  */
11 abstract class NumericItemBase extends FieldItemBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public static function defaultFieldSettings() {
17     return [
18       'min' => '',
19       'max' => '',
20       'prefix' => '',
21       'suffix' => '',
22     ] + parent::defaultFieldSettings();
23   }
24
25   /**
26    * {@inheritdoc}
27    */
28   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
29     $element = [];
30     $settings = $this->getSettings();
31
32     $element['min'] = [
33       '#type' => 'number',
34       '#title' => t('Minimum'),
35       '#default_value' => $settings['min'],
36       '#description' => t('The minimum value that should be allowed in this field. Leave blank for no minimum.'),
37     ];
38     $element['max'] = [
39       '#type' => 'number',
40       '#title' => t('Maximum'),
41       '#default_value' => $settings['max'],
42       '#description' => t('The maximum value that should be allowed in this field. Leave blank for no maximum.'),
43     ];
44     $element['prefix'] = [
45       '#type' => 'textfield',
46       '#title' => t('Prefix'),
47       '#default_value' => $settings['prefix'],
48       '#size' => 60,
49       '#description' => t("Define a string that should be prefixed to the value, like '$ ' or '&euro; '. Leave blank for none. Separate singular and plural values with a pipe ('pound|pounds')."),
50     ];
51     $element['suffix'] = [
52       '#type' => 'textfield',
53       '#title' => t('Suffix'),
54       '#default_value' => $settings['suffix'],
55       '#size' => 60,
56       '#description' => t("Define a string that should be suffixed to the value, like ' m', ' kb/s'. Leave blank for none. Separate singular and plural values with a pipe ('pound|pounds')."),
57     ];
58
59     return $element;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function isEmpty() {
66     if (empty($this->value) && (string) $this->value !== '0') {
67       return TRUE;
68     }
69     return FALSE;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function getConstraints() {
76     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
77     $constraints = parent::getConstraints();
78
79     $settings = $this->getSettings();
80     $label = $this->getFieldDefinition()->getLabel();
81
82     if (!empty($settings['min'])) {
83       $min = $settings['min'];
84       $constraints[] = $constraint_manager->create('ComplexData', [
85         'value' => [
86           'Range' => [
87             'min' => $min,
88             'minMessage' => t('%name: the value may be no less than %min.', ['%name' => $label, '%min' => $min]),
89           ],
90         ],
91       ]);
92     }
93
94     if (!empty($settings['max'])) {
95       $max = $settings['max'];
96       $constraints[] = $constraint_manager->create('ComplexData', [
97         'value' => [
98           'Range' => [
99             'max' => $max,
100             'maxMessage' => t('%name: the value may be no greater than %max.', ['%name' => $label, '%max' => $max]),
101           ],
102         ],
103       ]);
104     }
105
106     return $constraints;
107   }
108
109   /**
110    * Helper method to truncate a decimal number to a given number of decimals.
111    *
112    * @param float $decimal
113    *   Decimal number to truncate.
114    * @param int $num
115    *   Number of digits the output will have.
116    *
117    * @return float
118    *   Decimal number truncated.
119    */
120   protected static function truncateDecimal($decimal, $num) {
121     return floor($decimal * pow(10, $num)) / pow(10, $num);
122   }
123
124 }