You are here

protected function WebformSubmissionForm::actions in Webform 6.x

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

Returns an array of supported actions for the current entity form.

This function generates a list of Form API elements which represent actions supported by the current entity form.

@todo Consider introducing a 'preview' action here, since it is used by many entity types.

Parameters

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

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

Return value

array An array of supported Form API action elements keyed by name.

Overrides EntityForm::actions

File

src/WebformSubmissionForm.php, line 1315

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

protected function actions(array $form, FormStateInterface $form_state) {

  /* @var $webform_submission \Drupal\webform\WebformSubmissionInterface */
  $webform_submission = $this->entity;
  $element = parent::actions($form, $form_state);

  /* @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $preview_mode = $this
    ->getWebformSetting('preview');

  // Mark the submit action as the primary action, when it appears.
  $element['submit']['#button_type'] = 'primary';
  $element['submit']['#attributes']['class'][] = 'webform-button--submit';
  $element['submit']['#weight'] = 10;

  // Customize the submit button's label for new submissions only.
  if ($webform_submission
    ->isNew() || $webform_submission
    ->isDraft()) {
    $element['submit']['#value'] = $this
      ->config('webform.settings')
      ->get('settings.default_submit_button_label');
  }

  // Add validate and complete handler to submit.
  $element['submit']['#validate'][] = '::validateForm';
  $element['submit']['#validate'][] = '::autosave';
  $element['submit']['#validate'][] = '::complete';

  // Add confirm(ation) handler to submit button.
  $element['submit']['#submit'][] = '::confirmForm';

  // Hide the delete button and move it last.
  if (isset($element['delete'])) {
    $element['delete']['#access'] = FALSE;
    $element['delete']['#title'] = $this
      ->config('webform.settings')
      ->get('settings.default_delete_button_label');

    // Redirect to the 'add' submission form when this submission is deleted.
    if ($this->operation === 'add') {
      $element['delete']['#url']
        ->mergeOptions([
        'query' => $this
          ->getRedirectDestination()
          ->getAsArray(),
      ]);
    }
    $element['delete']['#weight'] = 20;
  }
  $pages = $this
    ->getPages($form, $form_state);
  $current_page = $this
    ->getCurrentPage($form, $form_state);
  if ($pages) {

    // Get current page element which can contain custom prev(ious) and next button
    // labels.
    $current_page_element = $this
      ->getWebform()
      ->getPage($this->operation, $current_page);
    $previous_page = $this
      ->getPreviousPage($pages, $current_page);
    $next_page = $this
      ->getNextPage($pages, $current_page);

    // Track previous and next page.
    $track = $this
      ->getWebform()
      ->getSetting('wizard_track');
    switch ($track) {
      case 'index':
        $track_pages = array_flip(array_keys($pages));
        $track_previous_page = $previous_page ? $track_pages[$previous_page] + 1 : NULL;
        $track_next_page = $next_page ? $track_pages[$next_page] + 1 : NULL;
        $track_last_page = $this
          ->getWebform()
          ->getSetting('wizard_confirmation') ? count($track_pages) : count($track_pages) + 1;
        break;
      default:
      case 'name':
        $track_previous_page = $previous_page;
        $track_next_page = $next_page;
        $track_last_page = WebformInterface::PAGE_CONFIRMATION;
        break;
    }
    $is_first_page = $current_page === $this
      ->getFirstPage($pages) ? TRUE : FALSE;
    $is_last_page = in_array($current_page, [
      WebformInterface::PAGE_PREVIEW,
      WebformInterface::PAGE_CONFIRMATION,
      $this
        ->getLastPage($pages),
    ]) ? TRUE : FALSE;
    $is_preview_page = $current_page === WebformInterface::PAGE_PREVIEW;
    $is_next_page_preview = $next_page === WebformInterface::PAGE_PREVIEW ? TRUE : FALSE;
    $is_next_page_complete = $next_page === WebformInterface::PAGE_CONFIRMATION ? TRUE : FALSE;
    $is_next_page_optional_preview = $is_next_page_preview && $preview_mode !== DRUPAL_REQUIRED;

    // Only show that save button if this is the last page of the wizard or
    // on preview page or right before the optional preview.
    $element['submit']['#access'] = $is_last_page || $is_preview_page || $is_next_page_optional_preview || $is_next_page_complete;

    // Use next page submit callback to make sure conditional page logic
    // is executed.
    $element['submit']['#submit'] = [
      '::submit',
    ];
    if ($track) {
      $element['submit']['#attributes']['data-webform-wizard-page'] = $track_last_page;
    }
    if (!$is_first_page) {
      if ($is_preview_page) {
        $element['preview_prev'] = [
          '#type' => 'submit',
          '#value' => $this
            ->config('webform.settings')
            ->get('settings.default_preview_prev_button_label'),
          // @see \Drupal\webform\WebformSubmissionForm::noValidate
          '#validate' => [
            '::noValidate',
          ],
          '#submit' => [
            '::previous',
          ],
          '#attributes' => [
            'formnovalidate' => 'formnovalidate',
            'class' => [
              'webform-button--previous',
            ],
          ],
          '#weight' => 0,
        ];
        if ($track) {
          $element['preview_prev']['#attributes']['data-webform-wizard-page'] = $track_previous_page;
        }
      }
      else {
        if (isset($current_page_element['#prev_button_label'])) {
          $previous_button_label = $current_page_element['#prev_button_label'];
          $previous_button_custom = TRUE;
        }
        else {
          $previous_button_label = $this
            ->config('webform.settings')
            ->get('settings.default_wizard_prev_button_label');
          $previous_button_custom = FALSE;
        }
        $element['wizard_prev'] = [
          '#type' => 'submit',
          '#value' => $previous_button_label,
          '#webform_actions_button_custom' => $previous_button_custom,
          // @see \Drupal\webform\WebformSubmissionForm::noValidate
          '#validate' => [
            '::noValidate',
          ],
          '#submit' => [
            '::previous',
          ],
          '#attributes' => [
            'formnovalidate' => 'formnovalidate',
            'class' => [
              'webform-button--previous',
            ],
          ],
          '#weight' => 0,
        ];
        if ($track) {
          $element['wizard_prev']['#attributes']['data-webform-wizard-page'] = $track_previous_page;
        }
      }
    }
    if (!$is_last_page && !$is_next_page_complete) {
      if ($is_next_page_preview) {
        $element['preview_next'] = [
          '#type' => 'submit',
          '#value' => $this
            ->config('webform.settings')
            ->get('settings.default_preview_next_button_label'),
          '#validate' => [
            '::validateForm',
          ],
          '#submit' => [
            '::next',
          ],
          '#attributes' => [
            'class' => [
              'webform-button--preview',
            ],
          ],
          '#weight' => 1,
        ];
        if ($track) {
          $element['preview_next']['#attributes']['data-webform-wizard-page'] = $track_next_page;
        }
      }
      else {
        if (isset($current_page_element['#next_button_label'])) {
          $next_button_label = $current_page_element['#next_button_label'];
          $next_button_custom = TRUE;
        }
        else {
          $next_button_label = $this
            ->config('webform.settings')
            ->get('settings.default_wizard_next_button_label');
          $next_button_custom = FALSE;
        }
        $element['wizard_next'] = [
          '#type' => 'submit',
          '#value' => $next_button_label,
          '#webform_actions_button_custom' => $next_button_custom,
          '#validate' => [
            '::validateForm',
          ],
          '#submit' => [
            '::next',
          ],
          '#attributes' => [
            'class' => [
              'webform-button--next',
            ],
          ],
          '#weight' => 1,
        ];
        if ($track) {
          $element['wizard_next']['#attributes']['data-webform-wizard-page'] = $track_next_page;
        }
      }
    }
    if ($track) {
      $element['#attached']['library'][] = 'webform/webform.wizard.track';
    }
  }

  // Draft.
  if ($this
    ->draftEnabled()) {
    $element['draft'] = [
      '#type' => 'submit',
      '#value' => $this
        ->config('webform.settings')
        ->get('settings.default_draft_button_label'),
      '#validate' => [
        '::draft',
      ],
      '#submit' => [
        '::submitForm',
        '::save',
        '::rebuild',
      ],
      '#attributes' => [
        'formnovalidate' => 'formnovalidate',
        'class' => [
          'webform-button--draft',
        ],
      ],
      '#weight' => -10,
    ];
  }

  // Reset.
  if ($this
    ->resetEnabled()) {
    $element['reset'] = [
      '#type' => 'submit',
      '#value' => $this
        ->config('webform.settings')
        ->get('settings.default_reset_button_label'),
      '#validate' => [
        '::noValidate',
      ],
      '#submit' => [
        '::reset',
      ],
      '#attributes' => [
        'formnovalidate' => 'formnovalidate',
        'class' => [
          'webform-button--reset',
        ],
      ],
      '#weight' => 10,
    ];
  }
  uasort($element, [
    'Drupal\\Component\\Utility\\SortArray',
    'sortByWeightProperty',
  ]);
  return $element;
}