You are here

public function WebformSubmissionForm::save in Webform 8.5

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

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

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

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

2 calls to WebformSubmissionForm::save()
WebformSubmissionForm::autosave in src/WebformSubmissionForm.php
Webform submission handler to autosave when there are validation errors.
WebformSubmissionForm::wizardSubmit in src/WebformSubmissionForm.php
Webform submission handler for the wizard submit action.

File

src/WebformSubmissionForm.php, line 1953

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

public function save(array $form, FormStateInterface $form_state) {
  $webform = $this
    ->getWebform();

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

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

  // Make sure the uri and remote addr are set correctly because
  // Ajax requests can cause these values to be reset.
  if ($webform_submission
    ->isNew()) {
    if (preg_match('/\\.webform\\.test_form$/', $this
      ->getRouteMatch()
      ->getRouteName())) {

      // For test submissions use the source URL.
      $source_url = $webform_submission
        ->set('uri', NULL)
        ->getSourceUrl()
        ->setAbsolute(FALSE);
      $uri = preg_replace('#^' . base_path() . '#', '/', $source_url
        ->toString());
    }
    else {

      // For all other submissions, use the request URI.
      $uri = preg_replace('#^' . base_path() . '#', '/', $this
        ->getRequest()
        ->getRequestUri());

      // Remove Ajax query string parameters.
      $uri = preg_replace('/(ajax_form=1|_wrapper_format=(drupal_ajax|drupal_modal|drupal_dialog|html|ajax))(&|$)/', '', $uri);

      // Remove empty query string.
      $uri = preg_replace('/\\?$/', '', $uri);
    }
    $webform_submission
      ->set('uri', $uri);
    $webform_submission
      ->set('remote_addr', $this
      ->getWebform()
      ->hasRemoteAddr() ? $this
      ->getRequest()
      ->getClientIp() : '');
    if ($this
      ->isConfidential()) {
      $webform_submission
        ->setOwnerId(0);
    }
  }

  // Block users from submitting templates that they can't update.
  if ($webform
    ->isTemplate() && !$webform
    ->access('update')) {
    return;
  }

  // Save and log webform submission.
  $webform_submission
    ->save();

  // Invalidate cache if any limits are specified.
  if ($this
    ->getWebformSetting('limit_total') || $this
    ->getWebformSetting('user_limit_total') || $this
    ->getWebformSetting('entity_limit_total') || $this
    ->getWebformSetting('entity_limit_user') || $this
    ->getWebformSetting('limit_total_unique') || $this
    ->getWebformSetting('limit_user_unique')) {
    Cache::invalidateTags([
      'webform:' . $this
        ->getWebform()
        ->id(),
    ]);
  }

  // Check limits rebuild.
  if ($this
    ->checkTotalLimit() || $this
    ->checkUserLimit()) {
    $form_state
      ->setRebuild();
  }
}