class Textarea extends FormElementBase (View source)

Defines the textarea element for the configuration translation interface.

Traits

Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.

Properties

protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected TypedDataInterface $element

The schema element this form is for.

from  FormElementBase
protected DataDefinitionInterface $definition

The data definition of the element this form element is for.

from  FormElementBase

Methods

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

getStringTranslation()

Gets the string translation service.

$this
setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

__construct(TypedDataInterface $element)

Constructs a FormElementBase.

create(TypedDataInterface $schema)

Creates a form element instance from a schema definition.

array
getTranslationBuild(LanguageInterface $source_language, LanguageInterface $translation_language, mixed $source_config, mixed $translation_config, array $parents, string|null $base_key = NULL)

Builds a render array containing the source and translation form elements.

array
getSourceElement(LanguageInterface $source_language, mixed $source_config)

Returns the source element for a given configuration definition.

array
getTranslationElement(LanguageInterface $translation_language, mixed $source_config, mixed $translation_config)

Returns the translation form element for a given configuration definition.

setConfig(Config $base_config, LanguageConfigOverride $config_translation, mixed $config_values, string|null $base_key = NULL)

Sets configuration based on a nested form value array.

Details

protected TranslatableMarkup t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

__construct(TypedDataInterface $element)

Constructs a FormElementBase.

Parameters

TypedDataInterface $element

The schema element this form element is for.

static ElementInterface create(TypedDataInterface $schema)

Creates a form element instance from a schema definition.

Parameters

TypedDataInterface $schema

The configuration schema.

Return Value

ElementInterface

array getTranslationBuild(LanguageInterface $source_language, LanguageInterface $translation_language, mixed $source_config, mixed $translation_config, array $parents, string|null $base_key = NULL)

Builds a render array containing the source and translation form elements.

Parameters

LanguageInterface $source_language

The source language of the configuration object.

LanguageInterface $translation_language

The language to display the translation form for.

mixed $source_config

The configuration value of the element in the source language.

mixed $translation_config

The configuration value of the element in the language to translate to.

array $parents

Parents array for the element in the form.

string|null $base_key

(optional) Base key to be used for the elements in the form. NULL for top-level form elements.

Return Value

array

A render array consisting of the source and translation elements for the source value.

protected array getSourceElement(LanguageInterface $source_language, mixed $source_config)

Returns the source element for a given configuration definition.

This can be either a render array that actually outputs the source values directly or a read-only form element with the source values depending on what is considered to provide a more intuitive user interface for the translator.

Parameters

LanguageInterface $source_language

Thee source language of the configuration object.

mixed $source_config

The configuration value of the element in the source language.

Return Value

array

A render array for the source value.

array getTranslationElement(LanguageInterface $translation_language, mixed $source_config, mixed $translation_config)

Returns the translation form element for a given configuration definition.

For complex data structures (such as mappings) that are translatable wholesale but contain non-translatable properties, the form element is responsible for checking access to the source value of those properties. In case of formatted text, for example, access to the source text format must be checked. If the translator does not have access to the text format, the textarea must be disabled and the translator may not be able to translate this particular configuration element. If the translator does have access to the text format, the element must be locked down to that particular text format; in other words, the format may not be changed by the translator (because the text format property is not itself translatable).

In addition, the form element is responsible for checking whether the value of such non-translatable properties in the translated configuration is equal to the corresponding source values. If not, that means that the source value has changed after the translation was added. In this case - again - the translation of this element must be disabled if the translator does not have access to the source value of the non-translatable property. For example, if a formatted text element, whose source format was plain text when it was first translated, gets changed to the Full HTML format, simply changing the format of the translation would lead to an XSS vulnerability as the translated text, that was intended to be escaped, would now be displayed unescaped. Thus, if the translator does not have access to the Full HTML format, the translation for this particular element may not be updated at all (the textarea must be disabled). Only if access to the Full HTML format is granted, an explicit translation taking into account the updated source value(s) may be submitted.

In the specific case of formatted text this logic is implemented by utilizing a form element of type 'text_format' and its #format and

allowed_formats properties. The access logic explained above is then

handled by the 'text_format' element itself, specifically by \Drupal\filter\Element\TextFormat::processFormat(). In case such a rich element is not available for translation of complex data, similar access logic must be implemented manually.

Parameters

LanguageInterface $translation_language

The language to display the translation form for.

mixed $source_config

The configuration value of the element in the source language.

mixed $translation_config

The configuration value of the element in the language to translate to.

Return Value

array

Form API array to represent the form element.

setConfig(Config $base_config, LanguageConfigOverride $config_translation, mixed $config_values, string|null $base_key = NULL)

Sets configuration based on a nested form value array.

If the configuration values are the same as the source configuration, the override should be removed from the translation configuration.

Parameters

Config $base_config

Base configuration values, in the source language.

LanguageConfigOverride $config_translation

Translation configuration override data.

mixed $config_values

The configuration value of the element taken from the form values.

string|null $base_key

(optional) The base key that the schema and the configuration values belong to. This should be NULL for the top-level configuration object and be populated consecutively when recursing into the configuration structure.