Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldFormatter / NumericFormatterBase.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\AllowedTagsXssTrait;
6 use Drupal\Core\Field\FormatterBase;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Form\FormStateInterface;
9
10 /**
11  * Parent plugin for decimal and integer formatters.
12  */
13 abstract class NumericFormatterBase extends FormatterBase {
14
15   use AllowedTagsXssTrait;
16
17   /**
18    * {@inheritdoc}
19    */
20   public function settingsForm(array $form, FormStateInterface $form_state) {
21     $options = [
22       ''  => t('- None -'),
23       '.' => t('Decimal point'),
24       ',' => t('Comma'),
25       ' ' => t('Space'),
26       chr(8201) => t('Thin space'),
27       "'" => t('Apostrophe'),
28     ];
29     $elements['thousand_separator'] = [
30       '#type' => 'select',
31       '#title' => t('Thousand marker'),
32       '#options' => $options,
33       '#default_value' => $this->getSetting('thousand_separator'),
34       '#weight' => 0,
35     ];
36
37     $elements['prefix_suffix'] = [
38       '#type' => 'checkbox',
39       '#title' => t('Display prefix and suffix'),
40       '#default_value' => $this->getSetting('prefix_suffix'),
41       '#weight' => 10,
42     ];
43
44     return $elements;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function settingsSummary() {
51     $summary = [];
52
53     $summary[] = $this->numberFormat(1234.1234567890);
54     if ($this->getSetting('prefix_suffix')) {
55       $summary[] = t('Display with prefix and suffix.');
56     }
57
58     return $summary;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function viewElements(FieldItemListInterface $items, $langcode) {
65     $elements = [];
66     $settings = $this->getFieldSettings();
67
68     foreach ($items as $delta => $item) {
69       $output = $this->numberFormat($item->value);
70
71       // Account for prefix and suffix.
72       if ($this->getSetting('prefix_suffix')) {
73         $prefixes = isset($settings['prefix']) ? array_map(['Drupal\Core\Field\FieldFilteredMarkup', 'create'], explode('|', $settings['prefix'])) : [''];
74         $suffixes = isset($settings['suffix']) ? array_map(['Drupal\Core\Field\FieldFilteredMarkup', 'create'], explode('|', $settings['suffix'])) : [''];
75         $prefix = (count($prefixes) > 1) ? $this->formatPlural($item->value, $prefixes[0], $prefixes[1]) : $prefixes[0];
76         $suffix = (count($suffixes) > 1) ? $this->formatPlural($item->value, $suffixes[0], $suffixes[1]) : $suffixes[0];
77         $output = $prefix . $output . $suffix;
78       }
79       // Output the raw value in a content attribute if the text of the HTML
80       // element differs from the raw value (for example when a prefix is used).
81       if (isset($item->_attributes) && $item->value != $output) {
82         $item->_attributes += ['content' => $item->value];
83       }
84
85       $elements[$delta] = ['#markup' => $output];
86     }
87
88     return $elements;
89   }
90
91   /**
92    * Formats a number.
93    *
94    * @param mixed $number
95    *   The numeric value.
96    *
97    * @return string
98    *   The formatted number.
99    */
100   abstract protected function numberFormat($number);
101
102 }