bat-8.x-1.x-dev/modules/bat_options/src/Plugin/Field/FieldType/BatTypeOptions.php
modules/bat_options/src/Plugin/Field/FieldType/BatTypeOptions.php
<?php
namespace Drupal\bat_options\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataReferenceTargetDefinition;
/**
* Description message.
*
* @FieldType(
* id = "bat_options",
* label = @Translation("BAT Type Options"),
* description = @Translation("BAT Type Options."),
* default_widget = "bat_options_combined",
* default_formatter = "bat_options_default"
* )
*/
class BatTypeOptions extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field) {
return [
'columns' => [
'name' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'quantity' => [
'type' => 'int',
'not null' => FALSE,
],
'operation' => [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
],
'value' => [
'type' => 'float',
'not null' => FALSE,
],
'type' => [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
],
],
];
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
return empty($this->get('name')->getValue()) ||
empty($this->get('quantity')->getValue()) ||
!(is_numeric($this->get('quantity')->getValue()) && is_int((int) $this->get('quantity')->getValue())) ||
((empty($this->get('value')->getValue()) || !is_numeric($this->get('value')->getValue())) && $this->get('operation')->getValue() != 'no_charge') ||
empty($this->get('operation')->getValue()) || !in_array($this->get('operation')->getValue(), array_keys(bat_options_price_options()));
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['name'] = DataReferenceTargetDefinition::create('string')
->setLabel($this->t('Name'));
$properties['quantity'] = DataReferenceTargetDefinition::create('integer')
->setLabel($this->t('Quantity'));
$properties['operation'] = DataReferenceTargetDefinition::create('string')
->setLabel($this->t('Operation'));
$properties['value'] = DataReferenceTargetDefinition::create('float')
->setLabel($this->t('Value'));
$properties['type'] = DataReferenceTargetDefinition::create('string')
->setLabel($this->t('Type'));
return $properties;
}
}
