You are here

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

File

src/Plugin/Field/FieldFormatter/GeneralLinkFormatter.php, line 568

Class

GeneralLinkFormatter
Formats a link 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();
  $linkIsInternalOnly = FALSE;
  if ($fieldSettings['link_type'] === LinkItemInterface::LINK_INTERNAL) {
    $linkIsInternalOnly = TRUE;
  }
  $linkTitleDisabled = FALSE;
  if ($fieldSettings['title'] === DRUPAL_DISABLED) {
    $linkTitleDisabled = TRUE;
  }

  // Get current settings.
  $titleStyle = $this
    ->getSetting('titleStyle');
  $showLink = $this
    ->getSetting('showLink');
  $openLinkIn = $this
    ->getSetting('openLinkIn');
  $noreferrer = $this
    ->getSetting('noreferrer');
  $noopener = $this
    ->getSetting('noopener');
  $nofollow = $this
    ->getSetting('nofollow');
  $linkTopic = $this
    ->getSetting('linkTopic');
  if ($linkIsInternalOnly === TRUE) {

    // For internal-only link fields, do not support setting noreferrer,
    // noopener, or nofollow.
    $noreferrer = FALSE;
    $noopener = FALSE;
    $nofollow = FALSE;
  }

  // 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 send other values back.
  // So check all <select> choices and use the default when a value is
  // empty or unknown.
  if ($linkTitleDisabled === TRUE) {
    $titleStyles = $this
      ->getTitleStylesFieldNoTitle();
  }
  else {
    $titleStyles = $this
      ->getTitleStyles();
  }
  if (empty($titleStyle) === TRUE || isset($titleStyles[$titleStyle]) === FALSE) {
    if ($linkTitleDisabled === TRUE) {
      $titleStyle = 'text_from_url';
    }
    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);
  $noreferrer = boolval($noreferrer);
  $noopener = boolval($noopener);
  $nofollow = boolval($nofollow);
  $this
    ->setSetting('showLink', $showLink);
  $this
    ->setSetting('noreferrer', $noreferrer);
  $this
    ->setSetting('noopener', $noopener);
  $this
    ->setSetting('nofollow', $nofollow);
  $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.
}