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