TextWithSummaryItem.php 3.47 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
    return [
28
      'display_summary' => 0,
29
      'required_summary' => FALSE,
30
    ] + parent::defaultFieldSettings();
31 32
  }

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

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

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

49
    return $properties;
50 51 52 53 54
  }

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

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

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

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

99 100 101 102 103 104 105
    $element['required_summary'] = [
      '#type' => 'checkbox',
      '#title' => t('Require summary'),
      '#description' => t('The summary will also be visible when marked as required.'),
      '#default_value' => $settings['required_summary'],
    ];

106 107 108
    return $element;
  }

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
    $constraints = parent::getConstraints();
    if ($this->getSetting('required_summary')) {
      $manager = $this->getTypedDataManager()->getValidationConstraintManager();
      $constraints[] = $manager->create('ComplexData', [
        'summary' => [
          'NotNull' => [
            'message' => $this->t('The summary field is required for @name', ['@name' => $this->getFieldDefinition()->getLabel()]),
          ],
        ],
      ]);
    }
    return $constraints;
  }

127
}