AdminNegotiator.php 2.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\user\Theme\AdminNegotiator.
 */

namespace Drupal\user\Theme;

10
use Drupal\Core\Config\ConfigFactoryInterface;
11
use Drupal\Core\Entity\EntityManagerInterface;
12
use Drupal\Core\Routing\AdminContext;
13
use Drupal\Core\Routing\RouteMatchInterface;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Theme\ThemeNegotiatorInterface;

/**
 * Sets the active theme on admin pages.
 */
class AdminNegotiator implements ThemeNegotiatorInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $user;

  /**
   * The config factory.
   *
32
   * @var \Drupal\Core\Config\ConfigFactoryInterface
33
34
35
36
37
38
   */
  protected $configFactory;

  /**
   * The entity manager.
   *
39
   * @var \Drupal\Core\Entity\EntityManagerInterface
40
41
42
   */
  protected $entityManager;

43
44
45
46
47
48
49
  /**
   * The route admin context to determine whether a route is an admin one.
   *
   * @var \Drupal\Core\Routing\AdminContext
   */
  protected $adminContext;

50
51
52
53
54
  /**
   * Creates a new AdminNegotiator instance.
   *
   * @param \Drupal\Core\Session\AccountInterface $user
   *   The current user.
55
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
56
   *   The config factory.
57
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
58
   *   The entity manager.
59
60
   * @param \Drupal\Core\Routing\AdminContext $admin_context
   *   The route admin context to determine whether the route is an admin one.
61
   */
62
  public function __construct(AccountInterface $user, ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, AdminContext $admin_context) {
63
64
65
    $this->user = $user;
    $this->configFactory = $config_factory;
    $this->entityManager = $entity_manager;
66
    $this->adminContext = $admin_context;
67
68
69
70
71
  }

  /**
   * {@inheritdoc}
   */
72
  public function applies(RouteMatchInterface $route_match) {
73
    return ($this->entityManager->hasHandler('user_role', 'storage') && $this->user->hasPermission('view the administration theme') && $this->adminContext->isAdminRoute($route_match->getRouteObject()));
74
75
76
77
78
  }

  /**
   * {@inheritdoc}
   */
79
  public function determineActiveTheme(RouteMatchInterface $route_match) {
80
81
82
83
    return $this->configFactory->get('system.theme')->get('admin');
  }

}