You are here

protected static function WebformTermReferenceTrait::getOptionsTree in Webform 6.x

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

Get options to term tree.

Parameters

array $element: The term reference element.

string $language: The language to be displayed.

Return value

array An associative array of term options formatted as a tree.

1 call to WebformTermReferenceTrait::getOptionsTree()
WebformTermReferenceTrait::setOptions in src/Element/WebformTermReferenceTrait.php
Set referencable term entities as options for an element.
1 method overrides WebformTermReferenceTrait::getOptionsTree()
WebformTermCheckboxes::getOptionsTree in src/Element/WebformTermCheckboxes.php
Get options to term tree.

File

src/Element/WebformTermReferenceTrait.php, line 92

Class

WebformTermReferenceTrait
Trait for term reference elements.

Namespace

Drupal\webform\Element

Code

protected static function getOptionsTree(array $element, $language) {
  $element += [
    '#tree_delimiter' => '-',
  ];

  /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
  $entity_repository = \Drupal::service('entity.repository');
  $tree = static::loadTree($element['#vocabulary']);
  $options = [];
  foreach ($tree as $item) {

    // Set the item in the correct language for display.
    $item = $entity_repository
      ->getTranslationFromContext($item);
    if (!$item
      ->access('view')) {
      continue;
    }

    // Check depth.
    if (!empty($element['#depth']) && $item->depth >= $element['#depth']) {
      continue;
    }
    $options[$item
      ->id()] = str_repeat($element['#tree_delimiter'], $item->depth) . $item
      ->getName();
  }
  return $options;
}