class RoleThemeSwitcherNegotiator in Role Theme Switcher 8
Class RoleThemeSwitcherNegotiator.
Hierarchy
- class \Drupal\role_theme_switcher\Theme\RoleThemeSwitcherNegotiator implements ThemeNegotiatorInterface
Expanded class hierarchy of RoleThemeSwitcherNegotiator
1 string reference to 'RoleThemeSwitcherNegotiator'
1 service uses RoleThemeSwitcherNegotiator
File
- src/
Theme/ RoleThemeSwitcherNegotiator.php, line 14
Namespace
Drupal\role_theme_switcher\ThemeView source
class RoleThemeSwitcherNegotiator implements ThemeNegotiatorInterface {
/**
* Protected theme variable to store the theme to active.
*
* @var string
*/
protected $theme = NULL;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Drupal\Core\Session\AccountProxy definition.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The route admin context to determine whether a route is an admin one.
*
* @var \Drupal\Core\Routing\AdminContext
*/
protected $adminContext;
/**
* Creates a new RoleThemeSwitcherNegotiator instance.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Routing\AdminContext $admin_context
* The route admin context to determine whether the route is an admin one.
*/
public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, AdminContext $admin_context) {
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
$this->adminContext = $admin_context;
}
/**
* Whether this theme negotiator should be used to set the theme.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match object.
*
* @return bool
* TRUE if this negotiator should be used or FALSE to let other negotiators
* decide.
*/
public function applies(RouteMatchInterface $route_match) {
$roles = $this->configFactory
->get('role_theme_switcher.settings')
->get('roles');
if ($roles) {
// Properly order all rows by weight.
uasort($roles, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
$user_roles = $this->currentUser
->getRoles();
foreach ($roles as $rid => $config) {
if (in_array($rid, $user_roles)) {
$this->theme = $config['theme'];
break;
}
}
}
// Return TRUE if there is a theme to activate and the current page is not
// an admin page.
return $this->theme && !$this->adminContext
->isAdminRoute($route_match
->getRouteObject());
}
/**
* Determine the active theme for the request.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match object.
*
* @return string
* The name of the theme
*/
public function determineActiveTheme(RouteMatchInterface $route_match) {
return $this->theme;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RoleThemeSwitcherNegotiator:: |
protected | property | The route admin context to determine whether a route is an admin one. | |
RoleThemeSwitcherNegotiator:: |
protected | property | The config factory. | |
RoleThemeSwitcherNegotiator:: |
protected | property | Drupal\Core\Session\AccountProxy definition. | |
RoleThemeSwitcherNegotiator:: |
protected | property | Protected theme variable to store the theme to active. | |
RoleThemeSwitcherNegotiator:: |
public | function |
Whether this theme negotiator should be used to set the theme. Overrides ThemeNegotiatorInterface:: |
|
RoleThemeSwitcherNegotiator:: |
public | function |
Determine the active theme for the request. Overrides ThemeNegotiatorInterface:: |
|
RoleThemeSwitcherNegotiator:: |
public | function | Creates a new RoleThemeSwitcherNegotiator instance. |