TextWithSummaryItem.php 2.77 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\text\Plugin\Field\FieldType\TextWithSummaryItem.
6
7
 */

8
namespace Drupal\text\Plugin\Field\FieldType;
9

10
use Drupal\Core\Field\FieldStorageDefinitionInterface;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\Core\TypedData\DataDefinition;
13
14
15
16
17
18

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

28
29
30
  /**
   * {@inheritdoc}
   */
31
  public static function defaultFieldSettings() {
32
33
    return array(
      'display_summary' => 0,
34
    ) + parent::defaultFieldSettings();
35
36
  }

37
38
39
  /**
   * {@inheritdoc}
   */
40
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
41
42
43
    $properties = parent::propertyDefinitions($field_definition);

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

46
    $properties['summary_processed'] = DataDefinition::create('string')
47
48
      ->setLabel(t('Processed summary'))
      ->setDescription(t('The summary text with the text format applied.'))
49
50
51
      ->setComputed(TRUE)
      ->setClass('\Drupal\text\TextProcessed')
      ->setSetting('text source', 'summary');
52

53
    return $properties;
54
55
56
57
58
  }

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

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

  /**
   * {@inheritdoc}
   */
92
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
93
    $element = array();
94
    $settings = $this->getSettings();
95
96
97
98

    $element['display_summary'] = array(
      '#type' => 'checkbox',
      '#title' => t('Summary input'),
99
      '#default_value' => $settings['display_summary'],
100
101
102
103
104
105
106
      '#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;
  }

}