RoleNegotiator.php in Role Based Theme Switcher 8
File
src/Theme/RoleNegotiator.php
View source
<?php
namespace Drupal\role_based_theme_switcher\Theme;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Theme\ThemeNegotiatorInterface;
class RoleNegotiator implements ThemeNegotiatorInterface {
protected $configFactory;
protected $adminRoute;
protected $routeMatch;
protected $account;
public function __construct(ConfigFactoryInterface $config_factory, AdminContext $adminRoute, RouteMatchInterface $routeMatch, AccountProxy $account) {
$this->configFactory = $config_factory;
$this->adminRoute = $adminRoute;
$this->routeMatch = $routeMatch;
$this->account = $account;
}
public function applies(RouteMatchInterface $route_match) {
$change_theme = TRUE;
$route = $this->routeMatch
->getRouteObject();
$is_admin_route = $this->adminRoute
->isAdminRoute($route);
if ($is_admin_route === TRUE && $this->account
->hasPermission('view the administration theme') === TRUE) {
$change_theme = FALSE;
}
$roleThemes = $this->configFactory
->get('role_based_theme_switcher.RoleBasedThemeSwitchConfig')
->get('roletheme');
$roles = $this->account
->getRoles();
$theme_role = $this
->getPriorityRole($roles);
if (!empty($roleThemes[$theme_role]['id'])) {
$this->theme = $roleThemes[$theme_role]['id'];
}
return $change_theme;
}
public function getPriorityRole(array $roles) {
$themes = $this->configFactory
->get('role_based_theme_switcher.RoleBasedThemeSwitchConfig')
->get('roletheme');
if (isset($themes)) {
foreach ($themes as $key => $value) {
if (in_array($key, $roles)) {
$themeArr[$key] = $value['weight'];
}
}
$priRole = array_search(max($themeArr), $themeArr);
return $priRole;
}
}
public function determineActiveTheme(RouteMatchInterface $route_match) {
return $this->theme;
}
}