You are here

public function DataPolicyAgreement::submitForm in Data Policy 8

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/DataPolicyAgreement.php, line 146

Class

DataPolicyAgreement
Class DataPolicyAgreement.

Namespace

Drupal\data_policy\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValue('data_policy');
  $result = [];
  foreach ($values as $name => $value) {
    $result[$name] = [
      'value' => $value,
      'required' => $form['account']['data_policy'][$name]['#required'],
      'entity_id' => (int) filter_var($name, FILTER_SANITIZE_NUMBER_INT),
      'state' => !empty($values[$name]),
    ];
  }
  $this->dataPolicyConsentManager
    ->saveConsent($this
    ->currentUser()
    ->id(), 'submit', $result);
  foreach ($result as $item) {

    // If the user does not agree and it is enforced, we will redirect to
    // the cancel account page.
    if ($item['required'] && !$item['value']) {
      $this
        ->getRequest()->query
        ->remove('destination');
      $form_state
        ->setRedirect('entity.user.cancel_form', [
        'user' => $this
          ->currentUser()
          ->id(),
      ]);
    }
  }

  // If the user agrees or does not agree (but it is not enforced), check if
  // we should redirect to the front page.
  if ($this->destination
    ->get() === '/data-policy-agreement') {
    $form_state
      ->setRedirect('<front>');
  }
}