You are here

public static function WebformEntityTrait::setOptions in Webform 6.x

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

Set referencable entities as options for an element.

Parameters

array $element: An element.

array $settings: An array of settings used to limit and randomize options.

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 WebformEntityTrait::setOptions()
OptionsLimitWebformHandler::getElementOptions in modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php
Get selected element's options.
WebformEntityCheckboxes::processCheckboxes in src/Element/WebformEntityCheckboxes.php
Processes a checkboxes form element.
WebformEntityRadios::processRadios in src/Element/WebformEntityRadios.php
Expands a radios element into individual radio elements.
WebformEntityReferenceTrait::setOptions in src/Plugin/WebformElement/WebformEntityReferenceTrait.php
Get element options.
WebformEntitySelect::processSelect in src/Element/WebformEntitySelect.php
Processes a select list form element.

File

src/Element/WebformEntityTrait.php, line 37

Class

WebformEntityTrait
Trait for entity reference elements.

Namespace

Drupal\webform\Element

Code

public static function setOptions(array &$element, array $settings = []) {
  if (!empty($element['#options'])) {
    return;
  }

  // Make sure #target_type is not empty.
  if (empty($element['#target_type'])) {
    $element['#options'] = [];
    return;
  }
  $selection_settings = isset($element['#selection_settings']) ? $element['#selection_settings'] : [];
  $selection_handler_options = [
    'target_type' => $element['#target_type'],
    'handler' => $element['#selection_handler'],
    // Set '_webform_settings' used to limit and randomize options.
    // @see webform_query_entity_reference_alter()
    '_webform_settings' => $settings,
  ] + $selection_settings;

  // Make sure settings has a limit.
  $settings += [
    'limit' => 0,
  ];

  /** @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(NULL, 'CONTAINS', $settings['limit']);

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

  // If the selection handler is not using views, then translate
  // the entity reference's options.
  if (!\Drupal::moduleHandler()
    ->moduleExists('views') || !$handler instanceof \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection) {
    $options = static::translateOptions($options, $element);
  }
  if ($element['#type'] === 'webform_entity_select') {

    // Strip tags from options since <option> element does
    // not support HTML tags.
    $options = WebformOptionsHelper::stripTagsOptions($options);
  }
  else {

    // Only select menu can support optgroups.
    $options = OptGroup::flattenOptions($options);
  }

  // Issue #2826451: TermSelection returning HTML characters in select list.
  $options = WebformOptionsHelper::decodeOptions($options);
  $element['#options'] = $options;
}