You are here

public static function WebformGroupRoles::getGroupRolesOptions in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_group/src/Element/WebformGroupRoles.php \Drupal\webform_group\Element\WebformGroupRoles::getGroupRolesOptions()

Get group roles options for an element.

Parameters

array $element: An element.

Return value

array Group roles options for an element.

2 calls to WebformGroupRoles::getGroupRolesOptions()
WebformGroupRoles::processSelect in modules/webform_group/src/Element/WebformGroupRoles.php
Processes a webform roles (checkboxes) element.
WebformGroupRolesElementTest::testGroupRolesElement in modules/webform_group/tests/src/Functional/WebformGroupRolesElementTest.php
Tests webform group roles element.

File

modules/webform_group/src/Element/WebformGroupRoles.php, line 70

Class

WebformGroupRoles
Provides a webform group roles element.

Namespace

Drupal\webform_group\Element

Code

public static function getGroupRolesOptions(array $element) {
  $element += [
    '#include_internal' => TRUE,
    '#include_user_roles' => FALSE,
    '#include_anonymous' => FALSE,
    '#include_outsider' => TRUE,
  ];

  /** @var \Drupal\group\Entity\GroupRoleInterface[] $group_roles */
  $group_roles = GroupRole::loadMultiple();
  $group_role_names = [];
  $options = [];
  foreach ($group_roles as $group_role) {
    if (!$element['#include_internal'] && $group_role
      ->isInternal()) {
      continue;
    }
    if (!$element['#include_user_roles'] && !$group_role
      ->inPermissionsUI()) {
      continue;
    }
    if (!$element['#include_anonymous'] && $group_role
      ->isAnonymous()) {
      continue;
    }
    if (!$element['#include_outsider'] && $group_role
      ->isOutsider()) {
      continue;
    }
    $group_role_id = $group_role
      ->id();
    $group_role_label = $group_role
      ->label();
    $group_type = $group_role
      ->getGroupType();
    $group_type_id = $group_type
      ->id();
    $group_type_label = $group_type
      ->label();
    $t_args = [
      '@group_type' => $group_type_label,
      '@group_role' => $group_role_label,
    ];
    $options[$group_type_label][$group_role_id] = t('@group_type: @group_role', $t_args);
    $group_role_name = preg_replace("/^{$group_type_id}-/", "", $group_role_id);
    $group_role_names[$group_role_name] = $group_role_label;
  }
  ksort($options);
  return [
    (string) t('Group role types') => $group_role_names,
  ] + $options;
}