You are here

protected function YamlFormSubmissionForm::getCustomForm in YAML Form 8

Get custom form which is displayed instead of the form'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 form or FALSE if the default form containing the form's elements should be built.

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

File

src/YamlFormSubmissionForm.php, line 333

Class

YamlFormSubmissionForm
Provides a form to collect and edit submissions.

Namespace

Drupal\yamlform

Code

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

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

  // Exit if elements are broken, usually occurs when elements YAML is edited
  // directly in the export config file.
  if (!$yamlform_submission
    ->getYamlForm()
    ->getElementsInitialized()) {
    $this->messageManager
      ->display(YamlFormMessageManagerInterface::FORM_EXCEPTION, 'warning');
    return $form;
  }

  // Handle form with managed file upload but saving of submission is disabled.
  if ($yamlform
    ->hasManagedFile() && !empty($this
    ->getYamlFormSetting('results_disabled'))) {
    $this->messageManager
      ->log(YamlFormMessageManagerInterface::FORM_FILE_UPLOAD_EXCEPTION, 'notice');
    $this->messageManager
      ->display(YamlFormMessageManagerInterface::FORM_EXCEPTION, 'warning');
    return $form;
  }

  // Display inline confirmation message with back to link which is rendered
  // via the controller.
  if ($this
    ->getYamlFormSetting('confirmation_type') == 'inline' && $this
    ->getRequest()->query
    ->get('yamlform_id') == $yamlform
    ->id()) {
    $yamlform_controller = new YamlFormController($this->requestHandler, $this->messageManager);
    $form['confirmation'] = $yamlform_controller
      ->confirmation($this
      ->getRequest(), $yamlform);
    return $form;
  }

  // Don't display form if it is closed.
  if ($yamlform_submission
    ->isNew() && $yamlform
    ->isClosed()) {

    // If the current user can update any submission just display the closed
    // message and still allow them to create new submissions.
    if ($yamlform
      ->isTemplate() && $yamlform
      ->access('duplicate')) {
      if (!$this
        ->isModalDialog()) {
        $this->messageManager
          ->display(YamlFormMessageManagerInterface::TEMPLATE_PREVIEW, 'warning');
      }
    }
    elseif ($yamlform
      ->access('submission_update_any')) {
      $this->messageManager
        ->display(YamlFormMessageManagerInterface::ADMIN_ACCESS, 'warning');
    }
    else {
      $form['closed'] = $this->messageManager
        ->build(YamlFormMessageManagerInterface::FORM_CLOSED_MESSAGE);
      return $form;
    }
  }

  // Disable this form if confidential and user is logged in.
  if ($this
    ->isConfidential() && $this
    ->currentUser()
    ->isAuthenticated() && $this->entity
    ->isNew()) {
    $this->messageManager
      ->display(YamlFormMessageManagerInterface::FORM_CONFIDENTIAL_MESSAGE, 'warning');
    return $form;
  }

  // Disable this form if submissions are not being saved to the database or
  // passed to a YamlFormHandler.
  if ($this
    ->getYamlFormSetting('results_disabled') && !$this
    ->getYamlFormSetting('results_disabled_ignore') && !$yamlform
    ->getHandlers(NULL, TRUE, YamlFormHandlerInterface::RESULTS_PROCESSED)
    ->count()) {
    $this->messageManager
      ->log(YamlFormMessageManagerInterface::FORM_SAVE_EXCEPTION, 'error');
    if ($this
      ->currentUser()
      ->hasPermission('administer yamlform')) {

      // Display error to admin but allow them to submit the broken form.
      $this->messageManager
        ->display(YamlFormMessageManagerInterface::FORM_SAVE_EXCEPTION, 'error');
      $this->messageManager
        ->display(YamlFormMessageManagerInterface::ADMIN_ACCESS, 'warning');
    }
    else {

      // Display exception message to users.
      $this->messageManager
        ->display(YamlFormMessageManagerInterface::FORM_EXCEPTION, 'warning');
      return $form;
    }
  }

  // Check total limit.
  if ($this
    ->checkTotalLimit()) {
    $this->messageManager
      ->display(YamlFormMessageManagerInterface::LIMIT_TOTAL_MESSAGE);
    if ($yamlform
      ->access('submission_update_any')) {
      $this->messageManager
        ->display(YamlFormMessageManagerInterface::ADMIN_ACCESS, 'warning');
    }
    else {
      return $form;
    }
  }

  // Check user limit.
  if ($this
    ->checkUserLimit()) {
    $this->messageManager
      ->display(YamlFormMessageManagerInterface::LIMIT_USER_MESSAGE, 'warning');
    if ($yamlform
      ->access('submission_update_any')) {
      $this->messageManager
        ->display(YamlFormMessageManagerInterface::ADMIN_ACCESS, 'warning');
    }
    else {
      return $form;
    }
  }
  return FALSE;
}