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