You are here

protected function GeneralImageFormatter::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 GeneralImageFormatter::sanitizeSettings()
GeneralImageFormatter::settingsSummary in src/Plugin/Field/FieldFormatter/GeneralImageFormatter.php
Returns a short summary for the current formatter settings.
GeneralImageFormatter::viewElements in src/Plugin/Field/FieldFormatter/GeneralImageFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/GeneralImageFormatter.php, line 608

Class

GeneralImageFormatter
Formats an image.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

protected function sanitizeSettings() {

  // Get current settings.
  //
  // Nothing to sanitize for the parent class's 'image_style'.
  // The style choice is checked on every use because styles come and go.
  $imageLink = $this
    ->getSetting('image_link');
  $openLinkIn = $this
    ->getSetting('openLinkIn');
  $captionLocation = $this
    ->getSetting('captionLocation');
  $captionIncludeTitle = $this
    ->getSetting('captionIncludeTitle');
  $captionIncludeFilename = $this
    ->getSetting('captionIncludeFilename');
  $captionIncludeSize = $this
    ->getSetting('captionIncludeSize');
  $captionIncludeDimensions = $this
    ->getSetting('captionIncludeDimensions');
  $captionIncludeMime = $this
    ->getSetting('captionIncludeMime');

  // 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.
  if (isset(self::getLinkTypes()[$imageLink]) === FALSE) {
    $this
      ->setSetting('image_link', $defaults['image_link']);
  }
  if (isset($this
    ->getOpenLinkInValues()[$openLinkIn]) === FALSE) {
    $this
      ->setSetting('openLinkIn', $defaults['openLinkIn']);
  }
  if (isset($this
    ->getCaptionLocations()[$captionLocation]) === FALSE) {
    $this
      ->setSetting('captionLocation', $defaults['captionLocation']);
  }

  // Insure boolean values are boolean.
  $this
    ->setSetting('captionIncludeTitle', boolval($captionIncludeTitle));
  $this
    ->setSetting('captionIncludeFilename', boolval($captionIncludeFilename));
  $this
    ->setSetting('captionIncludeSize', boolval($captionIncludeSize));
  $this
    ->setSetting('captionIncludeDimensions', boolval($captionIncludeDimensions));
  $this
    ->setSetting('captionIncludeMime', boolval($captionIncludeMime));

  // Classes are not sanitized or validated. They will be added to the link.
}