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