You are here

protected static function WebformEntityTrait::translateOptions in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformEntityTrait.php \Drupal\webform\Element\WebformEntityTrait::translateOptions()

Translate the select options.

Parameters

array $options: Untranslated options.

array $element: An element.

Return value

array Translated options.

1 call to WebformEntityTrait::translateOptions()
WebformEntityTrait::setOptions in src/Element/WebformEntityTrait.php
Set referencable entities as options for an element.

File

src/Element/WebformEntityTrait.php, line 106

Class

WebformEntityTrait
Trait for entity reference elements.

Namespace

Drupal\webform\Element

Code

protected static function translateOptions(array $options, array $element) {

  /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
  $entity_repository = \Drupal::service('entity.repository');
  foreach ($options as $key => $value) {
    if (is_array($value)) {
      $options[$key] = static::translateOptions($value, $element);
    }
    else {

      // Set the entity in the correct language for display.
      $option = \Drupal::entityTypeManager()
        ->getStorage($element['#target_type'])
        ->load($key);
      $option = $entity_repository
        ->getTranslationFromContext($option);
      $options[$key] = $option
        ->label();
    }
  }
  return $options;
}