You are here

public function GeneralImageFormatter::settingsSummary in Formatter Suite 8

Returns a short summary for the current formatter settings.

If an empty result is returned, a UI can still be provided to display a settings form in case the formatter has configurable settings.

Return value

string[] A short summary of the formatter settings.

Overrides ImageFormatter::settingsSummary

File

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

Class

GeneralImageFormatter
Formats an image.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

public function settingsSummary() {

  // Sanitize current settings.
  $this
    ->sanitizeSettings();

  // Get the image styles. Unset the empty 'No defined styles' option.
  // Note also that the current style choice could be invalid if the
  // indicated style has gone away.
  $imageStyles = image_style_options(FALSE);
  unset($imageStyles['']);
  $styleChoice = $this
    ->getSetting('image_style');
  $currentStyle = NULL;
  if (isset($imageStyles[$styleChoice]) === TRUE) {
    $currentStyle = $imageStyles[$styleChoice];
  }

  // Determine if the field has a title.
  $fieldDefinition = $this->fieldDefinition;
  $fieldSettings = $fieldDefinition
    ->getSettings();
  $hasTitle = FALSE;
  if (isset($fieldSettings['title_field']) === TRUE && boolval($fieldSettings['title_field']) === TRUE) {
    $hasTitle = TRUE;
  }

  // Summarize.
  $summary = [];
  if ($currentStyle === NULL) {
    $summary[] = $this
      ->t('Original image');
  }
  else {
    $summary[] = $this
      ->t('Image style "@style"', [
      '@style' => $currentStyle,
    ]);
  }
  $linkChoice = $this
    ->getSetting('image_link');
  $linkTypes = self::getLinkTypes();
  if (isset($linkTypes[$linkChoice]) === TRUE) {

    // The image is linked.
    $summary[] = $linkTypes[$linkChoice];
    $openInChoice = $this
      ->getSetting('openLinkIn');
    $openInValues = self::getOpenLinkInValues();
    if ($openInChoice !== 'download') {
      $summary[] = $openInValues[$openInChoice];
    }
    elseif ($linkChoice !== 'file') {

      // Ignore 'download' choice if the link type is not to a file.
      // Revert to '_blank'.
      $summary[] = $openInValues['_blank'];
    }
    else {
      $summary[] = $openInValues[$openInChoice];
    }
  }
  $captionChoice = $this
    ->getSetting('captionLocation');
  $captionLocations = self::getCaptionLocations();
  if ($captionChoice !== 'none' && isset($captionLocations[$captionChoice]) === TRUE) {
    $includes = [];
    if ($hasTitle === TRUE && $this
      ->getSetting('captionIncludeTitle') === TRUE) {
      $includes[] = (string) $this
        ->t('title');
    }
    if ($this
      ->getSetting('captionIncludeFilename') === TRUE) {
      $includes[] = (string) $this
        ->t('file name');
    }
    if ($this
      ->getSetting('captionIncludeSize') === TRUE) {
      $includes[] = (string) $this
        ->t('size');
    }
    if ($this
      ->getSetting('captionIncludeDimensions') === TRUE) {
      $includes[] = (string) $this
        ->t('dimensions');
    }
    if ($this
      ->getSetting('captionIncludeMime') === TRUE) {
      $includes[] = (string) $this
        ->t('MIME type');
    }
    if (empty($includes) === FALSE) {
      switch ($captionChoice) {
        case 'above':
          $summary[] = $this
            ->t('Caption above: @list', [
            '@list' => implode(', ', $includes),
          ]);
          break;
        default:
        case 'below':
          $summary[] = $this
            ->t('Caption below: @list', [
            '@list' => implode(', ', $includes),
          ]);
          break;
      }
    }
  }
  return $summary;
}