You are here

protected function WebformSubmissionForm::displayMessages in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/WebformSubmissionForm.php \Drupal\webform\WebformSubmissionForm::displayMessages()

Display draft, previous submission, and autofill status messages for this webform submission.

Parameters

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

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

1 call to WebformSubmissionForm::displayMessages()
WebformSubmissionForm::buildForm in src/WebformSubmissionForm.php
Form constructor.

File

src/WebformSubmissionForm.php, line 1060

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

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

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

  // Display test message, except on share page.
  if ($this
    ->isGet() && $this->operation === 'test' && !$this
    ->isSharePage()) {
    $this
      ->getMessageManager()
      ->display(WebformMessageManagerInterface::SUBMISSION_TEST, 'warning');

    // Display devel generate link for webform or source entity.
    if ($this->moduleHandler
      ->moduleExists('devel_generate') && $this
      ->currentUser()
      ->hasPermission('administer webform')) {
      $query = [
        'webform_id' => $webform
          ->id(),
      ];
      if ($source_entity) {
        $query += [
          'entity_type' => $source_entity
            ->getEntityTypeId(),
          'entity_id' => $source_entity
            ->id(),
        ];
      }
      $query['destination'] = $this->requestHandler
        ->getUrl($webform, $source_entity, 'webform.results_submissions')
        ->toString();
      $offcanvas = WebformDialogHelper::useOffCanvas();
      $build = [
        '#type' => 'link',
        '#title' => $this
          ->t('Generate %title submissions', [
          '%title' => $webform
            ->label(),
        ]),
        '#url' => Url::fromRoute('devel_generate.webform_submission', [], [
          'query' => $query,
        ]),
        '#attributes' => $offcanvas ? WebformDialogHelper::getOffCanvasDialogAttributes(400) : [
          'class' => [
            'button',
          ],
        ],
      ];
      if ($offcanvas) {
        WebformDialogHelper::attachLibraries($form);
      }
      $this
        ->messenger()
        ->addWarning($this->renderer
        ->renderPlain($build));
    }
  }

  // Display admin only message.
  if ($this
    ->isGet() && $this
    ->isRoute('webform.canonical') && $this
    ->getRouteMatch()
    ->getRawParameter('webform') === $webform
    ->id() && !$this
    ->getWebform()
    ->getSetting('page')) {
    $this
      ->getMessageManager()
      ->display(WebformMessageManagerInterface::ADMIN_PAGE, 'info');
  }

  // Display loaded or saved draft message.
  if ($webform_submission
    ->isDraft()) {
    if ($form_state
      ->get('draft_saved')) {
      $this
        ->getMessageManager()
        ->display(WebformMessageManagerInterface::SUBMISSION_DRAFT_SAVED_MESSAGE);
      $form_state
        ->set('draft_saved', FALSE);
    }
    elseif ($this
      ->isGet() && !$webform
      ->getSetting('draft_multiple') && !$webform
      ->isClosed()) {
      $this
        ->getMessageManager()
        ->display(WebformMessageManagerInterface::SUBMISSION_DRAFT_LOADED_MESSAGE);
    }
  }

  // Display link to multiple drafts message when user is adding a new
  // submission.
  if ($this
    ->isGet() && $this->operation === 'add' && $this
    ->getWebformSetting('draft') !== WebformInterface::DRAFT_NONE && $this
    ->getWebformSetting('draft_multiple', FALSE) && ($previous_draft_total = $this
    ->getStorage()
    ->getTotal($webform, $this->sourceEntity, $this
    ->currentUser(), [
    'in_draft' => TRUE,
  ]))) {
    if ($previous_draft_total > 1) {
      $this
        ->getMessageManager()
        ->display(WebformMessageManagerInterface::DRAFT_PENDING_MULTIPLE);
    }
    else {
      $draft_submission = $this
        ->getStorage()
        ->loadDraft($webform, $this->sourceEntity, $this
        ->currentUser());
      if (!$draft_submission || $webform_submission
        ->id() !== $draft_submission
        ->id()) {
        $this
          ->getMessageManager()
          ->display(WebformMessageManagerInterface::DRAFT_PENDING_SINGLE);
      }
    }
  }

  // Display link to previous submissions message when user is adding a new
  // submission.
  if ($this
    ->isGet() && $this->operation === 'add' && $this
    ->getWebformSetting('form_previous_submissions', FALSE) && ($webform
    ->access('submission_view_own') || $this
    ->currentUser()
    ->hasPermission('view own webform submission')) && ($previous_submission_total = $this
    ->getStorage()
    ->getTotal($webform, $this->sourceEntity, $this
    ->currentUser()))) {
    if ($previous_submission_total > 1) {
      $this
        ->getMessageManager()
        ->display(WebformMessageManagerInterface::PREVIOUS_SUBMISSIONS);
    }
    else {
      $last_submission = $this
        ->getStorage()
        ->getLastSubmission($webform, $source_entity, $account);
      if ($last_submission && $webform_submission
        ->id() !== $last_submission
        ->id()) {
        $this
          ->getMessageManager()
          ->display(WebformMessageManagerInterface::PREVIOUS_SUBMISSION);
      }
    }
  }

  // Display autofill message.
  if ($this
    ->isGet() && $this->operation === 'add' && $webform_submission
    ->isNew() && $webform
    ->getSetting('autofill') && $this
    ->getStorage()
    ->getLastSubmission($webform, $source_entity, $account, [
    'in_draft' => FALSE,
    'access_check' => FALSE,
  ])) {
    $this
      ->getMessageManager()
      ->display(WebformMessageManagerInterface::AUTOFILL_MESSAGE);
  }
}