You are here

protected function WebformSubmissionForm::setConfirmation in Webform 6.x

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

Set webform state confirmation redirect and message.

Parameters

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

2 calls to WebformSubmissionForm::setConfirmation()
WebformSubmissionDuplicateForm::setConfirmation in src/WebformSubmissionDuplicateForm.php
Set webform state confirmation redirect and message.
WebformSubmissionForm::confirmForm in src/WebformSubmissionForm.php
Webform confirm(ation) handler.
1 method overrides WebformSubmissionForm::setConfirmation()
WebformSubmissionDuplicateForm::setConfirmation in src/WebformSubmissionDuplicateForm.php
Set webform state confirmation redirect and message.

File

src/WebformSubmissionForm.php, line 2303

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

protected function setConfirmation(FormStateInterface $form_state) {

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $this
    ->getEntity();
  $webform = $webform_submission
    ->getWebform();

  // Get current route name, parameters, and options.
  $route_name = $this
    ->getRouteMatch()
    ->getRouteName();
  $route_parameters = $this
    ->getRouteMatch()
    ->getRawParameters()
    ->all();
  $route_options = [];

  // Add current query to route options.
  if (!$webform
    ->getSetting('confirmation_exclude_query')) {
    $query = $this
      ->getRequest()->query
      ->all();

    // Remove Ajax parameters from query.
    unset($query['ajax_form'], $query['_wrapper_format']);
    if ($query) {
      $route_options['query'] = $query;
    }
  }

  // Default to displaying a confirmation message on this page when submission
  // is updated or locked (but not just completed).
  $state = $webform_submission
    ->getState();
  $is_updated = $state === WebformSubmissionInterface::STATE_UPDATED;
  $is_locked = $state === WebformSubmissionInterface::STATE_LOCKED && $webform_submission
    ->getChangedTime() > $webform_submission
    ->getCompletedTime();
  $confirmation_update = $this
    ->getWebformSetting('confirmation_update');
  if ($is_updated && !$confirmation_update || $is_locked) {
    $this
      ->getMessageManager()
      ->display(WebformMessageManagerInterface::SUBMISSION_UPDATED);
    $form_state
      ->set('current_page', NULL);
    $form_state
      ->setRedirect($route_name, $route_parameters, $route_options);
    return;
  }

  // Add token route query options.
  if ($state === WebformSubmissionInterface::STATE_COMPLETED && !$webform
    ->getSetting('confirmation_exclude_token')) {
    $route_options['query']['token'] = $webform_submission
      ->getToken();
  }

  // Handle 'page', 'url', and 'inline' confirmation types.
  $confirmation_type = $this
    ->getWebformSetting('confirmation_type');
  switch ($confirmation_type) {
    case WebformInterface::CONFIRMATION_PAGE:
      $redirect_url = $this->requestHandler
        ->getUrl($webform, $this->sourceEntity, 'webform.confirmation', $route_options);
      $form_state
        ->setRedirectUrl($redirect_url);
      return;
    case WebformInterface::CONFIRMATION_URL:
    case WebformInterface::CONFIRMATION_URL_MESSAGE:
      $redirect_url = $this
        ->getConfirmationUrl();
      if ($redirect_url) {
        if ($confirmation_type === WebformInterface::CONFIRMATION_URL_MESSAGE) {
          $this
            ->getMessageManager()
            ->display(WebformMessageManagerInterface::SUBMISSION_CONFIRMATION_MESSAGE);
        }
        $this
          ->setTrustedRedirectUrl($form_state, $redirect_url);
      }
      else {
        $this
          ->getMessageManager()
          ->display(WebformMessageManagerInterface::SUBMISSION_CONFIRMATION_MESSAGE);
        $route_options['query']['webform_id'] = $webform
          ->id();
        $form_state
          ->setRedirect($route_name, $route_parameters, $route_options);
      }
      return;
    case WebformInterface::CONFIRMATION_INLINE:
      $form_state
        ->set('current_page', WebformInterface::PAGE_CONFIRMATION);
      $form_state
        ->setRebuild();
      return;
    case WebformInterface::CONFIRMATION_MESSAGE:
      $this
        ->getMessageManager()
        ->display(WebformMessageManagerInterface::SUBMISSION_CONFIRMATION_MESSAGE);
      return;
    case WebformInterface::CONFIRMATION_MODAL:
      $message = $this
        ->getMessageManager()
        ->build(WebformMessageManagerInterface::SUBMISSION_CONFIRMATION_MESSAGE);
      if ($message) {

        // Set webform confirmation modal in $form_state.
        $form_state
          ->set('webform_confirmation_modal', [
          'title' => $this
            ->getWebformSetting('confirmation_title', ''),
          'content' => $message,
        ]);
      }
      return;
    case WebformInterface::CONFIRMATION_NONE:
      return;
    case WebformInterface::CONFIRMATION_DEFAULT:
    default:
      $this
        ->getMessageManager()
        ->display(WebformMessageManagerInterface::SUBMISSION_DEFAULT_CONFIRMATION);
      return;
  }
}