class TextareaWidget in Typed Data API enhancements 8
Plugin implementation of the 'textarea' widget.
Plugin annotation
@TypedDataFormWidget(
id = "textarea",
label = @Translation("Textarea"),
description = @Translation("A multi-line text input widget."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\typed_data\Widget\FormWidgetBase implements ContainerFactoryPluginInterface, FormWidgetInterface
- class \Drupal\typed_data\Plugin\TypedDataFormWidget\TextareaWidget
- class \Drupal\typed_data\Widget\FormWidgetBase implements ContainerFactoryPluginInterface, FormWidgetInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TextareaWidget
File
- src/
Plugin/ TypedDataFormWidget/ TextareaWidget.php, line 27
Namespace
Drupal\typed_data\Plugin\TypedDataFormWidgetView source
class TextareaWidget extends FormWidgetBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'label' => NULL,
'description' => NULL,
'placeholder' => NULL,
'rows' => 5,
'cols' => 60,
'resizable' => 'both',
];
}
/**
* {@inheritdoc}
*/
public function isApplicable(DataDefinitionInterface $definition) {
if (is_subclass_of($definition
->getClass(), StringInterface::class)) {
$result = TRUE;
// Never use textarea for editing dates, durations, e-mail or URIs.
$classes = [
DateTimeInterface::class,
DurationInterface::class,
Email::class,
UriInterface::class,
];
foreach ($classes as $class) {
$result = $result && !is_subclass_of($definition
->getClass(), $class) && $definition
->getClass() != $class;
}
return $result;
}
else {
return FALSE;
}
}
/**
* {@inheritdoc}
*/
public function form(TypedDataInterface $data, SubformStateInterface $form_state) {
$form = SubformState::getNewSubForm();
$form['value'] = [
'#type' => 'textarea',
'#title' => $this->configuration['label'] ?: $data
->getDataDefinition()
->getLabel(),
'#description' => $this->configuration['description'] ?: $data
->getDataDefinition()
->getDescription(),
'#default_value' => $data
->getValue(),
'#placeholder' => $this->configuration['placeholder'],
'#rows' => $this->configuration['rows'],
'#cols' => $this->configuration['cols'],
'#resizable' => $this->configuration['resizable'],
'#required' => $data
->getDataDefinition()
->isRequired(),
'#disabled' => $data
->getDataDefinition()
->isReadOnly(),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function extractFormValues(TypedDataInterface $data, SubformStateInterface $form_state) {
// Ensure empty values correctly end up as NULL value.
$value = $form_state
->getValue('value');
if ($value === '') {
$value = NULL;
}
$data
->setValue($value);
}
/**
* {@inheritdoc}
*/
public function flagViolations(TypedDataInterface $data, ConstraintViolationListInterface $violations, SubformStateInterface $formState) {
foreach ($violations as $violation) {
/** @var ConstraintViolationInterface $violation */
$formState
->setErrorByName('value', $violation
->getMessage());
}
}
/**
* {@inheritdoc}
*/
public function getConfigurationDefinitions(DataDefinitionInterface $definition) {
return [
'label' => DataDefinition::create('string')
->setLabel($this
->t('Label')),
'description' => DataDefinition::create('string')
->setLabel($this
->t('Description')),
'placeholder' => DataDefinition::create('string')
->setLabel($this
->t('Placeholder value')),
'rows' => DataDefinition::create('integer')
->setLabel($this
->t('Number of rows in the text box')),
'cols' => DataDefinition::create('integer')
->setLabel($this
->t('Number of columns in the text box')),
'resizable' => DataDefinition::create('string')
->setLabel($this
->t('Controls whether the text area is resizable'))
->setDescription($this
->t('Allowed values are "none", "vertical", "horizontal", or "both".')),
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormWidgetBase:: |
protected | property | The typed data plugin manager. | |
FormWidgetBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
FormWidgetBase:: |
public | function | Create a default DrupalDateTime object. | |
FormWidgetBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
FormWidgetBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
FormWidgetBase:: |
public | function |
Constructs a FormWidgetBase object. Overrides PluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TextareaWidget:: |
public | function |
Gets default configuration for this plugin. Overrides FormWidgetBase:: |
|
TextareaWidget:: |
public | function |
Extracts the data value from submitted form values. Overrides FormWidgetInterface:: |
|
TextareaWidget:: |
public | function |
Reports validation errors against actual form elements. Overrides FormWidgetInterface:: |
|
TextareaWidget:: |
public | function |
Creates the widget's form elements for editing the given data. Overrides FormWidgetInterface:: |
|
TextareaWidget:: |
public | function |
Defines the supported configuration settings. Overrides FormWidgetInterface:: |
|
TextareaWidget:: |
public | function |
Returns if the widget can be used for the provided data. Overrides FormWidgetInterface:: |