NumericFormatterBase.php 3 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase.
6 7
 */

8
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
9

10
use Drupal\Core\Field\AllowedTagsXssTrait;
11
use Drupal\Core\Field\FormatterBase;
12
use Drupal\Core\Field\FieldItemListInterface;
13
use Drupal\Core\Form\FormStateInterface;
14 15

/**
16
 * Parent plugin for decimal and integer formatters.
17
 */
18
abstract class NumericFormatterBase extends FormatterBase {
19

20 21
  use AllowedTagsXssTrait;

22
  /**
23
   * {@inheritdoc}
24
   */
25
  public function settingsForm(array $form, FormStateInterface $form_state) {
26
    $options = array(
27
      ''  => t('- None -'),
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
      '.' => t('Decimal point'),
      ',' => t('Comma'),
      ' ' => t('Space'),
      chr(8201) => t('Thin space'),
      "'" => t('Apostrophe'),
    );
    $elements['thousand_separator'] = array(
      '#type' => 'select',
      '#title' => t('Thousand marker'),
      '#options' => $options,
      '#default_value' => $this->getSetting('thousand_separator'),
      '#weight' => 0,
    );

    $elements['prefix_suffix'] = array(
      '#type' => 'checkbox',
44
      '#title' => t('Display prefix and suffix'),
45 46 47 48 49 50 51 52
      '#default_value' => $this->getSetting('prefix_suffix'),
      '#weight' => 10,
    );

    return $elements;
  }

  /**
53
   * {@inheritdoc}
54 55 56 57 58 59 60 61 62
   */
  public function settingsSummary() {
    $summary = array();

    $summary[] = $this->numberFormat(1234.1234567890);
    if ($this->getSetting('prefix_suffix')) {
      $summary[] = t('Display with prefix and suffix.');
    }

63
    return $summary;
64 65 66
  }

  /**
67
   * {@inheritdoc}
68
   */
69
  public function viewElements(FieldItemListInterface $items) {
70
    $elements = array();
71
    $settings = $this->getFieldSettings();
72 73

    foreach ($items as $delta => $item) {
74
      $output = $this->numberFormat($item->value);
75 76 77

      // Account for prefix and suffix.
      if ($this->getSetting('prefix_suffix')) {
78 79
        $prefixes = isset($settings['prefix']) ? array_map(array($this, 'fieldFilterXss'), explode('|', $settings['prefix'])) : array('');
        $suffixes = isset($settings['suffix']) ? array_map(array($this, 'fieldFilterXss'), explode('|', $settings['suffix'])) : array('');
80 81
        $prefix = (count($prefixes) > 1) ? $this->formatPlural($item->value, $prefixes[0], $prefixes[1]) : $prefixes[0];
        $suffix = (count($suffixes) > 1) ? $this->formatPlural($item->value, $suffixes[0], $suffixes[1]) : $suffixes[0];
82 83
        $output = $prefix . $output . $suffix;
      }
84 85 86 87 88
      // Output the raw value in a content attribute if the text of the HTML
      // element differs from the raw value (for example when a prefix is used).
      if (!empty($item->_attributes) && $item->value != $output) {
        $item->_attributes += array('content' => $item->value);
      }
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

      $elements[$delta] = array('#markup' => $output);
    }

    return $elements;
  }

  /**
   * Formats a number.
   *
   * @param mixed $number
   *   The numeric value.
   *
   * @return string
   *   The formatted number.
   */
  abstract protected function numberFormat($number);
106

107
}