View source
<?php
namespace Drupal\json_field\Plugin\Field\FieldType;
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
class JSONItem extends FieldItemBase {
const SIZE_SMALL = 255;
const SIZE_NORMAL = 16384;
const SIZE_MEDIUM = 16777216;
const SIZE_BIG = 4294967296;
public static function defaultStorageSettings() {
return [
'size' => static::SIZE_BIG,
] + parent::defaultStorageSettings();
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$elements = parent::storageSettingsForm($form, $form_state, $has_data);
$elements['size'] = [
'#type' => 'select',
'#title' => $this
->t('Maximum space allowed'),
'#default_value' => $this
->getSetting('size'),
'#description' => $this
->t('Controls the type of column used in the database to store the data.'),
'#options' => [
static::SIZE_SMALL => t('255 characters (varchar)'),
static::SIZE_NORMAL => t('64 KB (text, size "normal")'),
static::SIZE_MEDIUM => t('16 MB (text, size "medium")'),
static::SIZE_BIG => t('4 GB (text, size "big")'),
],
];
return $elements;
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('JSON Value'))
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema['columns']['value'] = [];
$size = $field_definition
->getSetting('size');
switch ($size) {
case static::SIZE_SMALL:
$schema['columns']['value']['type'] = 'varchar';
$schema['columns']['value']['length'] = static::SIZE_SMALL;
break;
case static::SIZE_NORMAL:
$schema['columns']['value']['type'] = 'text';
$schema['columns']['value']['size'] = 'normal';
break;
case static::SIZE_MEDIUM:
$schema['columns']['value']['type'] = 'text';
$schema['columns']['value']['size'] = 'medium';
break;
case static::SIZE_BIG:
$schema['columns']['value']['type'] = 'text';
$schema['columns']['value']['size'] = 'big';
break;
}
return $schema;
}
public function getMaxLength() {
$size = $this
->getSetting('size');
switch ($size) {
case static::SIZE_SMALL:
return static::SIZE_SMALL;
default:
return floor($size / 4);
}
}
public function getConstraints() {
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints = parent::getConstraints();
$max_length = $this
->getMaxLength();
$constraints[] = $constraint_manager
->create('ComplexData', [
'value' => [
'Length' => [
'max' => $max_length,
'maxMessage' => t('%name: the text may not be longer than @max characters.', [
'%name' => $this
->getFieldDefinition()
->getLabel(),
'@max' => $max_length,
]),
],
],
]);
return $constraints;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$random = new Random();
$values['value'] = '{"foo": "' . $random
->word(mt_rand(1, 2000)) . '""}';
return $values;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
}