You are here

FractionFormatterBase.php in Fraction 8

Same filename and directory in other branches
  1. 2.x src/Plugin/Field/FieldFormatter/FractionFormatterBase.php

File

src/Plugin/Field/FieldFormatter/FractionFormatterBase.php
View source
<?php

namespace Drupal\fraction\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Base class for fraction formatters.
 */
abstract class FractionFormatterBase extends FormatterBase implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'prefix_suffix' => FALSE,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = parent::settingsForm($form, $form_state);
    $elements['prefix_suffix'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display prefix and suffix'),
      '#default_value' => $this
        ->getSetting('prefix_suffix'),
      '#weight' => 10,
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
    if ($this
      ->getSetting('prefix_suffix')) {
      $summary[] = $this
        ->t('Display with prefix and suffix.');
    }
    return $summary;
  }

  /**
   * Account for extra output such as prefixes and suffixes.
   *
   * @param \Drupal\Core\Field\FieldItemInterface $item
   *   The field item to evaluate.
   * @param string $output
   *   The output of the field.
   *
   * @return string
   *   The output with all relevant additions.
   */
  protected function viewOutput(FieldItemInterface $item, $output = '') {
    $field_settings = $this
      ->getFieldSettings();

    // Account for prefix and suffix.
    if ($this
      ->getSetting('prefix_suffix')) {
      $prefixes = isset($field_settings['prefix']) ? array_map([
        'Drupal\\Core\\Field\\FieldFilteredMarkup',
        'create',
      ], explode('|', $field_settings['prefix'])) : [
        '',
      ];
      $suffixes = isset($field_settings['suffix']) ? array_map([
        'Drupal\\Core\\Field\\FieldFilteredMarkup',
        'create',
      ], explode('|', $field_settings['suffix'])) : [
        '',
      ];
      $prefix = count($prefixes) > 1 ? $this
        ->formatPlural($item->value, $prefixes[0], $prefixes[1]) : $prefixes[0];
      $suffix = count($suffixes) > 1 ? $this
        ->formatPlural($item->value, $suffixes[0], $suffixes[1]) : $suffixes[0];
      $output = $prefix . $output . $suffix;
    }
    return $output;
  }

}

Classes

Namesort descending Description
FractionFormatterBase Base class for fraction formatters.