You are here

protected function Webform::buildPages in Webform 8.5

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

Build and cache a webform's wizard pages based on the current operation.

Parameters

string $operation: The webform submission operation. Usually 'default', 'add', 'edit', 'edit_all', 'api', or 'test'.

Return value

array An associative array of webform wizard pages.

See also

\Drupal\webform_cards\WebformCardsManager::buildPages

1 call to Webform::buildPages()
Webform::getPages in src/Entity/Webform.php
Get webform wizard pages.

File

src/Entity/Webform.php, line 2032

Class

Webform
Defines the webform entity.

Namespace

Drupal\webform\Entity

Code

protected function buildPages($operation = 'default') {
  if (isset($this->pages[$operation])) {
    return $this->pages[$operation];
  }

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');
  $wizard_properties = [
    '#title' => '#title',
    '#prev_button_label' => '#prev_button_label',
    '#next_button_label' => '#next_button_label',
    '#states' => '#states',
  ];
  $pages = [];

  // Add webform wizard pages.
  $elements = $this
    ->getElementsInitialized();
  if (is_array($elements) && !in_array($operation, [
    'edit_all',
    'api',
  ])) {
    foreach ($elements as $key => $element) {
      if (!isset($element['#type'])) {
        continue;
      }

      /** @var \Drupal\webform\Plugin\WebformElementInterface $element_plugin */
      $element_plugin = $element_manager
        ->getElementInstance($element, $this);
      if (!$element_plugin instanceof WebformElementWizardPageInterface) {
        continue;
      }

      // Check element access rules and only include pages that are visible
      // to the current user.
      $access_operation = in_array($operation, [
        'default',
        'add',
      ]) ? 'create' : 'update';
      if ($element_plugin
        ->checkAccessRules($access_operation, $element)) {
        $pages[$key] = array_intersect_key($element, $wizard_properties) + [
          '#type' => 'page',
          '#access' => TRUE,
        ];
      }
    }
  }

  // Add preview page.
  $settings = $this
    ->getSettings();
  if ((int) $settings['preview'] !== DRUPAL_DISABLED) {

    // If there is no start page, we must define one.
    if (empty($pages)) {
      $pages[WebformInterface::PAGE_START] = [
        '#title' => $this
          ->getSetting('wizard_start_label', TRUE),
        '#type' => 'page',
        '#access' => TRUE,
      ];
    }
    $pages[WebformInterface::PAGE_PREVIEW] = [
      '#title' => $this
        ->getSetting('preview_label', TRUE),
      '#type' => 'page',
      '#access' => TRUE,
    ];
  }

  // Only add complete page, if there are some pages.
  if ($pages && $this
    ->getSetting('wizard_confirmation')) {
    $pages[WebformInterface::PAGE_CONFIRMATION] = [
      '#title' => $this
        ->getSetting('wizard_confirmation_label', TRUE),
      '#type' => 'page',
      '#access' => TRUE,
    ];
  }
  $this->pages[$operation] = $pages;
  return $this->pages[$operation];
}