You are here

FractionDecimalWidget.php in Fraction 8

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

File

src/Plugin/Field/FieldWidget/FractionDecimalWidget.php
View source
<?php

namespace Drupal\fraction\Plugin\Field\FieldWidget;

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

/**
 * Plugin implementation of the 'fraction_decimal' widget.
 *
 * @FieldWidget(
 *   id = "fraction_decimal",
 *   label = @Translation("Decimal"),
 *   field_types = {
 *     "fraction"
 *   }
 * )
 */
class FractionDecimalWidget extends WidgetBase {

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

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

    // Decimal precision.
    $elements['precision'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Decimal precision'),
      '#description' => $this
        ->t('Specify the number of digits after the decimal place to display when converting the fraction to a decimal. When "Auto precision" is enabled, this value essentially becomes a minimum fallback precision.'),
      '#default_value' => $this
        ->getSetting('precision'),
      '#required' => TRUE,
      '#weight' => 0,
    ];

    // Auto precision.
    $elements['auto_precision'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Auto precision'),
      '#description' => $this
        ->t('Automatically determine the maximum precision if the fraction has a base-10 denominator. For example, 1/100 would have a precision of 2, 1/1000 would have a precision of 3, etc.'),
      '#default_value' => $this
        ->getSetting('auto_precision'),
      '#weight' => 1,
    ];
    return $elements;
  }

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

    // Summarize the precision setting.
    $precision = $this
      ->getSetting('precision');
    $auto_precision = !empty($this
      ->getSetting('auto_precision')) ? 'On' : 'Off';
    $summary[] = $this
      ->t('Precision: @precision, Auto-precision: @auto_precision', [
      '@precision' => $precision,
      '@auto_precision' => $auto_precision,
    ]);
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

    // Add a 'decimal' textfield for capturing the decimal value.
    // The default value is converted to a decimal with the specified precision.
    $precision = $this
      ->getSetting('precision');
    $auto_precision = !empty($this
      ->getSetting('auto_precision')) ? TRUE : FALSE;
    $element['decimal'] = $element + [
      '#type' => 'fraction_decimal',
      '#default_value' => $items
        ->isEmpty() ? '' : $items[$delta]->fraction
        ->toDecimal($precision, $auto_precision),
    ];
    $field_settings = $this
      ->getFieldSettings();

    // Set minimum and maximum.
    if (isset($field_settings['min']) && is_numeric($field_settings['min'])) {
      $element['#min'] = $field_settings['min'];
    }
    if (isset($field_settings['max']) && is_numeric($field_settings['max'])) {
      $element['#max'] = $field_settings['max'];
    }

    // Add prefix and suffix.
    if ($field_settings['prefix']) {
      $prefixes = explode('|', $field_settings['prefix']);
      $element['decimal']['#field_prefix'] = FieldFilteredMarkup::create(array_pop($prefixes));
    }
    if ($field_settings['suffix']) {
      $suffixes = explode('|', $field_settings['suffix']);
      $element['decimal']['#field_suffix'] = FieldFilteredMarkup::create(array_pop($suffixes));
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    foreach ($values as &$value) {

      // Remove the fraction form element wrapper if it exists to make the
      // FractionItem::setValue method more consistent.
      $value = $value['decimal'] ?? $value;
    }
    return $values;
  }

}

Classes

Namesort descending Description
FractionDecimalWidget Plugin implementation of the 'fraction_decimal' widget.