Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
VisitorThemeNegotiator
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 applies
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 determineActiveTheme
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Drupal\visitors\Theme;
4
5use Drupal\Core\Config\ConfigFactoryInterface;
6use Drupal\Core\Routing\RouteMatchInterface;
7use Drupal\Core\Theme\ThemeNegotiatorInterface;
8use Drupal\Core\Session\AccountInterface;
9
10/**
11 * A negotiator for custom visitors' theme.
12 */
13class VisitorThemeNegotiator implements ThemeNegotiatorInterface {
14
15  /**
16   * The config factory.
17   *
18   * @var \Drupal\Core\Config\ConfigFactoryInterface
19   */
20  protected $configFactory;
21
22  /**
23   * The current user.
24   *
25   * @var \Drupal\Core\Session\AccountInterface
26   */
27  protected $currentUser;
28
29  /**
30   * Constructs a new ThemeNegotiator.
31   *
32   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
33   *   The config factory.
34   * @param \Drupal\Core\Session\AccountInterface $current_user
35   *   The current user.
36   */
37  public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user) {
38    $this->configFactory = $config_factory;
39    $this->currentUser = $current_user;
40  }
41
42  /**
43   * {@inheritdoc}
44   */
45  public function applies(RouteMatchInterface $route_match) {
46    $access = $this->currentUser->hasPermission('access visitors');
47    if (!$access) {
48      return FALSE;
49    }
50
51    $route_object = $route_match->getRouteObject();
52    if (!$route_object) {
53      return FALSE;
54    }
55
56    $route_name = $route_match->getRouteName();
57    if (strpos($route_name, 'visitors.') === 0) {
58      return TRUE;
59    }
60    $path = $route_object->getPath();
61    if (strpos($path, '/visitors') === 0) {
62      return TRUE;
63    }
64
65    return FALSE;
66  }
67
68  /**
69   * {@inheritdoc}
70   */
71  public function determineActiveTheme(RouteMatchInterface $route_match) {
72    // Get the visitors config.
73    $config = $this->configFactory->get('visitors.config');
74    $theme = $config->get('theme') ?: 'admin';
75
76    return $this->configFactory->get('system.theme')->get($theme) ?: $theme;
77  }
78
79}