function ComponentMetadata::getEnumOptions

Get translated options labels from enumeration.

Parameters

string $propertyName: The enum property name.

Return value

array<string, \Drupal\Core\StringTranslation\TranslatableMarkup> An array with enum options as keys and the (non-rendered) translated labels as values.

1 call to ComponentMetadata::getEnumOptions()
ComponentMetadata::normalize in core/lib/Drupal/Core/Theme/Component/ComponentMetadata.php
Normalizes the value object.

File

core/lib/Drupal/Core/Theme/Component/ComponentMetadata.php, line 241

Class

ComponentMetadata
Component metadata.

Namespace

Drupal\Core\Theme\Component

Code

public function getEnumOptions(string $propertyName) : array {
    $options = [];
    if (isset($this->schema['properties'][$propertyName])) {
        $prop_definition = $this->schema['properties'][$propertyName];
        if (!empty($prop_definition['enum'])) {
            $translation_context = $prop_definition['x-translation-context'] ?? '';
            // We convert ['a', 'b'], into ['a' => t('a'), 'b' => t('b')].
            $options = array_combine($prop_definition['enum'], array_map(fn($value) => $this->t($value, [], [
                'context' => $translation_context,
            ]), $prop_definition['enum']));
            if (!empty($prop_definition['meta:enum'])) {
                foreach ($prop_definition['meta:enum'] as $enum_value => $enum_label) {
                    $options[$enum_value] = $this->t($enum_label, [], [
                        'context' => $translation_context,
                    ]);
                }
            }
        }
    }
    return $options;
}

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