FractionItem.php in Fraction 8
File
src/Plugin/Field/FieldType/FractionItem.php
View source
<?php
namespace Drupal\fraction\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\NumericItemBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\fraction\Fraction;
class FractionItem extends NumericItemBase {
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'numerator' => [
'description' => 'Fraction numerator value',
'type' => 'int',
'size' => 'big',
'not null' => TRUE,
'default' => 0,
],
'denominator' => [
'description' => 'Fraction denominator value',
'type' => 'int',
'not null' => TRUE,
'default' => 1,
],
],
];
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$element['min']['#step'] = 'any';
$element['max']['#step'] = 'any';
return $element;
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$property_definitions['numerator'] = DataDefinition::create('integer')
->setLabel(t('Numerator value'));
$property_definitions['denominator'] = DataDefinition::create('integer')
->setLabel(t('Denominator value'));
$property_definitions['fraction'] = MapDataDefinition::create()
->setLabel(t('Fraction'))
->setDescription(t('A fraction object instance.'))
->setComputed(TRUE)
->setClass('\\Drupal\\fraction\\FractionProperty');
$property_definitions['value'] = MapDataDefinition::create()
->setLabel(t('Fraction Decimal'))
->setDescription(t('Fraction decimal value.'))
->setComputed(TRUE)
->setClass('\\Drupal\\fraction\\FractionDecimalProperty');
return $property_definitions;
}
public function isEmpty() {
$numerator = $this
->get('numerator')
->getValue();
$denominator = $this
->get('denominator')
->getValue();
return (string) $numerator !== '0' && empty($numerator) || (string) $denominator !== '0' && empty($denominator);
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$divisor = pow(10, rand(0, 9));
$number = mt_rand(1, 20 * $divisor) / $divisor;
$fraction = Fraction::createFromDecimal($number);
return [
'numerator' => $fraction
->getNumerator(),
'denominator' => $fraction
->getDenominator(),
];
}
}
Classes
Name |
Description |
FractionItem |
Plugin implementation of the 'fraction' field type. |