class RoleNegotiator in Role Based Theme Switcher 8
Same name and namespace in other branches
- 9.1.x src/Theme/RoleNegotiator.php \Drupal\role_based_theme_switcher\Theme\RoleNegotiator
Sets the active theme on admin pages.
Hierarchy
- class \Drupal\role_based_theme_switcher\Theme\RoleNegotiator implements ThemeNegotiatorInterface
Expanded class hierarchy of RoleNegotiator
1 string reference to 'RoleNegotiator'
1 service uses RoleNegotiator
File
- src/
Theme/ RoleNegotiator.php, line 14
Namespace
Drupal\role_based_theme_switcher\ThemeView source
class RoleNegotiator implements ThemeNegotiatorInterface {
/**
* Protected configFactory variable.
*
* @var configFactory
*/
protected $configFactory;
/**
* Protected adminRoute variable.
*
* @var adminRoute
*/
protected $adminRoute;
/**
* Protected route_match variable.
*
* @var route_match
*/
protected $routeMatch;
/**
* Protected account variable.
*
* @var account
*/
protected $account;
/**
* {@inheritdoc}
*/
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;
}
/**
* 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) {
// Use this theme on a certain route.
$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;
}
// Here you return the actual theme name.
$roleThemes = $this->configFactory
->get('role_based_theme_switcher.RoleBasedThemeSwitchConfig')
->get('roletheme');
// Get current roles a user has.
$roles = $this->account
->getRoles();
// Get highest role.
$theme_role = $this
->getPriorityRole($roles);
if (!empty($roleThemes[$theme_role]['id'])) {
$this->theme = $roleThemes[$theme_role]['id'];
}
return $change_theme;
}
/**
* Function to get roles array and return highest priority role.
*
* @param array $roles
* Array of roles.
*
* @return string
* Return role.
*/
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 role.
return $priRole;
}
}
/**
* Determine the active theme for the request.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match object.
*
* @return string|null
* The name of the theme, or NULL if other negotiators, like the configured
* default one, should be used instead.
*/
public function determineActiveTheme(RouteMatchInterface $route_match) {
return $this->theme;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RoleNegotiator:: |
protected | property | Protected account variable. | |
RoleNegotiator:: |
protected | property | Protected adminRoute variable. | |
RoleNegotiator:: |
protected | property | Protected configFactory variable. | |
RoleNegotiator:: |
protected | property | Protected route_match variable. | |
RoleNegotiator:: |
public | function |
Whether this theme negotiator should be used to set the theme. Overrides ThemeNegotiatorInterface:: |
|
RoleNegotiator:: |
public | function |
Determine the active theme for the request. Overrides ThemeNegotiatorInterface:: |
|
RoleNegotiator:: |
public | function | Function to get roles array and return highest priority role. | |
RoleNegotiator:: |
public | function |