You are here

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

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

File

src/Plugin/Field/FieldFormatter/GeneralFileLinkFormatter.php, line 610

Class

GeneralFileLinkFormatter
Formats a file field as one or more links.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

protected function sanitizeSettings() {

  // Get field settings.
  $fieldSettings = $this
    ->getFieldSettings();
  $isMultiple = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->isMultiple();
  $fileDescriptionDisabled = FALSE;
  if ($fieldSettings['description_field'] === FALSE) {
    $fileDescriptionDisabled = TRUE;
  }

  // Get current settings.
  $titleStyle = $this
    ->getSetting('titleStyle');
  $showIcon = $this
    ->getSetting('showIcon');
  $showSize = $this
    ->getSetting('showSize');
  $showLink = $this
    ->getSetting('showLink');
  $openLinkIn = $this
    ->getSetting('openLinkIn');
  $linkTopic = $this
    ->getSetting('linkTopic');

  // 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 ($fileDescriptionDisabled === TRUE) {
    $titleStyles = $this
      ->getTitleStylesFieldNoDescription();
  }
  else {
    $titleStyles = $this
      ->getTitleStyles();
  }
  if (empty($titleStyle) === TRUE || isset($titleStyles[$titleStyle]) === FALSE) {
    if ($fileDescriptionDisabled === TRUE) {
      $titleStyle = 'text_from_filename';
    }
    else {
      $titleStyle = $defaults['titleStyle'];
    }
    $this
      ->setSetting('titleStyle', $titleStyle);
  }
  $openLinkInValues = $this
    ->getOpenLinkInValues();
  if (empty($openLinkIn) === TRUE || isset($openLinkInValues[$openLinkIn]) === FALSE) {
    $openLinkIn = $defaults['openLinkIn'];
    $this
      ->setSetting('openLinkIn', $openLinkIn);
  }
  $linkTopicValues = $this
    ->getLinkTopicValues();
  if (empty($linkTopic) === TRUE || isset($linkTopicValues[$linkTopic]) === FALSE) {
    $linkTopic = $defaults['linkTopic'];
    $this
      ->setSetting('linkTopic', $linkTopic);
  }

  // Insure boolean values are boolean.
  $showLink = boolval($showLink);
  $showIcon = boolval($showIcon);
  $showSize = boolval($showSize);
  $this
    ->setSetting('showLink', $showLink);
  $this
    ->setSetting('showIcon', $showIcon);
  $this
    ->setSetting('showSize', $showSize);
  $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);
    }
  }

  // Classes and custom title text are not sanitized or validated.
  // They will be added to the link, with appropriate Xss filtering.
}