You are here

protected function WebformSubmissionForm::setConfirmation in Webform 8.5

Same name and namespace in other branches
  1. 6.x 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 2386

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:
      $confirmation_url = trim($this
        ->getWebformSetting('confirmation_url', ''));

      // Remove base path from root-relative URL.
      // Only applies for Drupal sites within a sub directory.
      $confirmation_url = preg_replace('/^' . preg_quote(base_path(), '/') . '/', '/', $confirmation_url);

      // Get system path.
      $confirmation_url = $this->aliasManager
        ->getPathByAlias($confirmation_url);

      // Get redirect URL if internal or valid.
      if (strpos($confirmation_url, 'internal:') === 0) {
        $redirect_url = Url::fromUri($confirmation_url);
      }
      else {
        $redirect_url = $this->pathValidator
          ->getUrlIfValid($confirmation_url);
      }
      if ($redirect_url) {
        if ($confirmation_type === WebformInterface::CONFIRMATION_URL_MESSAGE) {
          $this
            ->getMessageManager()
            ->display(WebformMessageManagerInterface::SUBMISSION_CONFIRMATION_MESSAGE);
        }
        $this
          ->setTrustedRedirectUrl($form_state, $redirect_url);
        return;
      }
      else {
        $t_args = [
          '@webform' => $webform
            ->label(),
          '%url' => $this
            ->getWebformSetting('confirmation_url'),
        ];

        // Display warning to use who can update the webform.
        if ($webform
          ->access('update')) {
          $this
            ->messenger()
            ->addWarning($this
            ->t('Confirmation URL %url is not valid.', $t_args));
        }

        // Log warning.
        $this
          ->getLogger('webform')
          ->warning('@webform: Confirmation URL %url is not valid.', $t_args);
      }

      // If confirmation URL is invalid display message.
      $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;
  }
}