TextWithSummaryItem.php 2.74 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
22
23
24
25
26
 *   default_widget = "text_textarea_with_summary",
 *   default_formatter = "text_default"
 * )
 */
class TextWithSummaryItem extends TextItemBase {

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

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

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

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

52
    return $properties;
53
54
55
56
57
  }

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

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

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

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

}