You are here

FractionFormatter.php in Fraction 8

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

File

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

namespace Drupal\fraction\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'fraction' formatter.
 *
 * @FieldFormatter(
 *   id = "fraction",
 *   label = @Translation("Fraction"),
 *   field_types = {
 *     "fraction"
 *   }
 * )
 */
class FractionFormatter extends FractionFormatterBase {

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

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {

    // Numerator and denominator separator.
    $elements['separator'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Separator'),
      '#description' => $this
        ->t('Specify the separator to display between the numerator and denominator.'),
      '#default_value' => $this
        ->getSetting('separator'),
      '#required' => TRUE,
      '#weight' => 0,
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();

    // Summarize the separator setting.
    $separator = $this
      ->getSetting('separator');
    $summary[] = $this
      ->t('Separator: @separator', [
      '@separator' => $separator,
    ]);
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];

    // Load the separator setting.
    $separator = $this
      ->getSetting('separator');

    // Iterate through the items.
    foreach ($items as $delta => $item) {
      $output = $item->fraction
        ->toString($separator);
      $elements[$delta] = [
        '#markup' => $this
          ->viewOutput($item, $output),
      ];
    }
    return $elements;
  }

}

Classes

Namesort descending Description
FractionFormatter Plugin implementation of the 'fraction' formatter.