You are here

protected function NumberWithBytesFormatter::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

2 calls to NumberWithBytesFormatter::sanitizeSettings()
NumberWithBytesFormatter::settingsSummary in src/Plugin/Field/FieldFormatter/NumberWithBytesFormatter.php
Returns a short summary for the current formatter settings.
NumberWithBytesFormatter::viewElements in src/Plugin/Field/FieldFormatter/NumberWithBytesFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/NumberWithBytesFormatter.php, line 199

Class

NumberWithBytesFormatter
Formats numbers with a byte suffix, like "bytes", "KB", or "MB".

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

protected function sanitizeSettings() {

  // Get settings.
  $kunit = $this
    ->getSetting('kunit');
  $fullWord = $this
    ->getSetting('fullWord');
  $decimalDigits = $this
    ->getSetting('decimalDigits');
  $defaults = $this
    ->defaultSettings();

  // Sanitize & validate.
  $kunits = $this
    ->getKiloUnits();
  if (empty($kunit) === TRUE || isset($kunits[$kunit]) === FALSE) {
    $kunit = $defaults['kunit'];
    $this
      ->setSetting('kunit', $kunit);
  }
  $fullWord = boolval($fullWord);
  $this
    ->setSetting('fullWord', $fullWord);
  $decimalDigits = intval($decimalDigits);
  if ($decimalDigits < 0) {
    $decimalDigits = 0;
  }
  elseif ($decimalDigits > 3) {
    $decimalDigits = 3;
  }
  $this
    ->setSetting('decimalDigits', $decimalDigits);
}