You are here

public function WebformEntityStorage::getOptions in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/WebformEntityStorage.php \Drupal\webform\WebformEntityStorage::getOptions()

Get all webforms grouped by category.

Parameters

null|bool $template: If TRUE only template categories will be returned. If FALSE only webform categories will be returned. If NULL all categories will be returned.

Return value

string[] An array of options grouped by category.

Overrides WebformEntityStorageInterface::getOptions

File

src/WebformEntityStorage.php, line 214

Class

WebformEntityStorage
Storage controller class for "webform" configuration entities.

Namespace

Drupal\webform

Code

public function getOptions($template = NULL) {

  /** @var \Drupal\webform\WebformInterface[] $webforms */
  $webforms = $this
    ->loadMultiple();
  @uasort($webforms, [
    $this->entityType
      ->getClass(),
    'sort',
  ]);
  $uncategorized_options = [];
  $categorized_options = [];
  foreach ($webforms as $id => $webform) {

    // Skip templates.
    if ($template !== NULL && $webform
      ->get('template') !== $template) {
      continue;
    }

    // Skip archived.
    if ($webform
      ->isArchived()) {
      continue;
    }
    if ($category = $webform
      ->get('category')) {
      $categorized_options[$category][$id] = $webform
        ->label();
    }
    else {
      $uncategorized_options[$id] = $webform
        ->label();
    }
  }

  // Merge uncategorized options with categorized options.
  $options = $uncategorized_options;
  foreach ($categorized_options as $optgroup => $optgroup_options) {

    // If webform id and optgroup conflict move the webform into the optgroup.
    if (isset($options[$optgroup])) {
      $options[$optgroup] = [
        $optgroup => $options[$optgroup],
      ] + $optgroup_options;
      asort($options[$optgroup]);
    }
    else {
      $options[$optgroup] = $optgroup_options;
    }
  }
  return $options;
}