class DefaultController in Style Switcher 3.0.x
Same name and namespace in other branches
- 8.2 src/Controller/DefaultController.php \Drupal\styleswitcher\Controller\DefaultController
Default controller for the styleswitcher module.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\styleswitcher\Controller\DefaultController
Expanded class hierarchy of DefaultController
File
- src/
Controller/ DefaultController.php, line 15
Namespace
Drupal\styleswitcher\ControllerView source
class DefaultController extends ControllerBase {
/**
* The Datetime service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* The theme handler service.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* Constructs a new DefaultController.
*
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The DateTime service.
*/
public function __construct(ThemeHandlerInterface $theme_handler, TimeInterface $time) {
$this->themeHandler = $theme_handler;
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('theme_handler'), $container
->get('datetime.time'));
}
/**
* Switches style when JS is disabled.
*
* @param array $style
* New active style. The structure of an array is the same as returned from
* styleswitcher_style_load().
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Response object.
*
* @see styleswitcher_style_load()
*/
public function styleswitcherSwitch(array $style) {
if ($style['status']) {
$this
->saveUserPreference($style['theme'], $style['name']);
}
return $this
->redirect('<front>');
}
/**
* Redirects to CSS file of currently active style.
*
* @param string $theme
* Name of the theme to find the active style for. This argument is needed
* to know what the page user came from and what theme was used there.
*
* @return \Symfony\Component\HttpFoundation\Response
* Response object.
*/
public function styleswitcherCss($theme) {
// Prevent resource incorrect interpretation.
$headers = [
'Content-Type' => 'text/css',
];
$path = $this
->activeStylePath($theme);
if (isset($path)) {
return new TrustedRedirectResponse(file_create_url($path), 302, $headers);
}
else {
return new Response('', 200, $headers);
}
}
/**
* 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.
*
* @param string $theme
* Name of the theme to find the active style for.
*
* @return string|null
* The path property of active style. It can be NULL if active style is the
* blank one.
*
* @see \Drupal\styleswitcher\Controller\DefaultController::styleswitcherSwitch()
* @see Drupal.styleSwitcher.switchStyle()
*/
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'];
}
/**
* Saves the style key to the cookie.
*
* @param string $theme_key
* Name of the theme to save the style for.
* @param string $style_key
* Style key to save.
*/
protected function saveUserPreference($theme_key, $style_key) {
$request_time = $this->time
->getRequestTime();
setcookie('styleswitcher[' . $theme_key . ']', $style_key, $request_time + STYLESWITCHER_COOKIE_EXPIRE, base_path());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
DefaultController:: |
protected | property | The theme handler service. | |
DefaultController:: |
protected | property | The Datetime service. | |
DefaultController:: |
protected | function | Finds the style active for current user and returns its path. | |
DefaultController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
DefaultController:: |
protected | function | Saves the style key to the cookie. | |
DefaultController:: |
public | function | Redirects to CSS file of currently active style. | |
DefaultController:: |
public | function | Switches style when JS is disabled. | |
DefaultController:: |
public | function | Constructs a new DefaultController. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |