Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / field / type.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Plugin\Field\FieldType;
4
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FieldItemBase;
8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
9 {% if configurable_storage or configurable_instance %}
10 use Drupal\Core\Form\FormStateInterface;
11 {% endif %}
12 use Drupal\Core\TypedData\DataDefinition;
13
14 /**
15  * Defines the '{{ plugin_id }}' field type.
16  *
17  * @FieldType(
18  *   id = "{{ plugin_id }}",
19  *   label = @Translation("{{ plugin_label }}"),
20  *   category = @Translation("General"),
21  *   default_widget = "string_textfield",
22  *   default_formatter = "string"
23  * )
24  *
25  * @DCG
26  * If you are implementing a single value field type you may want to inherit
27  * this class form some of the field type classes provided by Drupal core.
28  * Check out /core/lib/Drupal/Core/Field/Plugin/Field/FieldType directory for a
29  * list of available field type implementations.
30  */
31 class {{ class }} extends FieldItemBase {
32
33 {% if configurable_storage %}
34   /**
35    * {@inheritdoc}
36    */
37   public static function defaultStorageSettings() {
38     $settings = ['foo' => 'wine'];
39     return $settings + parent::defaultStorageSettings();
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
46
47     $element['foo'] = [
48       '#type' => 'textfield',
49       '#title' => $this->t('Foo'),
50       '#default_value' => $this->getSetting('foo'),
51       '#disabled' => $has_data,
52     ];
53
54     return $element;
55   }
56
57 {% endif %}
58 {% if configurable_instance %}
59   /**
60    * {@inheritdoc}
61    */
62   public static function defaultFieldSettings() {
63     $settings = ['bar' => 'beer'];
64     return $settings + parent::defaultFieldSettings();
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
71
72     $element['bar'] = [
73       '#type' => 'textfield',
74       '#title' => t('Bar'),
75       '#default_value' => $this->getSetting('bar'),
76     ];
77
78     return $element;
79   }
80
81 {% endif %}
82   /**
83    * {@inheritdoc}
84    */
85   public function isEmpty() {
86     $value = $this->get('value')->getValue();
87     return $value === NULL || $value === '';
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
94
95     // @DCG
96     // See /core/lib/Drupal/Core/TypedData/Plugin/DataType directory for
97     // available data types.
98     $properties['value'] = DataDefinition::create('string')
99       ->setLabel(t('Text value'))
100       ->setRequired(TRUE);
101
102     return $properties;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function getConstraints() {
109     $constraints = parent::getConstraints();
110
111     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
112
113     // @DCG Suppose our value must not be longer than 10 characters.
114     $options['value']['Length']['max'] = 10;
115
116     // @DCG
117     // See /core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint
118     // directory for available constraints.
119     $constraints[] = $constraint_manager->create('ComplexData', $options);
120     return $constraints;
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public static function schema(FieldStorageDefinitionInterface $field_definition) {
127
128     $columns = [
129       'value' => [
130         'type' => 'varchar',
131         'not null' => FALSE,
132         'description' => 'Column description.',
133         'length' => 255,
134       ],
135     ];
136
137     $schema = [
138       'columns' => $columns,
139       // @DCG Add indexes here if necessary.
140     ];
141
142     return $schema;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
149     $random = new Random();
150     $values['value'] = $random->word(mt_rand(1, 50));
151     return $values;
152   }
153
154 }