Version 1
[yaffs-website] / web / core / modules / text / src / Plugin / Field / FieldType / TextWithSummaryItem.php
1 <?php
2
3 namespace Drupal\text\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\TypedData\DataDefinition;
8
9 /**
10  * Plugin implementation of the 'text_with_summary' field type.
11  *
12  * @FieldType(
13  *   id = "text_with_summary",
14  *   label = @Translation("Text (formatted, long, with summary)"),
15  *   description = @Translation("This field stores long text with a format and an optional summary."),
16  *   category = @Translation("Text"),
17  *   default_widget = "text_textarea_with_summary",
18  *   default_formatter = "text_default"
19  * )
20  */
21 class TextWithSummaryItem extends TextItemBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public static function defaultFieldSettings() {
27     return [
28       'display_summary' => 0,
29     ] + parent::defaultFieldSettings();
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
36     $properties = parent::propertyDefinitions($field_definition);
37
38     $properties['summary'] = DataDefinition::create('string')
39       ->setLabel(t('Summary'));
40
41     $properties['summary_processed'] = DataDefinition::create('string')
42       ->setLabel(t('Processed summary'))
43       ->setDescription(t('The summary text with the text format applied.'))
44       ->setComputed(TRUE)
45       ->setClass('\Drupal\text\TextProcessed')
46       ->setSetting('text source', 'summary');
47
48     return $properties;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function schema(FieldStorageDefinitionInterface $field_definition) {
55     return [
56       'columns' => [
57         'value' => [
58           'type' => 'text',
59           'size' => 'big',
60         ],
61         'summary' => [
62           'type' => 'text',
63           'size' => 'big',
64         ],
65         'format' => [
66           'type' => 'varchar_ascii',
67           'length' => 255,
68         ],
69       ],
70       'indexes' => [
71         'format' => ['format'],
72       ],
73     ];
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function isEmpty() {
80     $value = $this->get('summary')->getValue();
81     return parent::isEmpty() && ($value === NULL || $value === '');
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
88     $element = [];
89     $settings = $this->getSettings();
90
91     $element['display_summary'] = [
92       '#type' => 'checkbox',
93       '#title' => t('Summary input'),
94       '#default_value' => $settings['display_summary'],
95       '#description' => t('This allows authors to input an explicit summary, to be displayed instead of the automatically trimmed text when using the "Summary or trimmed" display type.'),
96     ];
97
98     return $element;
99   }
100
101 }