NumberFormat.php in Tamper 8
File
src/Plugin/Tamper/NumberFormat.php
View source
<?php
namespace Drupal\tamper\Plugin\Tamper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tamper\Exception\TamperException;
use Drupal\tamper\TamperableItemInterface;
use Drupal\tamper\TamperBase;
class NumberFormat extends TamperBase {
const SETTING_DECIMALS = 'decimals';
const SETTING_DEC_POINT = 'dec_point';
const SETTING_THOUSANDS_SEP = 'thousands_sep';
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config[self::SETTING_DECIMALS] = 0;
$config[self::SETTING_DEC_POINT] = '.';
$config[self::SETTING_THOUSANDS_SEP] = ',';
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[self::SETTING_DECIMALS] = [
'#type' => 'number',
'#title' => $this
->t('Decimals'),
'#default_value' => $this
->getSetting(self::SETTING_DECIMALS),
'#description' => $this
->t('The number of decimal places.'),
];
$form[self::SETTING_DEC_POINT] = [
'#type' => 'textfield',
'#title' => $this
->t('Decimal point'),
'#default_value' => $this
->getSetting(self::SETTING_DEC_POINT),
'#description' => $this
->t('The character to use as the decimal point.'),
];
$form[self::SETTING_THOUSANDS_SEP] = [
'#type' => 'textfield',
'#title' => $this
->t('Thousands separator'),
'#default_value' => $this
->getSetting(self::SETTING_THOUSANDS_SEP),
'#description' => $this
->t('The character to use as the thousands separator.'),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this
->setConfiguration([
self::SETTING_DECIMALS => $form_state
->getValue(self::SETTING_DECIMALS),
self::SETTING_DEC_POINT => $form_state
->getValue(self::SETTING_DEC_POINT),
self::SETTING_THOUSANDS_SEP => $form_state
->getValue(self::SETTING_THOUSANDS_SEP),
]);
}
public function tamper($data, TamperableItemInterface $item = NULL) {
if (!is_numeric($data)) {
throw new TamperException('Input should be numeric.');
}
$decimals = $this
->getSetting(self::SETTING_DECIMALS);
$dec_point = $this
->getSetting(self::SETTING_DEC_POINT);
$thousands_sep = $this
->getSetting(self::SETTING_THOUSANDS_SEP);
return number_format($data, $decimals, $dec_point, $thousands_sep);
}
}