NumericFormatterBase.php 2.9 KB

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