abstract class DefaultNumberFormatter

Parent plugin for decimal and integer formatters

Hierarchy

Expanded class hierarchy of DefaultNumberFormatter

2 files declare their use of DefaultNumberFormatter
NumberDecimalFormatter.php in drupal/core/modules/field/modules/number/lib/Drupal/number/Plugin/field/formatter/NumberDecimalFormatter.php
Definition of Drupal\number\Plugin\field\formatter\NumberDecimalFormatter.
NumberIntegerFormatter.php in drupal/core/modules/field/modules/number/lib/Drupal/number/Plugin/field/formatter/NumberIntegerFormatter.php
Definition of Drupal\number\Plugin\field\formatter\NumberIntegerFormatter.

File

drupal/core/modules/field/modules/number/lib/Drupal/number/Plugin/field/formatter/DefaultNumberFormatter.php, line 18
Definition of Drupal\number\Plugin\field\formatter\DefaultNumberFormatter.

Namespace

Drupal\number\Plugin\field\formatter
View source
abstract class DefaultNumberFormatter extends FormatterBase {

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  public function settingsForm(array $form, array &$form_state) {
    $options = array(
      '' => t('<none>'),
      '.' => 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',
      '#title' => t('Display prefix and suffix.'),
      '#default_value' => $this
        ->getSetting('prefix_suffix'),
      '#weight' => 10,
    );
    return $elements;
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  public function settingsSummary() {
    $summary = array();
    $summary[] = $this
      ->numberFormat(1234.123456789);
    if ($this
      ->getSetting('prefix_suffix')) {
      $summary[] = t('Display with prefix and suffix.');
    }
    return implode('<br />', $summary);
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
   */
  public function viewElements(EntityInterface $entity, $langcode, array $items) {
    $elements = array();
    foreach ($items as $delta => $item) {
      $output = $this
        ->numberFormat($item['value']);

      // Account for prefix and suffix.
      if ($this
        ->getSetting('prefix_suffix')) {
        $prefixes = isset($instance['settings']['prefix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['prefix'])) : array(
          '',
        );
        $suffixes = isset($instance['settings']['suffix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['suffix'])) : array(
          '',
        );
        $prefix = count($prefixes) > 1 ? format_plural($item['value'], $prefixes[0], $prefixes[1]) : $prefixes[0];
        $suffix = count($suffixes) > 1 ? format_plural($item['value'], $suffixes[0], $suffixes[1]) : $suffixes[0];
        $output = $prefix . $output . $suffix;
      }
      $elements[$delta] = array(
        '#markup' => $output,
      );
    }
    return $elements;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultNumberFormatter::numberFormat abstract protected function Formats a number. 2
DefaultNumberFormatter::settingsForm public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). Overrides FormatterBase::settingsForm 1
DefaultNumberFormatter::settingsSummary public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). Overrides FormatterBase::settingsSummary
DefaultNumberFormatter::viewElements public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). Overrides FormatterInterface::viewElements
FormatterBase::$field protected property The field definition.
FormatterBase::$instance protected property The field instance definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::$weight protected property The formatter weight.
FormatterBase::prepareView public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView(). Overrides FormatterInterface::prepareView 2
FormatterBase::view public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::view(). Overrides FormatterInterface::view
FormatterBase::__construct public function Constructs a FormatterBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$discovery protected property The discovery object.
PluginBase::$plugin_id protected property The plugin_id.
PluginBase::getDefinition public function Implements Drupal\Component\Plugin\PluginInterface::getDefinition(). Overrides PluginInspectionInterface::getDefinition
PluginBase::getPluginId public function Implements Drupal\Component\Plugin\PluginInterface::getPluginId(). Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings