You are here

RangeIntegerFormatter.php in Range 8

File

src/Plugin/Field/FieldFormatter/RangeIntegerFormatter.php
View source
<?php

namespace Drupal\range\Plugin\Field\FieldFormatter;

use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'range_integer' formatter.
 *
 * The 'Default' formatter is different for integer fields on the one hand, and
 * for decimal and float fields on the other hand, in order to be able to use
 * different settings.
 *
 * @FieldFormatter(
 *   id = "range_integer",
 *   label = @Translation("Default"),
 *   field_types = {
 *     "range_integer"
 *   }
 * )
 */
class RangeIntegerFormatter extends RangeFormatterBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'thousand_separator' => '',
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = parent::settingsForm($form, $form_state);
    $options = [
      '' => $this
        ->t('- None -'),
      '.' => $this
        ->t('Decimal point'),
      ',' => $this
        ->t('Comma'),
      ' ' => $this
        ->t('Space'),
      chr(8201) => $this
        ->t('Thin space'),
      "'" => $this
        ->t('Apostrophe'),
    ];
    $elements['thousand_separator'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Thousand marker'),
      '#options' => $options,
      '#default_value' => $this
        ->getSetting('thousand_separator'),
      '#weight' => 5,
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  protected function formatNumber($number) {
    return number_format($number, 0, '', $this
      ->getSetting('thousand_separator'));
  }

}

Classes

Namesort descending Description
RangeIntegerFormatter Plugin implementation of the 'range_integer' formatter.