View source
<?php
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
class DecimalFormatter extends NumericFormatterBase {
public static function defaultSettings() {
return array(
'thousand_separator' => '',
'decimal_separator' => '.',
'scale' => 2,
'prefix_suffix' => TRUE,
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['decimal_separator'] = array(
'#type' => 'select',
'#title' => t('Decimal marker'),
'#options' => array(
'.' => t('Decimal point'),
',' => t('Comma'),
),
'#default_value' => $this
->getSetting('decimal_separator'),
'#weight' => 5,
);
$range = range(0, 10);
$elements['scale'] = array(
'#type' => 'select',
'#title' => t('Scale', array(), array(
'decimal places',
)),
'#options' => array_combine($range, $range),
'#default_value' => $this
->getSetting('scale'),
'#description' => t('The number of digits to the right of the decimal.'),
'#weight' => 6,
);
return $elements;
}
protected function numberFormat($number) {
return number_format($number, $this
->getSetting('scale'), $this
->getSetting('decimal_separator'), $this
->getSetting('thousand_separator'));
}
}