3 namespace Drupal\{{ machine_name }}\Plugin\Field\FieldType;
6 use Drupal\Component\Utility\Random;
8 use Drupal\Core\Field\FieldDefinitionInterface;
9 use Drupal\Core\Field\FieldItemBase;
10 use Drupal\Core\Field\FieldStorageDefinitionInterface;
11 {% if storage_settings or instance_settings %}
12 use Drupal\Core\Form\FormStateInterface;
15 use Drupal\Core\Render\Element\Email;
17 use Drupal\Core\TypedData\DataDefinition;
20 * Defines the '{{ field_id }}' field type.
23 * id = "{{ field_id }}",
24 * label = @Translation("{{ field_label }}"),
25 * category = @Translation("General"),
26 * default_widget = "{{ field_id }}",
27 * default_formatter = "{{ field_id }}_default"
30 class {{ type_class }} extends FieldItemBase {
32 {% if storage_settings %}
36 public static function defaultStorageSettings() {
37 $settings = ['foo' => 'example'];
38 return $settings + parent::defaultStorageSettings();
44 public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
45 $settings = $this->getSettings();
48 '#type' => 'textfield',
49 '#title' => $this->t('Foo'),
50 '#default_value' => $settings['foo'],
51 '#disabled' => $has_data,
58 {% if instance_settings %}
62 public static function defaultFieldSettings() {
63 $settings = ['bar' => 'example'];
64 return $settings + parent::defaultFieldSettings();
70 public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
71 $settings = $this->getSettings();
74 '#type' => 'textfield',
75 '#title' => $this->t('Foo'),
76 '#default_value' => $settings['bar'],
86 public function isEmpty() {
87 {% for subfield in subfields %}
89 {% if subfield.type == 'boolean' %}$this->{{ subfield.machine_name }} == 1{% else %}$this->{{ subfield.machine_name }} !== NULL{% endif %}
91 {% if loop.index == 1 %}
92 if ({{ condition }}) {
94 elseif ({{ condition }}) {
105 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
107 {% for subfield in subfields %}
108 $properties['{{ subfield.machine_name }}'] = DataDefinition::create('{{ subfield.data_type }}')
109 ->setLabel(t('{{ subfield.name }}'));
118 public function getConstraints() {
119 $constraints = parent::getConstraints();
121 {% for subfield in subfields %}
122 {% if subfield.list %}
123 $options['{{ subfield.machine_name }}']['AllowedValues'] = array_keys({{ type_class }}::{{ subfield.allowed_values_method }}());
126 {% if subfield.required %}
127 {% if subfield.type == 'boolean' %}
128 // NotBlank validator is not suitable for booleans because it does not
129 // recognize '0' as an empty value.
130 $options['{{ subfield.machine_name }}']['AllowedValues']['choices'] = [1];
131 $options['{{ subfield.machine_name }}']['AllowedValues']['message'] = $this->t('This value should not be blank.');
134 $options['{{ subfield.machine_name }}']['NotBlank'] = [];
136 {% if subfield.type == 'email' %}
137 $options['{{ subfield.machine_name }}']['Length']['max'] = Email::EMAIL_MAX_LENGTH;
143 {% if list or required %}
144 $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
145 $constraints[] = $constraint_manager->create('ComplexData', $options);
147 // @todo Add more constrains here.
154 public static function schema(FieldStorageDefinitionInterface $field_definition) {
157 {% for subfield in subfields %}
158 '{{ subfield.machine_name }}' => [
159 {% if subfield.type == 'boolean' %}
162 {% elseif subfield.type == 'string' %}
165 {% elseif subfield.type == 'text' %}
168 {% elseif subfield.type == 'integer' %}
171 {% elseif subfield.type == 'float' %}
174 {% elseif subfield.type == 'numeric' %}
178 {% elseif subfield.type == 'email' %}
180 'length' => Email::EMAIL_MAX_LENGTH,
181 {% elseif subfield.type == 'telephone' %}
184 {% elseif subfield.type == 'uri' %}
187 {% elseif subfield.type == 'datetime' %}
196 'columns' => $columns,
197 // @DCG Add indexes here if necessary.
206 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
209 $random = new Random();
212 {% for subfield in subfields %}
213 {% if subfield.list %}
214 $values['{{ subfield.machine_name }}'] = array_rand(self::{{ subfield.allowed_values_method }}());
216 {% elseif subfield.type == 'boolean' %}
217 $values['{{ subfield.machine_name }}'] = (bool) mt_rand(0, 1);
219 {% elseif subfield.type == 'string' %}
220 $values['{{ subfield.machine_name }}'] = $random->word(mt_rand(1, 255));
222 {% elseif subfield.type == 'text' %}
223 $values['{{ subfield.machine_name }}'] = $random->paragraphs(5);
225 {% elseif subfield.type == 'integer' %}
226 $values['{{ subfield.machine_name }}'] = mt_rand(-1000, 1000);
228 {% elseif subfield.type == 'float' %}
230 $random_decimal = mt_rand() / mt_getrandmax() * (1000 - 0);
231 $values['{{ subfield.machine_name }}'] = floor($random_decimal * pow(10, $scale)) / pow(10, $scale);
233 {% elseif subfield.type == 'numeric' %}
234 $scale = rand(10, 2);
235 $random_decimal = -1000 + mt_rand() / mt_getrandmax() * (-1000 - 1000);
236 $values['{{ subfield.machine_name }}'] = floor($random_decimal * pow(10, $scale)) / pow(10, $scale);
238 {% elseif subfield.type == 'email' %}
239 $values['{{ subfield.machine_name }}'] = strtolower($random->name()) . '@example.com';
241 {% elseif subfield.type == 'telephone' %}
242 $values['{{ subfield.machine_name }}'] = mt_rand(pow(10, 8), pow(10, 9) - 1);
244 {% elseif subfield.type == 'uri' %}
245 $tlds = ['com', 'net', 'gov', 'org', 'edu', 'biz', 'info'];
246 $domain_length = mt_rand(7, 15);
247 $protocol = mt_rand(0, 1) ? 'https' : 'http';
248 $www = mt_rand(0, 1) ? 'www' : '';
249 $domain = $random->word($domain_length);
250 $tld = $tlds[mt_rand(0, (count($tlds) - 1))];
251 $values['{{ subfield.machine_name }}'] = "$protocol://$www.$domain.$tld";
253 {% elseif subfield.type == 'datetime' %}
254 $timestamp = \Drupal::time()->getRequestTime() - mt_rand(0, 86400 * 365);
255 $values['{{ subfield.machine_name }}'] = gmdate('{{ subfield.date_storage_format }}', $timestamp);
262 {% for subfield in subfields %}
263 {% if subfield.list %}
265 * Returns allowed values for '{{ subfield.machine_name }}' sub-field.
268 * The list of allowed values.
270 public static function {{ subfield.allowed_values_method }}() {
272 {% if subfield.type == 'string' %}
273 'alpha' => t('Alpha'),
275 'gamma' => t('Gamma'),
276 {% elseif subfield.type == 'integer' %}
280 {% elseif subfield.type == 'float' %}
284 {% elseif subfield.type == 'numeric' %}
288 {% elseif subfield.type == 'email' %}
289 'alpha@example.com' => 'alpha@example.com',
290 'beta@example.com' => 'beta@example.com',
291 'gamma@example.com' => 'gamma@example.com',
292 {% elseif subfield.type == 'telephone' %}
293 '71234567001' => '+7(123)45-67-001',
294 '71234567002' => '+7(123)45-67-002',
295 '71234567003' => '+7(123)45-67-003',
296 {% elseif subfield.type == 'uri' %}
297 'https://example.com' => 'https://example.com',
298 'http://www.php.net' => 'http://www.php.net',
299 'https://www.drupal.org' => 'https://www.drupal.org',
300 {% elseif subfield.type == 'datetime' %}
301 {% if subfield.date_type == 'date' %}
302 '2018-01-01' => '1 January 2018',
303 '2018-02-01' => '1 February 2018',
304 '2018-03-01' => '1 March 2018',
306 '2018-01-01T00:10:10' => '1 January 2018, 00:10:10',
307 '2018-02-01T00:20:20' => '1 February 2018, 00:20:20',
308 '2018-03-01T00:30:30' => '1 March 2018, 00:30:30',