You are here

public function GeneralNumberWithBarIndicatorFormatter::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 GeneralNumberFormatter::settingsSummary

File

src/Plugin/Field/FieldFormatter/GeneralNumberWithBarIndicatorFormatter.php, line 73

Class

GeneralNumberWithBarIndicatorFormatter
Presents an integer as a labeled horizontal bar of varying length.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

public function settingsSummary() {
  $this
    ->sanitizeSettings();

  // Get current settings.
  $barLength = $this
    ->getSetting('barLength');
  $barWidth = $this
    ->getSetting('barWidth');
  $valueLocation = $this
    ->getSetting('valueLocation');
  $fieldSettings = $this
    ->getFieldSettings();
  $min = $fieldSettings['min'];
  $max = $fieldSettings['max'];

  // Sanitize & validate.
  $valueLocations = $this
    ->getValueLocations();
  if (empty($valueLocation) === TRUE || isset($valueLocations[$valueLocation]) === FALSE) {
    $valueLocation = 'none';
  }
  $disabledByMinMax = FALSE;
  $disabledByLength = FALSE;
  if (isset($min) === FALSE || isset($max) === FALSE) {
    $disabledByMinMax = TRUE;
  }
  elseif ($barLength <= 0 || $barWidth <= 0) {
    $disabledByLength = TRUE;
  }
  else {
    $barLength .= 'px';
    $barWidth .= 'px';
  }

  // Summarize.
  $summary = [];
  if ($disabledByMinMax === TRUE) {
    $summary[] = $this
      ->t('Disabled color bar, field min/max need to be set.');
    $summary = array_merge($summary, parent::settingsSummary());
  }
  elseif ($disabledByLength === TRUE) {
    $summary[] = $this
      ->t('Disabled color bar, bar size needs to be set.');
    $summary = array_merge($summary, parent::settingsSummary());
  }
  else {
    $summary[] = $this
      ->t('Colored bar @barLength long, @barWidth wide.', [
      '@barLength' => $barLength,
      '@barWidth' => $barWidth,
    ]);
    if ($valueLocation === 'none') {
      $summary[] = $this
        ->t('No value shown.');
    }
    else {
      $summary[] = $this
        ->t('Value @location.', [
        '@location' => $valueLocations[$valueLocation],
      ]);
      $summary = array_merge($summary, parent::settingsSummary());
    }
  }
  return $summary;
}