ThemeSwitcherController.php in Theme Switcher Rules 8
File
src/Controller/ThemeSwitcherController.php
View source
<?php
namespace Drupal\theme_switcher\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\theme_switcher\ThemeSwitcherRuleInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class ThemeSwitcherController extends ControllerBase {
protected $messenger;
protected $logger;
public function __construct(MessengerInterface $messenger, LoggerChannelInterface $logger) {
$this->messenger = $messenger;
$this->logger = $logger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('messenger'), $container
->get('logger.factory')
->get('theme_switcher'));
}
public function ajaxOperation(ThemeSwitcherRuleInterface $theme_switcher_rule, $op = NULL) {
$message = $this
->t("The operation '%op' to '%label' failed.", [
'%op' => $op,
'%label' => $theme_switcher_rule
->label(),
]);
try {
switch ($op) {
case 'enable':
$theme_switcher_rule
->enable();
$message = $this
->t("The Theme Switcher Rule '%label' has been enabled.", [
'%label' => $theme_switcher_rule
->label(),
]);
break;
case 'disable':
$theme_switcher_rule
->disable();
$message = $this
->t("The Theme Switcher Rule '%label' has been disabled.", [
'%label' => $theme_switcher_rule
->label(),
]);
break;
}
$theme_switcher_rule
->save();
$this->messenger
->addStatus($message);
$this->logger
->notice($message);
} catch (EntityStorageException $e) {
$this->messenger
->addStatus($message);
$this->logger
->error($message);
}
$url = Url::fromRoute('theme_switcher.admin', [], [
'absolute' => TRUE,
]);
return new RedirectResponse($url
->toString(), 302);
}
}