Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / options / src / Plugin / Field / FieldType / ListStringItem.php
1 <?php
2
3 namespace Drupal\options\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\TypedData\DataDefinition;
7
8 /**
9  * Plugin implementation of the 'list_string' field type.
10  *
11  * @FieldType(
12  *   id = "list_string",
13  *   label = @Translation("List (text)"),
14  *   description = @Translation("This field stores text values from a list of allowed 'value => label' pairs, i.e. 'US States': IL => Illinois, IA => Iowa, IN => Indiana."),
15  *   category = @Translation("Text"),
16  *   default_widget = "options_select",
17  *   default_formatter = "list_default",
18  * )
19  */
20 class ListStringItem extends ListItemBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
26     $properties['value'] = DataDefinition::create('string')
27       ->setLabel(t('Text value'))
28       ->addConstraint('Length', ['max' => 255])
29       ->setRequired(TRUE);
30
31     return $properties;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function schema(FieldStorageDefinitionInterface $field_definition) {
38     return [
39       'columns' => [
40         'value' => [
41           'type' => 'varchar',
42           'length' => 255,
43         ],
44       ],
45       'indexes' => [
46         'value' => ['value'],
47       ],
48     ];
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function allowedValuesDescription() {
55     $description = '<p>' . t('The possible values this field can contain. Enter one value per line, in the format key|label.');
56     $description .= '<br/>' . t('The key is the stored value. The label will be used in displayed values and edit forms.');
57     $description .= '<br/>' . t('The label is optional: if a line contains a single string, it will be used as key and label.');
58     $description .= '</p>';
59     $description .= '<p>' . t('Allowed HTML tags in labels: @tags', ['@tags' => $this->displayAllowedTags()]) . '</p>';
60     return $description;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   protected static function validateAllowedValue($option) {
67     if (mb_strlen($option) > 255) {
68       return t('Allowed values list: each key must be a string at most 255 characters long.');
69     }
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   protected static function castAllowedValue($value) {
76     return (string) $value;
77   }
78
79 }