You are here

protected function YamlFormSubmissionForm::setConfirmation in YAML Form 8

Set form state confirmation redirect and message.

Parameters

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

1 call to YamlFormSubmissionForm::setConfirmation()
YamlFormSubmissionForm::confirmForm in src/YamlFormSubmissionForm.php
Form confirm(ation) handler.

File

src/YamlFormSubmissionForm.php, line 1068

Class

YamlFormSubmissionForm
Provides a form to collect and edit submissions.

Namespace

Drupal\yamlform

Code

protected function setConfirmation(FormStateInterface $form_state) {

  /** @var \Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission */
  $yamlform_submission = $this
    ->getEntity();
  $yamlform = $yamlform_submission
    ->getYamlForm();

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

  // Default to displaying a confirmation message on this page.
  $state = $yamlform_submission
    ->getState();
  if ($state == YamlFormSubmissionInterface::STATE_UPDATED) {
    $this->messageManager
      ->display(YamlFormMessageManagerInterface::SUBMISSION_UPDATED);
    $form_state
      ->setRedirect($route_name, $route_parameters, $route_options);
    return;
  }

  // Add token route query options.
  if ($state == YamlFormSubmissionInterface::STATE_COMPLETED) {
    $route_options['query']['token'] = $yamlform_submission
      ->getToken();
  }

  // Handle 'page', 'url', and 'inline' confirmation types.
  $confirmation_type = $this
    ->getYamlFormSetting('confirmation_type');
  switch ($confirmation_type) {
    case 'page':
      $redirect_route_name = $this->requestHandler
        ->getRouteName($yamlform, $this->sourceEntity, 'yamlform.confirmation');
      $redirect_route_parameters = $this->requestHandler
        ->getRouteParameters($yamlform, $this->sourceEntity);
      $form_state
        ->setRedirect($redirect_route_name, $redirect_route_parameters, $route_options);
      return;
    case 'url':
    case 'url_message':
      $confirmation_url = trim($this
        ->getYamlFormSetting('confirmation_url', ''));

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

      // Get system path.

      /** @var \Drupal\Core\Path\AliasManagerInterface $path_alias_manager */
      $path_alias_manager = \Drupal::service('path.alias_manager');
      $confirmation_url = $path_alias_manager
        ->getPathByAlias($confirmation_url);
      if ($redirect_url = \Drupal::pathValidator()
        ->getUrlIfValid($confirmation_url)) {
        if ($confirmation_type == 'url_message') {
          $this->messageManager
            ->display(YamlFormMessageManagerInterface::SUBMISSION_CONFIRMATION);
        }
        $this
          ->setTrustedRedirectUrl($form_state, $redirect_url);
        return;
      }

      // If confirmation URL is invalid display message.
      $this->messageManager
        ->display(YamlFormMessageManagerInterface::SUBMISSION_CONFIRMATION);
      $route_options['query']['yamlform_id'] = $yamlform
        ->id();
      break;
    case 'inline':
      $route_options['query']['yamlform_id'] = $yamlform
        ->id();
      break;
    case 'message':
    default:
      if (!$this->messageManager
        ->display(YamlFormMessageManagerInterface::SUBMISSION_CONFIRMATION)) {
        $this->messageManager
          ->display(YamlFormMessageManagerInterface::SUBMISSION_DEFAULT_CONFIRMATION);
      }
      break;
  }
  $form_state
    ->setRedirect($route_name, $route_parameters, $route_options);
}