3 namespace Drupal\options\Plugin\Field\FieldType;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\TypedData\DataDefinition;
9 * Plugin implementation of the 'list_string' field type.
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",
20 class ListStringItem extends ListItemBase {
25 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
26 $properties['value'] = DataDefinition::create('string')
27 ->setLabel(t('Text value'))
28 ->addConstraint('Length', ['max' => 255])
37 public static function schema(FieldStorageDefinitionInterface $field_definition) {
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>';
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.');
75 protected static function castAllowedValue($value) {
76 return (string) $value;