You are here

public function OperationsForm::toggleBypassCookie in Advanced CSS/JS Aggregation 8.3

Same name and namespace in other branches
  1. 8.4 src/Form/OperationsForm.php \Drupal\advagg\Form\OperationsForm::toggleBypassCookie()
  2. 8.2 src/Form/OperationsForm.php \Drupal\advagg\Form\OperationsForm::toggleBypassCookie()

Set or remove the AdvAggDisabled cookie.

Parameters

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

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

File

src/Form/OperationsForm.php, line 344

Class

OperationsForm
Configure advagg settings for this site.

Namespace

Drupal\advagg\Form

Code

public function toggleBypassCookie(array &$form, FormStateInterface $form_state) {
  $cookie_name = 'AdvAggDisabled';
  $key = Crypt::hashBase64($this->privateKey
    ->get());

  // If the cookie does exist then remove it.
  if (!empty($_COOKIE[$cookie_name]) && $_COOKIE[$cookie_name] == $key) {
    setcookie($cookie_name, '', -1, $GLOBALS['base_path'], '.' . $_SERVER['HTTP_HOST']);
    unset($_COOKIE[$cookie_name]);
    $this->messenger
      ->addMessage($this
      ->t('AdvAgg Bypass Cookie Removed.'));
  }
  else {
    setcookie($cookie_name, $key, $this->time
      ->getRequestTime() + $form_state
      ->getValue('timespan'), $GLOBALS['base_path'], '.' . $_SERVER['HTTP_HOST']);
    $_COOKIE[$cookie_name] = $key;
    $this->messenger
      ->addMessage($this
      ->t('AdvAgg Bypass Cookie Set for %time.', [
      '%time' => $this->dateFormatter
        ->formatInterval($form_state
        ->getValue('timespan')),
    ]));
  }
  $this
    ->clearAggregates();
}