TextWithSummaryItem.php 2.7 KB
Newer Older
1
2
<?php

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
11
12
13

/**
 * Plugin implementation of the 'text_with_summary' field type.
 *
 * @FieldType(
 *   id = "text_with_summary",
14
15
 *   label = @Translation("Text (formatted, long, with summary)"),
 *   description = @Translation("This field stores long text with a format and an optional summary."),
16
 *   category = @Translation("Text"),
17
18
19
20
21
22
 *   default_widget = "text_textarea_with_summary",
 *   default_formatter = "text_default"
 * )
 */
class TextWithSummaryItem extends TextItemBase {

23
24
25
  /**
   * {@inheritdoc}
   */
26
  public static function defaultFieldSettings() {
27
28
    return array(
      'display_summary' => 0,
29
    ) + parent::defaultFieldSettings();
30
31
  }

32
33
34
  /**
   * {@inheritdoc}
   */
35
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
36
37
38
    $properties = parent::propertyDefinitions($field_definition);

    $properties['summary'] = DataDefinition::create('string')
39
      ->setLabel(t('Summary'));
40

41
    $properties['summary_processed'] = DataDefinition::create('string')
42
43
      ->setLabel(t('Processed summary'))
      ->setDescription(t('The summary text with the text format applied.'))
44
45
46
      ->setComputed(TRUE)
      ->setClass('\Drupal\text\TextProcessed')
      ->setSetting('text source', 'summary');
47

48
    return $properties;
49
50
51
52
53
  }

  /**
   * {@inheritdoc}
   */
54
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
55
56
57
58
59
60
61
62
63
64
65
    return array(
      'columns' => array(
        'value' => array(
          'type' => 'text',
          'size' => 'big',
        ),
        'summary' => array(
          'type' => 'text',
          'size' => 'big',
        ),
        'format' => array(
66
          'type' => 'varchar_ascii',
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
          'length' => 255,
        ),
      ),
      'indexes' => array(
        'format' => array('format'),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('summary')->getValue();
    return parent::isEmpty() && ($value === NULL || $value === '');
  }

  /**
   * {@inheritdoc}
   */
87
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
88
    $element = array();
89
    $settings = $this->getSettings();
90
91
92
93

    $element['display_summary'] = array(
      '#type' => 'checkbox',
      '#title' => t('Summary input'),
94
      '#default_value' => $settings['display_summary'],
95
96
97
98
99
100
101
      '#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.'),
    );

    return $element;
  }

}