StringTextareaWidget.php

Same filename in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php

Namespace

Drupal\Core\Field\Plugin\Field\FieldWidget

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php

View source
<?php

namespace Drupal\Core\Field\Plugin\Field\FieldWidget;

use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'string_textarea' widget.
 */
class StringTextareaWidget extends WidgetBase {
    
    /**
     * {@inheritdoc}
     */
    public static function defaultSettings() {
        return [
            'rows' => '5',
            'placeholder' => '',
        ] + parent::defaultSettings();
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsForm(array $form, FormStateInterface $form_state) {
        $element['rows'] = [
            '#type' => 'number',
            '#title' => $this->t('Rows'),
            '#default_value' => $this->getSetting('rows'),
            '#required' => TRUE,
            '#min' => 1,
        ];
        $element['placeholder'] = [
            '#type' => 'textarea',
            '#title' => $this->t('Placeholder'),
            '#default_value' => $this->getSetting('placeholder'),
            '#description' => $this->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.'),
        ];
        return $element;
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsSummary() {
        $summary = [];
        $summary[] = $this->t('Number of rows: @rows', [
            '@rows' => $this->getSetting('rows'),
        ]);
        $placeholder = $this->getSetting('placeholder');
        if (!empty($placeholder)) {
            $summary[] = $this->t('Placeholder: @placeholder', [
                '@placeholder' => $placeholder,
            ]);
        }
        return $summary;
    }
    
    /**
     * {@inheritdoc}
     */
    public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
        $element['value'] = $element + [
            '#type' => 'textarea',
            '#default_value' => $items[$delta]->value,
            '#rows' => $this->getSetting('rows'),
            '#placeholder' => $this->getSetting('placeholder'),
            '#attributes' => [
                'class' => [
                    'js-text-full',
                    'text-full',
                ],
            ],
        ];
        return $element;
    }

}

Classes

Title Deprecated Summary
StringTextareaWidget Plugin implementation of the 'string_textarea' widget.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.