ListStringItem.php in Drupal 9
File
core/modules/options/src/Plugin/Field/FieldType/ListStringItem.php
View source
<?php
namespace Drupal\options\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class ListStringItem extends ListItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Text value'))
->addConstraint('Length', [
'max' => 255,
])
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 255,
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
protected function allowedValuesDescription() {
$description = '<p>' . t('The possible values this field can contain. Enter one value per line, in the format key|label.');
$description .= '<br/>' . t('The key is the stored value. The label will be used in displayed values and edit forms.');
$description .= '<br/>' . t('The label is optional: if a line contains a single string, it will be used as key and label.');
$description .= '</p>';
$description .= '<p>' . t('Allowed HTML tags in labels: @tags', [
'@tags' => FieldFilteredMarkup::displayAllowedTags(),
]) . '</p>';
return $description;
}
protected static function validateAllowedValue($option) {
if (mb_strlen($option) > 255) {
return t('Allowed values list: each key must be a string at most 255 characters long.');
}
}
protected static function castAllowedValue($value) {
return (string) $value;
}
}