You are here

protected function GeneralNumberWithMinMaxFormatter::numberFormat in Formatter Suite 8

Returns a formatted number, including min, max, prefix, and suffix.

The current settings are used to get the format and fill it in.

The returned string is not automatically translated. Any prefix, suffix, or custom format entered by the site admin is used as-is, after security checks.

Parameters

mixed $number: The number to format.

Return value

string Returns the formatted number.

Overrides GeneralNumberFormatter::numberFormat

1 call to GeneralNumberWithMinMaxFormatter::numberFormat()
GeneralNumberWithMinMaxFormatter::settingsSummary in src/Plugin/Field/FieldFormatter/GeneralNumberWithMinMaxFormatter.php
Returns a short summary for the current formatter settings.

File

src/Plugin/Field/FieldFormatter/GeneralNumberWithMinMaxFormatter.php, line 293

Class

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

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

protected function numberFormat($number) {

  // Get settings.
  $fieldSettings = $this
    ->getFieldSettings();
  $min = $fieldSettings['min'];
  $max = $fieldSettings['max'];
  $format = $this
    ->getSetting('customFormat');
  $commonFormat = $this
    ->getSetting('commonFormat');
  $prefixes = $this
    ->getFieldPrefixes();
  $suffixes = $this
    ->getFieldSuffixes();
  $prefix = $this
    ->selectPrefix($number, $prefixes);
  $suffix = $this
    ->selectSuffix($number, $suffixes);

  // Sanitize and validate.
  $hasMin = isset($min) === TRUE;
  $hasMax = isset($max) === TRUE;

  // Format min, max, and number.
  $formattedNumber = parent::numberFormat($number);
  $formattedMin = $hasMin === TRUE ? parent::numberFormat($min) : '';
  $formattedMax = $hasMax === TRUE ? parent::numberFormat($max) : '';

  // Build the argument list for use in t() below with the chosen format.
  $args = [
    '@prefix' => $prefix,
    '@suffix' => $suffix,
    '@value' => $formattedNumber,
    '@min' => $formattedMin,
    '@max' => $formattedMax,
  ];

  // Format the result.
  //
  // Security: A custom format may have been entered by the administrator,
  // or we may be using a built-in common format. In either case, the
  // format may legitimately include HTML entities and minor HTML. It
  // should not include dangerous HTML.
  //
  // For the common formats, passing the format text to t() automatically
  // handles sanitizing any HTML that might be in the format. It also handles
  // possible automatic translation.
  //
  // For a custom format, we could pass it to t() as well, but Drupal's
  // style checking scripts object. The format text for t() is supposed to
  // be a literal so that Drupal's static code scanning can build up a list
  // of translatable text. In this case, however, we cannot provide literal
  // text since the text was entered by the site admin. All we can do is
  // bypass t() and the underlying TranslatableMarkup.
  switch ($commonFormat) {
    case 'N_slash_MAX':

      // Common format: N/MAX.
      if ($hasMax === FALSE) {

        // Fall thru to default.
        break;
      }
      return $this
        ->t('@prefix@value/@prefix@max@suffix', $args);
    case 'N_out_of_MAX':

      // Common format: N out of MAX.
      if ($hasMax === FALSE) {

        // Fall thru to default.
        break;
      }
      return $this
        ->t('@prefix@value out of @prefix@max@suffix', $args);
    case 'N_in_MIN_MAX':

      // Common format: N in [MIN:MAX].
      if ($hasMin === FALSE || $hasMax === FALSE) {

        // Fall thru to default.
        break;
      }
      return $this
        ->t('@prefix@value in [@prefix@min,@prefix@max]@suffix', $args);
    case 'N_element_MIN_MAX':

      // Common format: N E {MIN...MAX}.
      if ($hasMin === FALSE || $hasMax === FALSE) {

        // Fall thru to default.
        break;
      }
      return $this
        ->t('@prefix@value ∈ {@prefix@min...@prefix@max}@suffix', $args);
    case 'MIN_N_MAX':

      // Common format: MIN <= N <= MAX.
      if ($hasMin === FALSE && $hasMax === FALSE) {

        // Fall thru to default.
        break;
      }
      if ($hasMin === FALSE) {
        return $this
          ->t('@prefix@value &le; @prefix@max@suffix', $args);
      }
      if ($hasMax === FALSE) {
        return $this
          ->t('@prefix@min &le; @prefix@value@suffix', $args);
      }
      return $this
        ->t('@prefix@min &le; @prefix@value &le; @prefix@max@suffix', $args);
    default:
    case 'custom':

      // Custom format.
      if (empty($format) === TRUE) {

        // Fall thru to default.
        break;
      }

      // Escape the site admin-entered format string to make it safe.
      $safeFormat = Html::escape($format);

      // Since the admin-entered text is not a literal and could not have
      // been picked up by Drupal's static code scanning to build a
      // translation, it won't be translatable. Just format it as-is.
      return new FormattableMarkup($safeFormat, $args);
  }

  // Otherwise use the default format.
  return $this
    ->t('@prefix@value@suffix', $args);
}