You are here

protected function WebformSubmissionForm::getCustomForm in Webform 6.x

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

Get custom webform which is displayed instead of the webform's elements.

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|bool A custom webform or FALSE if the default webform containing the webform's elements should be built.

1 call to WebformSubmissionForm::getCustomForm()
WebformSubmissionForm::form in src/WebformSubmissionForm.php
Gets the actual form array to be built.

File

src/WebformSubmissionForm.php, line 833

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

protected function getCustomForm(array &$form, FormStateInterface $form_state) {

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

  // Exit if elements are broken, usually occurs when elements YAML is edited
  // directly in the export config file.
  if (!$webform_submission
    ->getWebform()
    ->getElementsInitialized()) {
    return $this
      ->getMessageManager()
      ->append($form, WebformMessageManagerInterface::FORM_EXCEPTION_MESSAGE, 'warning');
  }

  // Exit if submission is locked.
  if ($webform_submission
    ->isLocked()) {
    return $this
      ->getMessageManager()
      ->append($form, WebformMessageManagerInterface::SUBMISSION_LOCKED_MESSAGE, 'warning');
  }

  // Check prepopulate source entity required and type.
  if ($webform
    ->getSetting('form_prepopulate_source_entity')) {
    if ($webform
      ->getSetting('form_prepopulate_source_entity_required') && empty($this
      ->getSourceEntity())) {
      $this
        ->getMessageManager()
        ->log(WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_REQUIRED, 'notice');
      return $this
        ->getMessageManager()
        ->append($form, WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_REQUIRED, 'warning');
    }
    $source_entity_type = $webform
      ->getSetting('form_prepopulate_source_entity_type');
    if ($source_entity_type && $this
      ->getSourceEntity() && $source_entity_type !== $this
      ->getSourceEntity()
      ->getEntityTypeId()) {
      $this
        ->getMessageManager()
        ->log(WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_TYPE, 'notice');
      return $this
        ->getMessageManager()
        ->append($form, WebformMessageManagerInterface::PREPOPULATE_SOURCE_ENTITY_TYPE, 'warning');
    }
  }

  // Display inline confirmation message with back to link.
  if ($form_state
    ->get('current_page') === WebformInterface::PAGE_CONFIRMATION) {
    $form['confirmation'] = [
      '#theme' => 'webform_confirmation',
      '#webform' => $webform,
      '#source_entity' => $webform_submission
        ->getSourceEntity(TRUE),
      '#webform_submission' => $webform_submission,
    ];

    // Add hidden back (aka reset) button used by the Ajaxified back to link.
    // NOTE: Below code could be used to add a 'Reset' button to any webform.
    // @see Drupal.behaviors.webformConfirmationBackAjax
    $form['actions'] = [
      '#type' => 'actions',
      '#attributes' => [
        'style' => 'display:none',
      ],
      // Do not process the actions element. This prevents the
      // .form-actions class from being added, which then makes the button
      // appear in dialogs.
      // @see \Drupal\Core\Render\Element\Actions::processActions
      // @see Drupal.behaviors.dialog.prepareDialogButtons
      '#process' => [],
    ];
    $form['actions']['reset'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Reset'),
      // @see \Drupal\webform\WebformSubmissionForm::noValidate
      '#validate' => [
        '::noValidate',
      ],
      '#submit' => [
        '::reset',
      ],
      '#attributes' => [
        'style' => 'display:none',
        'class' => [
          'js-webform-confirmation-back-submit-ajax',
        ],
      ],
    ];
    return $form;
  }

  // Don't display webform if it is closed.
  if (($webform_submission
    ->isNew() || $webform_submission
    ->isDraft()) && $webform
    ->isClosed()) {

    // If the current user can update any submission just display the closed
    // message and still allow them to create new submissions.
    if ($webform
      ->isTemplate() && $webform
      ->access('duplicate') && !$webform
      ->isArchived()) {
      if (!$this
        ->isDialog()) {
        $this
          ->getMessageManager()
          ->display(WebformMessageManagerInterface::TEMPLATE_PREVIEW, 'warning');
      }
    }
    elseif ($webform
      ->access('submission_update_any')) {
      $form = $this
        ->getMessageManager()
        ->append($form, $webform
        ->isArchived() ? WebformMessageManagerInterface::ADMIN_ARCHIVED : WebformMessageManagerInterface::ADMIN_CLOSED, 'info');
    }
    else {
      if ($webform
        ->isOpening()) {
        return $this
          ->getMessageManager()
          ->append($form, WebformMessageManagerInterface::FORM_OPEN_MESSAGE);
      }
      else {
        return $this
          ->getMessageManager()
          ->append($form, WebformMessageManagerInterface::FORM_CLOSE_MESSAGE);
      }
    }
  }

  // Disable this webform if confidential and user is logged in.
  if ($this
    ->isConfidential() && $this
    ->currentUser()
    ->isAuthenticated() && $this->entity
    ->isNew() && $this->operation === 'add') {
    return $this
      ->getMessageManager()
      ->append($form, WebformMessageManagerInterface::FORM_CONFIDENTIAL_MESSAGE, 'warning');
  }

  // Disable this webform if submissions are not being saved to the database or
  // passed to a WebformHandler.
  if ($this
    ->getWebformSetting('results_disabled') && !$this
    ->getWebformSetting('results_disabled_ignore') && !$webform
    ->getHandlers(NULL, TRUE, WebformHandlerInterface::RESULTS_PROCESSED)
    ->count()) {
    $this
      ->getMessageManager()
      ->log(WebformMessageManagerInterface::FORM_SAVE_EXCEPTION, 'error');
    if ($this
      ->currentUser()
      ->hasPermission('administer webform')) {

      // Display error to admin but allow them to submit the broken webform.
      $form = $this
        ->getMessageManager()
        ->append($form, WebformMessageManagerInterface::FORM_SAVE_EXCEPTION, 'error');
      $form = $this
        ->getMessageManager()
        ->append($form, WebformMessageManagerInterface::ADMIN_CLOSED, 'info');
    }
    else {

      // Display exception message to users.
      return $this
        ->getMessageManager()
        ->append($form, WebformMessageManagerInterface::FORM_EXCEPTION_MESSAGE, 'warning');
    }
  }

  // Check total limit.
  if ($this
    ->checkTotalLimit() && empty($this
    ->getWebformSetting('limit_total_unique'))) {
    $form = $this
      ->getMessageManager()
      ->append($form, WebformMessageManagerInterface::LIMIT_TOTAL_MESSAGE, 'warning');
    if ($webform
      ->access('submission_update_any')) {
      $form = $this
        ->getMessageManager()
        ->append($form, WebformMessageManagerInterface::ADMIN_CLOSED, 'info');
      return FALSE;
    }
    else {
      return $form;
    }
  }

  // Check user limit.
  if ($this
    ->checkUserLimit() && empty($this
    ->getWebformSetting('limit_user_unique'))) {
    $form = $this
      ->getMessageManager()
      ->append($form, WebformMessageManagerInterface::LIMIT_USER_MESSAGE, 'warning');
    if ($webform
      ->access('submission_update_any')) {
      $form = $this
        ->getMessageManager()
        ->append($form, WebformMessageManagerInterface::ADMIN_CLOSED, 'info');
      return FALSE;
    }
    else {
      return $form;
    }
  }
  return FALSE;
}