Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldType / StringItem.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\Form\FormStateInterface;
9
10 /**
11  * Defines the 'string' entity field type.
12  *
13  * @FieldType(
14  *   id = "string",
15  *   label = @Translation("Text (plain)"),
16  *   description = @Translation("A field containing a plain string value."),
17  *   category = @Translation("Text"),
18  *   default_widget = "string_textfield",
19  *   default_formatter = "string"
20  * )
21  */
22 class StringItem extends StringItemBase {
23
24   /**
25    * {@inheritdoc}
26    */
27   public static function defaultStorageSettings() {
28     return [
29       'max_length' => 255,
30       'is_ascii' => FALSE,
31     ] + parent::defaultStorageSettings();
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function schema(FieldStorageDefinitionInterface $field_definition) {
38     return [
39       'columns' => [
40         'value' => [
41           'type' => $field_definition->getSetting('is_ascii') === TRUE ? 'varchar_ascii' : 'varchar',
42           'length' => (int) $field_definition->getSetting('max_length'),
43           'binary' => $field_definition->getSetting('case_sensitive'),
44         ],
45       ],
46     ];
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function getConstraints() {
53     $constraints = parent::getConstraints();
54
55     if ($max_length = $this->getSetting('max_length')) {
56       $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
57       $constraints[] = $constraint_manager->create('ComplexData', [
58         'value' => [
59           'Length' => [
60             'max' => $max_length,
61             'maxMessage' => t('%name: may not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length]),
62           ],
63         ],
64       ]);
65     }
66
67     return $constraints;
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
74     $random = new Random();
75     $values['value'] = $random->word(mt_rand(1, $field_definition->getSetting('max_length')));
76     return $values;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
83     $element = [];
84
85     $element['max_length'] = [
86       '#type' => 'number',
87       '#title' => t('Maximum length'),
88       '#default_value' => $this->getSetting('max_length'),
89       '#required' => TRUE,
90       '#description' => t('The maximum length of the field in characters.'),
91       '#min' => 1,
92       '#disabled' => $has_data,
93     ];
94
95     return $element;
96   }
97
98 }