You are here

public static function WebformExcludedElements::getWebformExcludedOptions in Webform 6.x

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

Get options for excluded tableselect element.

Parameters

array $element: An associative array containing the properties and children of the generic element element.

Return value

array An array of options containing title, name, and type of items for a tableselect element.

Overrides WebformExcludedBase::getWebformExcludedOptions

File

src/Element/WebformExcludedElements.php, line 65

Class

WebformExcludedElements
Provides a webform element for webform excluded elements.

Namespace

Drupal\webform\Element

Code

public static function getWebformExcludedOptions(array $element) {

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

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = WebformEntity::load($element['#webform_id']) ?: \Drupal::service('webform.request')
    ->getCurrentWebform();
  $options = [];
  if ($element['#exclude_markup']) {
    $form_elements = $webform
      ->getElementsInitializedFlattenedAndHasValue();
  }
  else {
    $form_elements = $webform
      ->getElementsInitializedAndFlattened();
  }
  foreach ($form_elements as $key => $form_element) {
    $form_element_plugin = $element_manager
      ->getElementInstance($form_element);

    // Skip markup elements that are containers or actions.
    if (!$element['#exclude_markup'] && ($form_element_plugin
      ->isContainer($form_element) || $form_element_plugin instanceof WebformActionsWebformElement)) {
      continue;
    }
    if (!empty($form_element['#access_view_roles'])) {
      $roles = array_map(function ($item) {
        return $item
          ->label();
      }, Role::loadMultiple($form_element['#access_view_roles']));
    }
    else {
      $roles = [];
    }
    $options[$key] = [
      'title' => $form_element['#admin_title'] ?: $form_element['#title'] ?: $key,
      'key' => $key,
      'type' => isset($form_element['#type']) ? $form_element['#type'] : '',
      'private' => empty($form_element['#private']) ? t('No') : t('Yes'),
      'access' => $roles ? WebformArrayHelper::toString($roles) : t('All roles'),
    ];
    if (!empty($form_element['#private']) || $roles) {
      $options[$key]['#attributes']['class'][] = 'color-warning';
    }
  }
  return $options;
}