You are here

protected function DefaultController::activeStylePath in Style Switcher 8.2

Same name and namespace in other branches
  1. 3.0.x src/Controller/DefaultController.php \Drupal\styleswitcher\Controller\DefaultController::activeStylePath()

Finds the style active for current user and returns its path.

This function is called at every page request before styleswitcherSwitch() or JS' Drupal.styleSwitcher.switchStyle() so we can update old user cookies here once and not bother about it in other places.

Parameters

string $theme: Name of the theme to find the active style for.

Return value

string|null The path property of active style. It can be NULL if active style is the blank one.

See also

\Drupal\styleswitcher\Controller\DefaultController::styleswitcherSwitch()

Drupal.styleSwitcher.switchStyle()

1 call to DefaultController::activeStylePath()
DefaultController::styleswitcherCss in src/Controller/DefaultController.php
Redirects to CSS file of currently active style.

File

src/Controller/DefaultController.php, line 115

Class

DefaultController
Default controller for the styleswitcher module.

Namespace

Drupal\styleswitcher\Controller

Code

protected function activeStylePath($theme) {
  if (isset($_COOKIE['styleswitcher'])) {
    $cookie = $_COOKIE['styleswitcher'];
    if (!is_array($cookie)) {

      // This style with its settings belongs to the theme which was default
      // before styleswitcher_update_7206(). If there's no variable, try the
      // default theme, it could still be the same one.
      $style_theme = $this
        ->config('styleswitcher.settings')
        ->get('7206_theme_default') ?? $this->themeHandler
        ->getDefault();
      if (strpos($cookie, '/')) {
        if (styleswitcher_style_load($cookie, $style_theme)) {
          $name = $cookie;
        }
      }
      elseif (($style = styleswitcher_style_load($cookie, $style_theme, 'theme')) || ($style = styleswitcher_style_load($cookie, $style_theme, 'custom'))) {
        $name = $style['name'];
      }

      // Remove this old cookie.
      setcookie('styleswitcher', '', 0, base_path());
      $cookie = [];
      if (isset($name)) {

        // And save the new one.
        $this
          ->saveUserPreference($style_theme, $name);
        $cookie[$style_theme] = $name;
      }
    }
    if (isset($cookie[$theme])) {
      $active = styleswitcher_style_load($cookie[$theme], $theme);
    }
  }
  elseif (isset($_COOKIE['styleSwitcher'])) {
    $name = 'theme/' . _styleswitcher_style_name($_COOKIE['styleSwitcher']);

    // Remove this old cookie.
    setcookie('styleSwitcher', '', 0, base_path());

    // We actually do not know what theme was used (it was a global $theme)
    // when user switched to this style. So let us just set this style as
    // active for every theme which has a style with this name.
    $themes = array_keys($this->themeHandler
      ->listInfo());
    foreach ($themes as $style_theme) {
      if ($style = styleswitcher_style_load($name, $style_theme)) {
        $this
          ->saveUserPreference($style_theme, $name);
        if ($theme == $style_theme) {
          $active = $style;
        }
      }
    }
  }
  if (empty($active)) {
    $active = styleswitcher_style_load(styleswitcher_default_style_key($theme), $theme);
  }
  return $active['path'];
}