You are here

protected function WebformSubmissionForm::displayCurrentPage in Webform 8.5

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

Set webform wizard current page.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

1 call to WebformSubmissionForm::displayCurrentPage()
WebformSubmissionForm::form in src/WebformSubmissionForm.php
Gets the actual form array to be built.

File

src/WebformSubmissionForm.php, line 2312

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

protected function displayCurrentPage(array &$form, FormStateInterface $form_state) {
  $current_page = $this
    ->getCurrentPage($form, $form_state);
  if ($current_page === WebformInterface::PAGE_PREVIEW) {

    // Hide all elements except 'webform_actions'.
    foreach ($form['elements'] as $element_key => $element) {
      if (isset($element['#type']) && $element['#type'] === 'webform_actions') {
        continue;
      }

      // Set #access to FALSE which will suppresses webform #required validation.
      if (Element::child($element_key) && is_array($form['elements'])) {
        WebformElementHelper::setPropertyRecursive($form['elements'][$element_key], '#access', FALSE);
      }
    }

    // Display preview message.
    $this
      ->getMessageManager()
      ->display(WebformMessageManagerInterface::FORM_PREVIEW_MESSAGE, 'warning');

    // Build preview.
    $preview_attributes = new Attribute($this
      ->getWebform()
      ->getSetting('preview_attributes'));
    $preview_attributes
      ->addClass('webform-preview');
    $form['#title'] = PlainTextOutput::renderFromHtml($this
      ->getWebformSetting('preview_title'));
    $form['preview'] = [
      '#type' => 'container',
      '#attributes' => $preview_attributes,
      // Progress bar is -20.
      '#weight' => -10,
      'submission' => $this->entityTypeManager
        ->getViewBuilder('webform_submission')
        ->view($this->entity, 'preview'),
    ];
  }
  else {

    // Get all pages so that we can also hide skipped pages.
    $pages = $this
      ->getWebform()
      ->getPages($this->operation);
    foreach ($pages as $page_key => $page) {
      if (isset($form['elements'][$page_key])) {
        $page_element =& $form['elements'][$page_key];
        $page_element_plugin = $this->elementManager
          ->getElementInstance($page_element);
        if ($page_element_plugin instanceof WebformElementWizardPageInterface) {
          if ($page_key != $current_page) {
            $page_element_plugin
              ->hidePage($page_element);
          }
          else {
            $page_element_plugin
              ->showPage($page_element);
          }
        }
      }
    }
  }
}