UcPriceItem.php in Ubercart 8.4
Namespace
Drupal\uc_product\Plugin\Field\FieldTypeFile
uc_product/src/Plugin/Field/FieldType/UcPriceItem.phpView source
<?php
namespace Drupal\uc_product\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\NumericItemBase;
use Drupal\Core\TypedData\DataDefinition;
/**
* Defines the Ubercart price field type.
*
* @FieldType(
* id = "uc_price",
* label = @Translation("Price"),
* description = @Translation("This field stores a price in the database."),
* default_widget = "uc_price",
* default_formatter = "uc_price"
* )
*/
class UcPriceItem extends NumericItemBase {
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('float')
->setLabel(t('Amount'));
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'numeric',
'precision' => 16,
'scale' => 5,
'not null' => FALSE,
],
],
];
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$settings = $field_definition
->getSettings();
$precision = 6;
$scale = 2;
$max = is_numeric($settings['max']) ?: pow(10, $precision - $scale) - 1;
$min = is_numeric($settings['min']) ?: -pow(10, $precision - $scale) + 1;
// @see "Example #1 Calculate a random floating-point number" in
// http://php.net/manual/en/function.mt-getrandmax.php
$random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min);
$values['value'] = self::truncateDecimal($random_decimal, $scale);
return $values;
}
}
Classes
Name | Description |
---|---|
UcPriceItem | Defines the Ubercart price field type. |