class DecimalFormatter
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/DecimalFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\DecimalFormatter
- 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/DecimalFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\DecimalFormatter
- 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/DecimalFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\DecimalFormatter
Plugin implementation of the 'number_decimal' 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.
Attributes
#[FieldFormatter(id: 'number_decimal', label: new TranslatableMarkup('Default'), field_types: [
'decimal',
'float',
])]
Hierarchy
- class \Drupal\Component\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Field\FormatterBase extends \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface implements \Drupal\Core\Field\PluginSettingsBase
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase implements \Drupal\Core\Field\FormatterBase
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\DecimalFormatter implements \Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase implements \Drupal\Core\Field\FormatterBase
- class \Drupal\Core\Field\FormatterBase extends \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface implements \Drupal\Core\Field\PluginSettingsBase
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of DecimalFormatter
File
-
core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldFormatter/ DecimalFormatter.php, line 16
Namespace
Drupal\Core\Field\Plugin\Field\FieldFormatterView source
class DecimalFormatter extends NumericFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'thousand_separator' => '',
'decimal_separator' => '.',
'scale' => 2,
'prefix_suffix' => TRUE,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['decimal_separator'] = [
'#type' => 'select',
'#title' => $this->t('Decimal marker'),
'#options' => [
'.' => $this->t('Decimal point'),
',' => $this->t('Comma'),
],
'#default_value' => $this->getSetting('decimal_separator'),
'#weight' => 5,
];
$elements['scale'] = [
'#type' => 'number',
'#title' => $this->t('Scale', [], [
'context' => 'decimal places',
]),
'#min' => 0,
'#max' => 10,
'#default_value' => $this->getSetting('scale'),
'#description' => $this->t('The number of digits to the right of the decimal.'),
'#weight' => 6,
];
return $elements;
}
/**
* {@inheritdoc}
*/
protected function numberFormat($number) {
return number_format($number, $this->getSetting('scale'), $this->getSetting('decimal_separator'), $this->getSetting('thousand_separator'));
}
}
Members
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.