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 / FieldFormatter / DecimalFormatter.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Plugin implementation of the 'number_decimal' formatter.
9  *
10  * The 'Default' formatter is different for integer fields on the one hand, and
11  * for decimal and float fields on the other hand, in order to be able to use
12  * different settings.
13  *
14  * @FieldFormatter(
15  *   id = "number_decimal",
16  *   label = @Translation("Default"),
17  *   field_types = {
18  *     "decimal",
19  *     "float"
20  *   }
21  * )
22  */
23 class DecimalFormatter extends NumericFormatterBase {
24
25   /**
26    * {@inheritdoc}
27    */
28   public static function defaultSettings() {
29     return [
30       'thousand_separator' => '',
31       'decimal_separator' => '.',
32       'scale' => 2,
33       'prefix_suffix' => TRUE,
34     ] + parent::defaultSettings();
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function settingsForm(array $form, FormStateInterface $form_state) {
41     $elements = parent::settingsForm($form, $form_state);
42
43     $elements['decimal_separator'] = [
44       '#type' => 'select',
45       '#title' => t('Decimal marker'),
46       '#options' => ['.' => t('Decimal point'), ',' => t('Comma')],
47       '#default_value' => $this->getSetting('decimal_separator'),
48       '#weight' => 5,
49     ];
50     $elements['scale'] = [
51       '#type' => 'number',
52       '#title' => t('Scale', [], ['context' => 'decimal places']),
53       '#min' => 0,
54       '#max' => 10,
55       '#default_value' => $this->getSetting('scale'),
56       '#description' => t('The number of digits to the right of the decimal.'),
57       '#weight' => 6,
58     ];
59
60     return $elements;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   protected function numberFormat($number) {
67     return number_format($number, $this->getSetting('scale'), $this->getSetting('decimal_separator'), $this->getSetting('thousand_separator'));
68   }
69
70 }