You are here

public function Webform::getElementsSelectorOptions in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Entity/Webform.php \Drupal\webform\Entity\Webform::getElementsSelectorOptions()

Get webform element's selectors as options.

Parameters

array $options: (Optional) Options to be appled to element selectors.

Return value

array Webform elements selectors as options.

Overrides WebformInterface::getElementsSelectorOptions

File

src/Entity/Webform.php, line 1366

Class

Webform
Defines the webform entity.

Namespace

Drupal\webform\Entity

Code

public function getElementsSelectorOptions(array $options = []) {

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');

  // The value element is excluded because it is not available
  // to the #states API. The value element is available to WebformHandles.
  // @see \Drupal\webform\Form\WebformHandlerFormBase
  $options += [
    'excluded_elements' => [
      'value',
    ],
  ];
  $selectors = [];
  $elements = $this
    ->getElementsInitializedAndFlattened();
  foreach ($elements as $element) {
    $element_plugin = $element_manager
      ->getElementInstance($element, $this);

    // Check excluded elements.
    if ($options['excluded_elements'] && in_array($element_plugin
      ->getPluginId(), $options['excluded_elements'])) {
      continue;
    }
    $element_selectors = $element_plugin
      ->getElementSelectorOptions($element);
    foreach ($element_selectors as $element_selector_key => $element_selector_value) {

      // Suffix duplicate selector optgroups with empty characters.
      //
      // This prevents elements with the same titles and multiple selectors
      // from clobbering each other.
      if (isset($selectors[$element_selector_key]) && is_array($element_selector_value)) {
        while (isset($selectors[$element_selector_key])) {
          $element_selector_key .= ' ';
        }
        $selectors[$element_selector_key] = $element_selector_value;
      }
      else {
        $selectors[$element_selector_key] = $element_selector_value;
      }
    }
    $selectors += $element_plugin
      ->getElementSelectorOptions($element);
  }
  return $selectors;
}