You are here

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

File

src/Plugin/Field/FieldFormatter/GeneralUserReferenceFormatter.php, line 635

Class

GeneralUserReferenceFormatter
Formats a user entity reference as one or more links.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

protected function sanitizeSettings() {

  // Get current settings.
  $userReferenceStyle = $this
    ->getSetting('userReferenceStyle');
  $showLink = $this
    ->getSetting('showLink');
  $openLinkIn = $this
    ->getSetting('openLinkIn');
  $linkTopic = $this
    ->getSetting('linkTopic');
  $isMultiple = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->isMultiple();

  // Get setting defaults.
  $defaults = self::defaultSettings();

  // Sanitize & validate.
  //
  // While <select> inputs constrain choices to those we define in the
  // form, it is possible to hack a form response send other values back.
  // So check all <select> choices and use the default when a value is
  // empty or unknown.
  $userReferenceStyles = $this
    ->getUserReferenceStyles();
  if (empty($userReferenceStyle) === TRUE || isset($userReferenceStyles[$userReferenceStyle]) === FALSE) {
    $userReferenceStyle = $defaults['userReferenceStyle'];
    $this
      ->setSetting('userReferenceStyle', $userReferenceStyle);
  }
  $openLinkInValues = self::getOpenLinkInValues();
  if (empty($openLinkIn) === TRUE || isset($openLinkInValues[$openLinkIn]) === FALSE) {
    $openLinkIn = $defaults['openLinkIn'];
    $this
      ->setSetting('openLinkIn', $openLinkIn);
  }
  $linkTopicValues = self::getLinkTopicValues();
  if (empty($linkTopic) === TRUE || isset($linkTopicValues[$linkTopic]) === FALSE) {
    $linkTopic = $defaults['linkTopic'];
    $this
      ->setSetting('linkTopic', $linkTopic);
  }

  // Insure boolean values are boolean.
  $showLink = boolval($showLink);
  $this
    ->setSetting('showLink', $showLink);
  $listStyle = $this
    ->getSetting('listStyle');
  $listStyles = self::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.
}