You are here

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

Same name and namespace in other branches
  1. 8.4 src/Form/OperationsForm.php \Drupal\advagg\Form\OperationsForm::toggleBypassCookie()
  2. 8.3 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 443

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]);
    drupal_set_message($this
      ->t('AdvAgg Bypass Cookie Removed.'));
  }
  else {
    setcookie($cookie_name, $key, REQUEST_TIME + $form_state
      ->getValue('timespan'), $GLOBALS['base_path'], '.' . $_SERVER['HTTP_HOST']);
    $_COOKIE[$cookie_name] = $key;
    drupal_set_message($this
      ->t('AdvAgg Bypass Cookie Set for %time.', [
      '%time' => $this->dateFormatter
        ->formatInterval($form_state
        ->getValue('timespan')),
    ]));
  }
}