View source
<?php
namespace Drupal\field_test\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldItemBase;
class TestItem extends FieldItemBase {
public static function defaultStorageSettings() {
return [
'test_field_storage_setting' => 'dummy test string',
'changeable' => 'a changeable field storage setting',
'unchangeable' => 'an unchangeable field storage setting',
'translatable_storage_setting' => 'a translatable field storage setting',
] + parent::defaultStorageSettings();
}
public static function defaultFieldSettings() {
return [
'test_field_setting' => 'dummy test string',
'translatable_field_setting' => 'a translatable field setting',
] + parent::defaultFieldSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('integer')
->setLabel(t('Test integer value'))
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'int',
'size' => 'medium',
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$form['test_field_storage_setting'] = [
'#type' => 'textfield',
'#title' => t('Field test field storage setting'),
'#default_value' => $this
->getSetting('test_field_storage_setting'),
'#required' => FALSE,
'#description' => t('A dummy form element to simulate field storage setting.'),
];
return $form;
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$form['test_field_setting'] = [
'#type' => 'textfield',
'#title' => t('Field test field setting'),
'#default_value' => $this
->getSetting('test_field_setting'),
'#required' => FALSE,
'#description' => t('A dummy form element to simulate field setting.'),
];
return $form;
}
public function delete() {
field_test_memorize('field_test_field_delete', [
$this
->getEntity(),
]);
}
public function getConstraints() {
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints = parent::getConstraints();
$constraints[] = $constraint_manager
->create('ComplexData', [
'value' => [
'TestField' => [
'value' => -1,
'message' => t('%name does not accept the value @value.', [
'%name' => $this
->getFieldDefinition()
->getLabel(),
'@value' => -1,
]),
],
],
]);
return $constraints;
}
public function isEmpty() {
return empty($this->value);
}
public static function storageSettingsToConfigData(array $settings) {
$settings['config_data_from_storage_setting'] = 'TRUE';
unset($settings['storage_setting_from_config_data']);
return $settings;
}
public static function storageSettingsFromConfigData(array $settings) {
$settings['storage_setting_from_config_data'] = 'TRUE';
unset($settings['config_data_from_storage_setting']);
return $settings;
}
public static function fieldSettingsToConfigData(array $settings) {
$settings['config_data_from_field_setting'] = 'TRUE';
unset($settings['field_setting_from_config_data']);
return $settings;
}
public static function fieldSettingsFromConfigData(array $settings) {
$settings['field_setting_from_config_data'] = 'TRUE';
unset($settings['config_data_from_field_setting']);
return $settings;
}
}