You are here

class RoleNegotiator in Role Based Theme Switcher 8

Same name and namespace in other branches
  1. 9.1.x src/Theme/RoleNegotiator.php \Drupal\role_based_theme_switcher\Theme\RoleNegotiator

Sets the active theme on admin pages.

Hierarchy

Expanded class hierarchy of RoleNegotiator

1 string reference to 'RoleNegotiator'
role_based_theme_switcher.services.yml in ./role_based_theme_switcher.services.yml
role_based_theme_switcher.services.yml
1 service uses RoleNegotiator
theme.negotiator.role_based_theme_switcher in ./role_based_theme_switcher.services.yml
Drupal\role_based_theme_switcher\Theme\RoleNegotiator

File

src/Theme/RoleNegotiator.php, line 14

Namespace

Drupal\role_based_theme_switcher\Theme
View 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

Namesort descending Modifiers Type Description Overrides
RoleNegotiator::$account protected property Protected account variable.
RoleNegotiator::$adminRoute protected property Protected adminRoute variable.
RoleNegotiator::$configFactory protected property Protected configFactory variable.
RoleNegotiator::$routeMatch protected property Protected route_match variable.
RoleNegotiator::applies public function Whether this theme negotiator should be used to set the theme. Overrides ThemeNegotiatorInterface::applies
RoleNegotiator::determineActiveTheme public function Determine the active theme for the request. Overrides ThemeNegotiatorInterface::determineActiveTheme
RoleNegotiator::getPriorityRole public function Function to get roles array and return highest priority role.
RoleNegotiator::__construct public function