You are here

public function WebformSubmissionForm::buildForm in Webform 8.5

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

Form constructor.

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 The form structure.

Overrides EntityForm::buildForm

1 call to WebformSubmissionForm::buildForm()
WebformTemplatesSubmissionPreviewForm::buildForm in modules/webform_templates/src/WebformTemplatesSubmissionPreviewForm.php
Form constructor.
1 method overrides WebformSubmissionForm::buildForm()
WebformTemplatesSubmissionPreviewForm::buildForm in modules/webform_templates/src/WebformTemplatesSubmissionPreviewForm.php
Form constructor.

File

src/WebformSubmissionForm.php, line 627

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

public function buildForm(array $form, FormStateInterface $form_state) {

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

  // Track the 'webform_submission_token' form multiple draft submissions.
  // The 'webform_submission_token' value is set after the form is cached
  // and built.
  // @see \Drupal\webform\WebformSubmissionForm::afterBuild
  if ($webform
    ->getSetting('draft_multiple') && ($webform_submission
    ->isNew() || $webform_submission
    ->isDraft())) {
    $form['webform_submission_token'] = [
      '#type' => 'hidden',
      '#value' => $webform_submission
        ->getToken(),
    ];
  }

  // Only prepopulate data when a webform is initially loaded.
  if (!$form_state
    ->isRebuilding()) {
    $data = $webform_submission
      ->getData();
    $this
      ->prepopulateData($data);
    $webform_submission
      ->setData($data);
  }

  // Apply variants.
  $webform
    ->applyVariants($webform_submission);

  // All anonymous submissions are tracked in the $_SESSION.
  // @see \Drupal\webform\WebformSubmissionStorage::setAnonymousSubmission
  if ($this
    ->currentUser()
    ->isAnonymous()) {
    $form['#cache']['contexts'][] = 'session';
  }

  // Add the webform as a cacheable dependency.
  $this->renderer
    ->addCacheableDependency($form, $webform);

  // Kill page cache for scheduled webforms.
  // @todo Remove once bubbling of element's max-age to page cache is fixed.
  // @see https://www.drupal.org/project/webform/issues/3015760
  // @see https://www.drupal.org/project/drupal/issues/2352009
  // @see \Drupal\webform\Element\Webform::preRenderWebformElement
  if ($webform
    ->isScheduled() && $this
    ->currentUser()
    ->isAnonymous() && $this->moduleHandler
    ->moduleExists('page_cache')) {
    $this->killSwitch
      ->trigger();
  }

  // Display status messages.
  $this
    ->displayMessages($form, $form_state);

  // Build the webform.
  $form = parent::buildForm($form, $form_state);

  // Ajax: Scroll to.
  // @see \Drupal\webform\Form\WebformAjaxFormTrait::submitAjaxForm
  if ($this
    ->isAjax()) {
    $form['#webform_ajax_scroll_top'] = $this
      ->getWebformSetting('ajax_scroll_top', '');
  }

  // Alter element's form.
  if (isset($form['elements']) && is_array($form['elements'])) {
    $elements = $form['elements'];
    $this
      ->alterElementsForm($elements, $form, $form_state);
  }

  // Add Ajax callbacks.
  $ajax_settings = [
    'effect' => $this
      ->getWebformSetting('ajax_effect'),
    'speed' => (int) $this
      ->getWebformSetting('ajax_speed'),
    'progress' => [
      'type' => $this
        ->getWebformSetting('ajax_progress_type'),
      'message' => '',
    ],
  ];
  $form = $this
    ->buildAjaxForm($form, $form_state, $ajax_settings);

  // Alter webform via webform handler.
  $this
    ->getWebform()
    ->invokeHandlers('alterForm', $form, $form_state, $webform_submission);

  // Call custom webform alter hook.
  $form_id = $this
    ->getFormId();
  $this->thirdPartySettingsManager
    ->alter('webform_submission_form', $form, $form_state, $form_id);

  // Server side #states API validation.
  $this->conditionsValidator
    ->buildForm($form, $form_state);

  // Append the bubbleable metadat to the form's render array.
  // @see \Drupal\webform\WebformSubmissionForm::setEntity
  $this->bubbleableMetadata
    ->appendTo($form);
  return $form;
}