Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / text / src / Plugin / Field / FieldType / TextItem.php
1 <?php
2
3 namespace Drupal\text\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Plugin implementation of the 'text' field type.
10  *
11  * @FieldType(
12  *   id = "text",
13  *   label = @Translation("Text (formatted)"),
14  *   description = @Translation("This field stores a text with a text format."),
15  *   category = @Translation("Text"),
16  *   default_widget = "text_textfield",
17  *   default_formatter = "text_default"
18  * )
19  */
20 class TextItem extends TextItemBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function defaultStorageSettings() {
26     return [
27       'max_length' => 255,
28     ] + parent::defaultStorageSettings();
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public static function schema(FieldStorageDefinitionInterface $field_definition) {
35     return [
36       'columns' => [
37         'value' => [
38           'type' => 'varchar',
39           'length' => $field_definition->getSetting('max_length'),
40         ],
41         'format' => [
42           'type' => 'varchar',
43           'length' => 255,
44         ],
45       ],
46       'indexes' => [
47         'format' => ['format'],
48       ],
49     ];
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function getConstraints() {
56     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
57     $constraints = parent::getConstraints();
58
59     if ($max_length = $this->getSetting('max_length')) {
60       $constraints[] = $constraint_manager->create('ComplexData', [
61         'value' => [
62           'Length' => [
63             'max' => $max_length,
64             'maxMessage' => t('%name: the text may not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length]),
65           ],
66         ],
67       ]);
68     }
69
70     return $constraints;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
77     $element = [];
78
79     $element['max_length'] = [
80       '#type' => 'number',
81       '#title' => t('Maximum length'),
82       '#default_value' => $this->getSetting('max_length'),
83       '#required' => TRUE,
84       '#description' => t('The maximum length of the field in characters.'),
85       '#min' => 1,
86       '#disabled' => $has_data,
87     ];
88     $element += parent::storageSettingsForm($form, $form_state, $has_data);
89
90     return $element;
91   }
92
93 }