You are here

protected function WebformSubmissionForm::pagesElement in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformSubmissionForm.php \Drupal\webform\WebformSubmissionForm::pagesElement()

Returns the wizard page submit buttons for the current entity form.

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

File

src/WebformSubmissionForm.php, line 1223

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

protected function pagesElement(array $form, FormStateInterface $form_state) {
  $pages = $this
    ->getPages($form, $form_state);
  if (!$pages) {
    return NULL;
  }
  $current_page_name = $this
    ->getCurrentPage($form, $form_state);
  if (!$this
    ->getWebformSetting('wizard_progress_link') && !($this
    ->getWebformSetting('wizard_preview_link') && $current_page_name === WebformInterface::PAGE_PREVIEW)) {
    return NULL;
  }
  $page_indexes = array_flip(array_keys($pages));
  $current_index = $page_indexes[$current_page_name] - 1;

  // Build dedicated actions element for pages links.
  $element = [
    '#type' => 'actions',
    '#weight' => -20,
    '#attributes' => [
      'class' => [
        'webform-wizard-pages-links',
        'js-webform-wizard-pages-links',
      ],
    ],
    // Only process the container and prevent .form-actions from being added
    // which force submit buttons to be rendered in dialogs.
    // @see \Drupal\Core\Render\Element\Actions
    // @see Drupal.behaviors.dialog.prepareDialogButtons
    '#process' => [
      [
        '\\Drupal\\Core\\Render\\Element\\Actions',
        'processContainer',
      ],
    ],
  ];
  if ($this
    ->getWebformSetting('wizard_progress_link')) {
    $element['#attributes']['data-wizard-progress-link'] = 'true';
  }
  if ($this
    ->getWebformSetting('wizard_preview_link')) {
    $element['#attributes']['data-wizard-preview-link'] = 'true';
  }
  $index = 1;
  $total = count($pages);
  foreach ($pages as $page_name => $page) {

    // Always include submit button for each page but only allows access
    // to previous and visible pages.
    //
    // Developers who want to allow users to jump to any wizard page can
    // expose these buttons via a form alter hook. Beware that
    // skipped pages will not be validated.
    $access = $page['#access'] && $page_indexes[$page_name] <= $current_index ? TRUE : FALSE;
    $t_args = [
      '@label' => $page['#title'],
      '@start' => $index++,
      '@end' => $total,
    ];
    $element[$page_name] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Edit'),
      '#page' => $page_name,
      '#validate' => [
        '::noValidate',
      ],
      '#submit' => [
        '::gotoPage',
      ],
      '#name' => 'webform_wizard_page-' . $page_name,
      '#attributes' => [
        'data-webform-page' => $page_name,
        'formnovalidate' => 'formnovalidate',
        'class' => [
          'webform-wizard-pages-link',
          'js-webform-wizard-pages-link',
        ],
        'title' => $this
          ->t("Edit '@label' (@start of @end)", $t_args),
      ],
      '#access' => $access,
    ];
  }
  $element['#attached']['library'][] = 'webform/webform.wizard.pages';
  return $element;
}