You are here

public static function YamlFormEntityTrait::setOptions in YAML Form 8

Set referencable entities as options for an element.

Parameters

array $element: An element.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Thrown when the current user doesn't have access to the specified entity.

See also

\Drupal\system\Controller\EntityAutocompleteController

5 calls to YamlFormEntityTrait::setOptions()
YamlFormEntityCheckboxes::processCheckboxes in src/Element/YamlFormEntityCheckboxes.php
Processes a checkboxes form element.
YamlFormEntityOptionsTrait::getElementSelectorInputsOptions in src/Plugin/YamlFormElement/YamlFormEntityOptionsTrait.php
YamlFormEntityOptionsTrait::prepare in src/Plugin/YamlFormElement/YamlFormEntityOptionsTrait.php
YamlFormEntityRadios::processRadios in src/Element/YamlFormEntityRadios.php
Expands a radios element into individual radio elements.
YamlFormEntitySelect::processSelect in src/Element/YamlFormEntitySelect.php
Processes a select list form element.

File

src/Element/YamlFormEntityTrait.php, line 35

Class

YamlFormEntityTrait
Trait for entity reference elements.

Namespace

Drupal\yamlform\Element

Code

public static function setOptions(array &$element) {
  if (!empty($element['#options'])) {
    return;
  }
  $selection_handler_options = [
    'target_type' => $element['#target_type'],
    'handler' => $element['#selection_handler'],
    'handler_settings' => isset($element['#selection_settings']) ? $element['#selection_settings'] : [],
  ];

  /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selection_manager */
  $selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
  $handler = $selection_manager
    ->getInstance($selection_handler_options);
  $referenceable_entities = $handler
    ->getReferenceableEntities();

  // Flatten all bundle grouping since they are not applicable to
  // YamlFormEntity elements.
  $options = [];
  foreach ($referenceable_entities as $bundle_options) {
    $options += $bundle_options;
  }

  // Only select menu can support optgroups.
  if ($element['#type'] !== 'yamlform_entity_select') {
    $options = OptGroup::flattenOptions($options);
  }

  // Issue #2826451: TermSelection returning HTML characters in select list.
  foreach ($options as $key => $value) {
    $options[$key] = Html::decodeEntities($value);
  }
  $element['#options'] = $options;
}