View source
<?php
namespace Drupal\range\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\range\RangeItemInterface;
abstract class RangeItemBase extends FieldItemBase implements RangeItemInterface {
public static function mainPropertyName() {
return NULL;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'from' => static::getColumnSpecification($field_definition),
'to' => static::getColumnSpecification($field_definition),
],
];
}
public static function defaultFieldSettings() {
return [
'min' => '',
'max' => '',
'field' => [
'prefix' => '',
'suffix' => '',
],
'from' => [
'prefix' => '',
'suffix' => '',
],
'to' => [
'prefix' => '',
'suffix' => '',
],
'combined' => [
'prefix' => '',
'suffix' => '',
],
] + parent::defaultFieldSettings();
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['min'] = [
'#type' => 'number',
'#title' => $this
->t('Minimum'),
'#default_value' => $this
->getSetting('min'),
'#description' => $this
->t('The minimum value that should be allowed in this field. Leave blank for no minimum.'),
];
$element['max'] = [
'#type' => 'number',
'#title' => $this
->t('Maximum'),
'#default_value' => $this
->getSetting('max'),
'#description' => $this
->t('The maximum value that should be allowed in this field. Leave blank for no maximum.'),
];
$element += $this
->fieldSettingsFormSubElementPrefixSuffix($this
->t('FIELD'), 'field');
$element += $this
->fieldSettingsFormSubElementPrefixSuffix($this
->t('FROM'), 'from');
$element += $this
->fieldSettingsFormSubElementPrefixSuffix($this
->t('TO'), 'to');
$element += $this
->fieldSettingsFormSubElementPrefixSuffix($this
->t('COMBINED'), 'combined');
return $element;
}
protected static function propertyDefinitionsByType($type) {
$properties = [];
$properties['from'] = DataDefinition::create($type)
->setLabel(t('From value'))
->setRequired(TRUE);
$properties['to'] = DataDefinition::create($type)
->setLabel(t('To value'))
->setRequired(TRUE);
return $properties;
}
protected function fieldSettingsFormSubElementPrefixSuffix($title, $element_name) {
$element = [];
$element[$element_name] = [
'#type' => 'fieldset',
'#title' => $title,
];
$element[$element_name]['prefix'] = [
'#type' => 'textfield',
'#title' => $this
->t('Prefix'),
'#default_value' => $this
->getSetting($element_name)['prefix'],
'#size' => 60,
'#description' => $this
->t("Define a string that should be prefixed to the value, like '\$ ' or '€ '. Leave blank for none."),
];
$element[$element_name]['suffix'] = [
'#type' => 'textfield',
'#title' => $this
->t('Suffix'),
'#default_value' => $this
->getSetting($element_name)['suffix'],
'#size' => 60,
'#description' => $this
->t("Define a string that should be suffixed to the value, like ' m', ' kb/s'. Leave blank for none."),
];
return $element;
}
public function isEmpty() {
if (empty($this->from) && (string) $this->from !== '0' && empty($this->to) && (string) $this->to !== '0') {
return TRUE;
}
return FALSE;
}
}