protected function GeneralNumberFormatter::sanitizeSettings in Formatter Suite 8
Sanitize settings to insure that they are safe and valid.
@internal Drupal's class hierarchy for plugins and their settings does not include a 'validate' function, like that for other classes with forms. Validation must therefore occur on use, rather than on form submission. @endinternal
6 calls to GeneralNumberFormatter::sanitizeSettings()
- GeneralNumberFormatter::numberFormat in src/
Plugin/ Field/ FieldFormatter/ GeneralNumberFormatter.php - Format a number using the current settings.
- GeneralNumberFormatter::settingsForm in src/
Plugin/ Field/ FieldFormatter/ GeneralNumberFormatter.php - Returns a form to configure settings for the formatter.
- GeneralNumberFormatter::settingsSummary in src/
Plugin/ Field/ FieldFormatter/ GeneralNumberFormatter.php - Returns a short summary for the current formatter settings.
- GeneralNumberFormatter::viewElements in src/
Plugin/ Field/ FieldFormatter/ GeneralNumberFormatter.php - Builds a renderable array for a field value.
- GeneralNumberWithBarIndicatorFormatter::sanitizeSettings in src/
Plugin/ Field/ FieldFormatter/ GeneralNumberWithBarIndicatorFormatter.php - Sanitize settings to insure that they are safe and valid.
2 methods override GeneralNumberFormatter::sanitizeSettings()
- GeneralNumberWithBarIndicatorFormatter::sanitizeSettings in src/
Plugin/ Field/ FieldFormatter/ GeneralNumberWithBarIndicatorFormatter.php - Sanitize settings to insure that they are safe and valid.
- GeneralNumberWithMinMaxFormatter::sanitizeSettings in src/
Plugin/ Field/ FieldFormatter/ GeneralNumberWithMinMaxFormatter.php - Sanitize settings to insure that they are safe and valid.
File
- src/
Plugin/ Field/ FieldFormatter/ GeneralNumberFormatter.php, line 677
Class
- GeneralNumberFormatter
- Format a number field with a variety of notation styles and parameters.
Namespace
Drupal\formatter_suite\Plugin\Field\FieldFormatterCode
protected function sanitizeSettings() {
// Get current settings.
$notationStyle = $this
->getSetting('notationStyle');
$exponentStyle = $this
->getSetting('exponentStyle');
$numberBase = $this
->getSetting('numberBase');
$decimalDigits = $this
->getSetting('decimalDigits');
$decimalSeparator = $this
->getSetting('decimalSeparator');
$useThousands = $this
->getSetting('useThousands');
$thousandsSeparator = $this
->getSetting('thousandsSeparator');
$positiveStyle = $this
->getSetting('positiveStyle');
$negativeStyle = $this
->getSetting('negativeStyle');
$useZeroPadding = $this
->getSetting('useZeroPadding');
$paddingWidth = $this
->getSetting('paddingWidth');
$usePrefixAndSuffix = $this
->getSetting('usePrefixAndSuffix');
$isMultiple = $this->fieldDefinition
->getFieldStorageDefinition()
->isMultiple();
// Get the field type.
$fieldType = $this->fieldDefinition
->getType();
if ($fieldType === 'decimal') {
$fieldSettings = $this
->getFieldSettings();
$fieldPrecision = $fieldSettings['precision'];
$fieldScale = $fieldSettings['scale'];
}
else {
$fieldPrecision = 0;
$fieldScale = 0;
}
// Get setting defaults.
$defaults = $this
->defaultSettings();
// Sanitize & validate.
//
// While <select> inputs constrain choices to those we define in the
// form, it is possible to hack a form response and send other values
// back. So check all <select> choices and use the default when a
// value is empty or unknown.
$notationStyles = $this
->getNotationStyles();
if (empty($notationStyle) === TRUE || isset($notationStyles[$notationStyle]) === FALSE) {
$notationStyle = $defaults['notationStyle'];
}
$exponentStyles = $this
->getExponentStyles();
if (empty($exponentStyle) === TRUE || isset($exponentStyles[$exponentStyle]) === FALSE) {
$exponentStyle = $defaults['exponentStyle'];
}
$negativeStyles = $this
->getNegativeStyles();
if (empty($negativeStyle) === TRUE || isset($negativeStyles[$negativeStyle]) === FALSE) {
$negativeStyle = $defaults['negativeStyle'];
}
$positiveStyles = $this
->getPositiveStyles();
if (empty($positiveStyle) === TRUE || isset($positiveStyles[$positiveStyle]) === FALSE) {
$positiveStyle = $defaults['positiveStyle'];
}
$thousandsSeparators = $this
->getThousandsSeparators();
if (empty($thousandsSeparator) === TRUE || isset($thousandsSeparators[$thousandsSeparator]) === FALSE) {
$thousandsSeparator = $defaults['thousandsSeparator'];
}
$decimalSeparators = $this
->getDecimalSeparators();
if (empty($decimalSeparator) === TRUE || isset($decimalSeparators[$decimalSeparator]) === FALSE) {
$decimalSeparator = $defaults['decimalSeparator'];
}
// Insure that boolean values are boolean.
$useZeroPadding = boolval($useZeroPadding);
$useThousands = boolval($useThousands);
$usePrefixAndSuffix = boolval($usePrefixAndSuffix);
// Insure that integer values are integers.
//
// Security: The number of decimal digits and the padding width are
// both entered by the administrator. Both should be simple integers
// and should not include HTML or HTML entities.
//
// Parsing these as integers ignores any additional text that might
// be present, such as HTML or HTML entities.
if (empty($decimalDigits) === TRUE) {
// If the field type is "decimal", default to the decimal field's scale.
//
// Otherwise use the setting default.
if ($fieldType === 'decimal') {
$decimalDigits = $fieldScale;
}
elseif ($fieldType === 'integer') {
$decimalDigits = 0;
}
else {
$decimalDigits = 2;
}
}
else {
$decimalDigits = intval($decimalDigits);
}
if (empty($numberBase) === TRUE) {
$numberBase = $defaults['numberBase'];
}
else {
$numberBase = intval($numberBase);
if ($numberBase < 2) {
$numberBase = 2;
}
elseif ($numberBase > 36) {
$numberBase = 36;
}
}
if (empty($paddingWidth) === TRUE) {
// If the field type is "decimal", default to the decimal field's
// precision, plus 1 for the decimal.
//
// Otherwise use the setting default.
if ($fieldType === 'decimal') {
$paddingWidth = (int) $fieldPrecision + 1;
}
else {
$paddingWidth = 0;
}
}
else {
$paddingWidth = intval($paddingWidth);
}
// Set settings.
$this
->setSetting('notationStyle', $notationStyle);
$this
->setSetting('exponentStyle', $exponentStyle);
$this
->setSetting('numberBase', $numberBase);
$this
->setSetting('decimalDigits', $decimalDigits);
$this
->setSetting('decimalSeparator', $decimalSeparator);
$this
->setSetting('useThousands', $useThousands);
$this
->setSetting('thousandsSeparator', $thousandsSeparator);
$this
->setSetting('positiveStyle', $positiveStyle);
$this
->setSetting('negativeStyle', $negativeStyle);
$this
->setSetting('useZeroPadding', $useZeroPadding);
$this
->setSetting('paddingWidth', $paddingWidth);
$this
->setSetting('usePrefixAndSuffix', $usePrefixAndSuffix);
$listStyle = $this
->getSetting('listStyle');
$listStyles = $this
->getListStyles();
if ($isMultiple === TRUE) {
if (empty($listStyle) === TRUE || isset($listStyles[$listStyle]) === FALSE) {
$listStyle = $defaults['listStyle'];
$this
->setSetting('listStyle', $listStyle);
}
}
}