You are here

public function GeneralNumberWithMinMaxFormatter::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/GeneralNumberWithMinMaxFormatter.php, line 77

Class

GeneralNumberWithMinMaxFormatter
Formats with a variety of notation styles and includes field min/max.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

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

  // Get settings.
  $fieldSettings = $this
    ->getFieldSettings();
  $min = $fieldSettings['min'];
  $max = $fieldSettings['max'];

  // Sanitize & validate.
  $disabled = FALSE;
  if (isset($min) === FALSE || isset($max) === FALSE) {
    $disabled = TRUE;
  }

  // Get sample number.
  if (isset($min) === FALSE && isset($max) === FALSE) {

    // No min or max. Just use a chosen number.
    $sample = 1234.123456789;
  }
  elseif (isset($min) === FALSE && isset($max) === TRUE) {

    // No min. Use max.
    $sample = $max;
  }
  elseif (isset($min) === TRUE && isset($max) === FALSE) {

    // No max. Use min.
    $sample = $min;
  }
  else {

    // Max and min, so use midpoint.
    $sample = ($max - $min) / 2 + $min;
  }

  // Summarize.
  $summary = [];
  if ($disabled === TRUE) {
    $summary[] = $this
      ->t('Disabled min/max formatting, field min/max need to be set.');
    return $summary;
  }

  // Formatting a number can introduced HTML. To preserve it during
  // presentation, call it formatted markup.
  $value = $this
    ->numberFormat($sample);
  $value = new FormattableMarkup($value, []);
  $summary[] = $this
    ->t('Sample: @value', [
    '@value' => $value,
  ]);
  return $summary;
}