PageThemeNegotiator.php in Switch Page Theme 8
File
src/Theme/PageThemeNegotiator.php
View source
<?php
namespace Drupal\switch_page_theme\Theme;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Theme\ThemeNegotiatorInterface;
use Drupal\domain\DomainNegotiatorInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class PageThemeNegotiator implements ThemeNegotiatorInterface {
protected $configFactory;
protected $currentPath;
protected $pathAlias;
protected $pathMatcher;
protected $account;
protected $moduleHandler;
protected $negotiator;
public $request;
protected $languageManager;
public function __construct(ConfigFactoryInterface $config_factory, CurrentPathStack $currentPath, AliasManagerInterface $pathAlias, PathMatcherInterface $pathMatcher, AccountProxyInterface $account, ModuleHandlerInterface $module_handler, RequestStack $request, DomainNegotiatorInterface $negotiator = NULL, LanguageManagerInterface $language_manager = NULL) {
$this->configFactory = $config_factory;
$this->currentPath = $currentPath;
$this->pathAlias = $pathAlias;
$this->pathMatcher = $pathMatcher;
$this->account = $account;
$this->moduleHandler = $module_handler;
$this->request = $request;
if ($negotiator) {
$this->negotiator = $negotiator;
}
if ($language_manager) {
$this->languageManager = $language_manager;
}
}
public function applies(RouteMatchInterface $route_match) {
global $theme;
$applies = FALSE;
$spt_table = $this->configFactory
->get('switch_page_theme.settings')
->get('spt_table');
if (!$spt_table) {
return FALSE;
}
foreach ($spt_table as $value) {
$value += [
'domain' => [],
'language' => [],
];
$condition = FALSE;
if ($value['status'] == 1) {
$condition = $this->request
->getCurrentRequest()->attributes
->get("_route") != "system.403" && ($this->pathMatcher
->matchPath($this->currentPath
->getPath(), $value["pages"]) || $this->pathMatcher
->matchPath($this->pathAlias
->getAliasByPath($this->currentPath
->getPath()), $value["pages"])) && (empty($value["theme_key"]) || $this->request
->getCurrentRequest()
->get('theme_key') == $value["theme_key"]) && (!array_filter($value["roles"]) || !empty(array_intersect($value["roles"], $this->account
->getRoles())));
if ($this->moduleHandler
->moduleExists('domain') && !empty($this->negotiator
->getActiveDomain())) {
$condition = $condition && (!array_filter($value["domain"]) || !empty(array_intersect($value["domain"], [
$this->negotiator
->getActiveDomain()
->id(),
])));
}
if ($this->languageManager
->isMultilingual() || $this->moduleHandler
->moduleExists('language')) {
$condition = $condition && (!array_filter($value["language"]) || !empty(array_intersect($value["language"], [
$this->languageManager
->getCurrentLanguage()
->getId(),
])));
}
if ($condition) {
$applies = TRUE;
$theme = $value['theme'];
}
}
}
return $applies;
}
public function determineActiveTheme(RouteMatchInterface $route_match) {
global $theme;
return $theme;
}
}