3 namespace Drupal\text\Plugin\Field\FieldType;
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FieldItemBase;
8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
9 use Drupal\Core\TypedData\DataDefinition;
12 * Base class for 'text' configurable field types.
14 abstract class TextItemBase extends FieldItemBase {
19 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
20 $properties['value'] = DataDefinition::create('string')
24 $properties['format'] = DataDefinition::create('filter_format')
25 ->setLabel(t('Text format'));
27 $properties['processed'] = DataDefinition::create('string')
28 ->setLabel(t('Processed text'))
29 ->setDescription(t('The text with the text format applied.'))
31 ->setClass('\Drupal\text\TextProcessed')
32 ->setSetting('text source', 'value');
40 public function applyDefaultValue($notify = TRUE) {
41 // @todo: Add in the filter default format here.
42 $this->setValue(['format' => NULL], $notify);
49 public function isEmpty() {
50 $value = $this->get('value')->getValue();
51 return $value === NULL || $value === '';
57 public function onChange($property_name, $notify = TRUE) {
58 // Unset processed properties that are affected by the change.
59 foreach ($this->definition->getPropertyDefinitions() as $property => $definition) {
60 if ($definition->getClass() == '\Drupal\text\TextProcessed') {
61 if ($property_name == 'format' || ($definition->getSetting('text source') == $property_name)) {
62 $this->writePropertyValue($property, NULL);
66 parent::onChange($property_name, $notify);
72 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
73 $random = new Random();
74 $settings = $field_definition->getSettings();
76 if (empty($settings['max_length'])) {
78 $value = $random->paragraphs();
81 // Textfield handling.
82 $value = substr($random->sentences(mt_rand(1, $settings['max_length'] / 3), FALSE), 0, $settings['max_length']);
88 'format' => filter_fallback_format(),