Version 1
[yaffs-website] / web / core / modules / text / src / Plugin / Field / FieldWidget / TextareaWithSummaryWidget.php
1 <?php
2
3 namespace Drupal\text\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Symfony\Component\Validator\ConstraintViolationInterface;
7 use Drupal\Core\Field\FieldItemListInterface;
8
9 /**
10  * Plugin implementation of the 'text_textarea_with_summary' widget.
11  *
12  * @FieldWidget(
13  *   id = "text_textarea_with_summary",
14  *   label = @Translation("Text area with a summary"),
15  *   field_types = {
16  *     "text_with_summary"
17  *   }
18  * )
19  */
20 class TextareaWithSummaryWidget extends TextareaWidget {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function defaultSettings() {
26     return [
27       'rows' => '9',
28       'summary_rows' => '3',
29       'placeholder' => '',
30     ] + parent::defaultSettings();
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function settingsForm(array $form, FormStateInterface $form_state) {
37     $element = parent::settingsForm($form, $form_state);
38     $element['summary_rows'] = [
39       '#type' => 'number',
40       '#title' => t('Summary rows'),
41       '#default_value' => $this->getSetting('summary_rows'),
42       '#description' => $element['rows']['#description'],
43       '#required' => TRUE,
44       '#min' => 1,
45     ];
46     return $element;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function settingsSummary() {
53     $summary = parent::settingsSummary();
54
55     $summary[] = t('Number of summary rows: @rows', ['@rows' => $this->getSetting('summary_rows')]);
56
57     return $summary;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
64     $element = parent::formElement($items, $delta, $element, $form, $form_state);
65
66     $display_summary = $items[$delta]->summary || $this->getFieldSetting('display_summary');
67     $element['summary'] = [
68       '#type' => $display_summary ? 'textarea' : 'value',
69       '#default_value' => $items[$delta]->summary,
70       '#title' => t('Summary'),
71       '#rows' => $this->getSetting('summary_rows'),
72       '#description' => t('Leave blank to use trimmed value of full text as the summary.'),
73       '#attached' => [
74         'library' => ['text/drupal.text'],
75       ],
76       '#attributes' => ['class' => ['js-text-summary', 'text-summary']],
77       '#prefix' => '<div class="js-text-summary-wrapper text-summary-wrapper">',
78       '#suffix' => '</div>',
79       '#weight' => -10,
80     ];
81
82     return $element;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
89     $element = parent::errorElement($element, $violation, $form, $form_state);
90     if ($element === FALSE) {
91       return FALSE;
92     }
93     elseif (isset($violation->arrayPropertyPath[0])) {
94       return $element[$violation->arrayPropertyPath[0]];
95     }
96     else {
97       return $element;
98     }
99   }
100
101 }