Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldWidget / StringTextareaWidget.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Plugin implementation of the 'string_textarea' widget.
11  *
12  * @FieldWidget(
13  *   id = "string_textarea",
14  *   label = @Translation("Text area (multiple rows)"),
15  *   field_types = {
16  *     "string_long"
17  *   }
18  * )
19  */
20 class StringTextareaWidget extends WidgetBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function defaultSettings() {
26     return [
27       'rows' => '5',
28       'placeholder' => '',
29     ] + parent::defaultSettings();
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function settingsForm(array $form, FormStateInterface $form_state) {
36     $element['rows'] = [
37       '#type' => 'number',
38       '#title' => t('Rows'),
39       '#default_value' => $this->getSetting('rows'),
40       '#required' => TRUE,
41       '#min' => 1,
42     ];
43     $element['placeholder'] = [
44       '#type' => 'textfield',
45       '#title' => t('Placeholder'),
46       '#default_value' => $this->getSetting('placeholder'),
47       '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
48     ];
49     return $element;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function settingsSummary() {
56     $summary = [];
57
58     $summary[] = t('Number of rows: @rows', ['@rows' => $this->getSetting('rows')]);
59     $placeholder = $this->getSetting('placeholder');
60     if (!empty($placeholder)) {
61       $summary[] = t('Placeholder: @placeholder', ['@placeholder' => $placeholder]);
62     }
63
64     return $summary;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
71     $element['value'] = $element + [
72       '#type' => 'textarea',
73       '#default_value' => $items[$delta]->value,
74       '#rows' => $this->getSetting('rows'),
75       '#placeholder' => $this->getSetting('placeholder'),
76       '#attributes' => ['class' => ['js-text-full', 'text-full']],
77     ];
78
79     return $element;
80   }
81
82 }