You are here

public function AgreementForm::submitForm in Agreement 8.2

Same name in this branch
  1. 8.2 src/Form/AgreementForm.php \Drupal\agreement\Form\AgreementForm::submitForm()
  2. 8.2 src/Entity/AgreementForm.php \Drupal\agreement\Entity\AgreementForm::submitForm()
Same name and namespace in other branches
  1. 3.0.x src/Form/AgreementForm.php \Drupal\agreement\Form\AgreementForm::submitForm()

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/AgreementForm.php, line 191

Class

AgreementForm
Agreement page form.

Namespace

Drupal\agreement\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $storage = $form_state
    ->getStorage();

  /** @var \Drupal\agreement\Entity\Agreement $agreement */
  $agreement = $storage['agreement'];
  $settings = $agreement
    ->getSettings();
  $destination = '/';
  $agree = $form_state
    ->getValue('agree');
  if ($settings['destination']) {
    $destination = $settings['destination'];
  }
  elseif (isset($_SESSION['agreement_destination'])) {
    $destination = $_SESSION['agreement_destination'];
  }
  $successfulResponse = $this
    ->processAgreement($agreement, $agree, $storage, $destination);
  if ($successfulResponse) {
    $message = $agree ? $settings['success'] : $settings['revoked'];
    $this->messenger
      ->addStatus($this
      ->t('@success', [
      '@success' => $message,
    ]));
    if ($settings['recipient'] && (!$storage['agreed'] || !$agree)) {

      // Only spam the email recipient if its the users first acceptance of
      // the agreement. If this is desired, please file an issue to discuss.
      $mail_key = $agree ? 'notice' : 'revoked';
      $this
        ->notify($agreement, $this->account, $settings['recipient'], $mail_key);
    }
    $form_state
      ->setResponse($successfulResponse);
  }
  elseif (!$storage['agreed']) {
    $this->messenger
      ->addError($this
      ->t('An error occurred accepting the agreement. Please try again.'));
  }
}