class NumericFormatterBase
Same name in other branches
- 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase
- 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase
- 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase
Parent plugin for decimal and integer formatters.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface
- class \Drupal\Core\Field\FormatterBase extends \Drupal\Core\Field\PluginSettingsBase implements \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase extends \Drupal\Core\Field\FormatterBase
- class \Drupal\Core\Field\FormatterBase extends \Drupal\Core\Field\PluginSettingsBase implements \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
Expanded class hierarchy of NumericFormatterBase
File
-
core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldFormatter/ NumericFormatterBase.php, line 12
Namespace
Drupal\Core\Field\Plugin\Field\FieldFormatterView source
abstract class NumericFormatterBase extends FormatterBase {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $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' => 0,
];
$elements['prefix_suffix'] = [
'#type' => 'checkbox',
'#title' => $this->t('Display prefix and suffix'),
'#default_value' => $this->getSetting('prefix_suffix'),
'#weight' => 10,
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = $this->numberFormat(1234.123456789);
if ($this->getSetting('prefix_suffix')) {
$summary[] = $this->t('Display with prefix and suffix.');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$settings = $this->getFieldSettings();
foreach ($items as $delta => $item) {
$output = $this->numberFormat($item->value);
// Account for prefix and suffix.
if ($this->getSetting('prefix_suffix')) {
$prefixes = isset($settings['prefix']) ? array_map([
'Drupal\\Core\\Field\\FieldFilteredMarkup',
'create',
], explode('|', $settings['prefix'])) : [
'',
];
$suffixes = isset($settings['suffix']) ? array_map([
'Drupal\\Core\\Field\\FieldFilteredMarkup',
'create',
], explode('|', $settings['suffix'])) : [
'',
];
$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];
$output = $prefix . $output . $suffix;
}
// 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 (isset($item->_attributes) && $item->value != $output) {
$item->_attributes += [
'content' => $item->value,
];
}
$elements[$delta] = [
'#markup' => $output,
];
}
return $elements;
}
/**
* Formats a number.
*
* @param mixed $number
* The numeric value.
*
* @return string
* The formatted number.
*/
protected abstract function numberFormat($number);
}
Members
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.