You are here

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

Overrides GeneralNumberFormatter::sanitizeSettings

1 call to GeneralNumberWithBarIndicatorFormatter::sanitizeSettings()
GeneralNumberWithBarIndicatorFormatter::settingsSummary in src/Plugin/Field/FieldFormatter/GeneralNumberWithBarIndicatorFormatter.php
Returns a short summary for the current formatter settings.

File

src/Plugin/Field/FieldFormatter/GeneralNumberWithBarIndicatorFormatter.php, line 281

Class

GeneralNumberWithBarIndicatorFormatter
Presents an integer as a labeled horizontal bar of varying length.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

protected function sanitizeSettings() {

  // Get settings.
  $barLength = $this
    ->getSetting('barLength');
  $barWidth = $this
    ->getSetting('barWidth');
  $barColor = $this
    ->getSetting('barColor');
  $backgroundColor = $this
    ->getSetting('backgroundColor');
  $valueLocation = $this
    ->getSetting('valueLocation');
  $defaults = $this
    ->defaultSettings();

  // Sanitize & validate.
  parent::sanitizeSettings();
  $valueLocations = $this
    ->getValueLocations();
  if (empty($valueLocation) === TRUE || isset($valueLocations[$valueLocation]) === FALSE) {
    $valueLocation = $defaults['valueLocation'];
  }

  // Security: The bar length and weight have been entered by an
  // administrator. They both should be simple integers and should
  // not contain HTML or HTML entities.
  //
  // Parsing the values as integers ignores anything extra that
  // might be included in the value, such as spurious HTML.
  if (empty($barLength) === TRUE) {
    $barLength = intval($defaults['barLength']);
  }
  else {
    $barLength = intval($barLength);
    if ($barLength < 0) {
      $barLength = intval($defaults['barLength']);
    }
  }
  if (empty($barWidth) === TRUE) {
    $barWidth = intval($defaults['barWidth']);
  }
  else {
    $barWidth = intval($barWidth);
    if ($barWidth < 0) {
      $barWidth = intval($defaults['barWidth']);
    }
  }

  // Security: The bar and background colors have been entered by an
  // administrator. They both should be valid CSS colors of the form
  // #HEX.
  //
  // If a color doesn't start with '#', then it is illegal and we
  // revert to a default. Otherwise the color is escaped. The bar
  // color will be used to create an image, which will parse the
  // color. The background color will be included as an HTML attribute.
  if (empty($barColor) === TRUE || $barColor[0] !== '#') {
    $barColor = $defaults['barColor'];
  }
  else {
    $barColor = Html::escape($barColor);
  }
  if (empty($backgroundColor) === TRUE || $backgroundColor[0] !== '#') {
    $backgroundColor = $defaults['backgroundColor'];
  }
  else {
    $backgroundColor = Html::escape($backgroundColor);
  }
  $this
    ->setSetting('barLength', $barLength);
  $this
    ->setSetting('barWidth', $barWidth);
  $this
    ->setSetting('barColor', $barColor);
  $this
    ->setSetting('backgroundColor', $backgroundColor);
  $this
    ->setSetting('valueLocation', $valueLocation);
}