Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / modules / field_test / src / Plugin / Field / FieldType / TestItem.php
1 <?php
2
3 namespace Drupal\field_test\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\TypedData\DataDefinition;
8 use Drupal\Core\Field\FieldItemBase;
9
10 /**
11  * Defines the 'test_field' entity field item.
12  *
13  * @FieldType(
14  *   id = "test_field",
15  *   label = @Translation("Test field"),
16  *   description = @Translation("Dummy field type used for tests."),
17  *   default_widget = "test_field_widget",
18  *   default_formatter = "field_test_default"
19  * )
20  */
21 class TestItem extends FieldItemBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public static function defaultStorageSettings() {
27     return [
28       'test_field_storage_setting' => 'dummy test string',
29       'changeable' => 'a changeable field storage setting',
30       'unchangeable' => 'an unchangeable field storage setting',
31       'translatable_storage_setting' => 'a translatable field storage setting',
32     ] + parent::defaultStorageSettings();
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function defaultFieldSettings() {
39     return [
40       'test_field_setting' => 'dummy test string',
41       'translatable_field_setting' => 'a translatable field setting',
42     ] + parent::defaultFieldSettings();
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
49     $properties['value'] = DataDefinition::create('integer')
50       ->setLabel(t('Test integer value'))
51       ->setRequired(TRUE);
52
53     return $properties;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public static function schema(FieldStorageDefinitionInterface $field_definition) {
60     return [
61       'columns' => [
62         'value' => [
63           'type' => 'int',
64           'size' => 'medium',
65         ],
66       ],
67       'indexes' => [
68         'value' => ['value'],
69       ],
70     ];
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
77     $form['test_field_storage_setting'] = [
78       '#type' => 'textfield',
79       '#title' => t('Field test field storage setting'),
80       '#default_value' => $this->getSetting('test_field_storage_setting'),
81       '#required' => FALSE,
82       '#description' => t('A dummy form element to simulate field storage setting.'),
83     ];
84
85     return $form;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
92     $form['test_field_setting'] = [
93       '#type' => 'textfield',
94       '#title' => t('Field test field setting'),
95       '#default_value' => $this->getSetting('test_field_setting'),
96       '#required' => FALSE,
97       '#description' => t('A dummy form element to simulate field setting.'),
98     ];
99
100     return $form;
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function delete() {
107     // Reports that delete() method is executed for testing purposes.
108     field_test_memorize('field_test_field_delete', [$this->getEntity()]);
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function getConstraints() {
115     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
116     $constraints = parent::getConstraints();
117
118     $constraints[] = $constraint_manager->create('ComplexData', [
119       'value' => [
120         'TestField' => [
121           'value' => -1,
122           'message' => t('%name does not accept the value @value.', ['%name' => $this->getFieldDefinition()->getLabel(), '@value' => -1]),
123         ],
124       ],
125     ]);
126
127     return $constraints;
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function isEmpty() {
134     return empty($this->value);
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public static function storageSettingsToConfigData(array $settings) {
141     $settings['config_data_from_storage_setting'] = 'TRUE';
142     unset($settings['storage_setting_from_config_data']);
143     return $settings;
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public static function storageSettingsFromConfigData(array $settings) {
150     $settings['storage_setting_from_config_data'] = 'TRUE';
151     unset($settings['config_data_from_storage_setting']);
152     return $settings;
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public static function fieldSettingsToConfigData(array $settings) {
159     $settings['config_data_from_field_setting'] = 'TRUE';
160     unset($settings['field_setting_from_config_data']);
161     return $settings;
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public static function fieldSettingsFromConfigData(array $settings) {
168     $settings['field_setting_from_config_data'] = 'TRUE';
169     unset($settings['config_data_from_field_setting']);
170     return $settings;
171   }
172
173 }