-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
CivicrmThemeNegotiator.php
137 lines (112 loc) · 3.42 KB
/
CivicrmThemeNegotiator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Drupal\civicrmtheme\Theme;
use Drupal\civicrm\Civicrm;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Theme\ThemeNegotiatorInterface;
/**
* Theme negotiator for CiviCRM pages.
*/
class CivicrmThemeNegotiator implements ThemeNegotiatorInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $user;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The Civicrm service.
*
* @var \Drupal\civicrm\Civicrm
*/
protected $civicrm;
/**
* Constructs a CivicrmThemeNegotiator.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The current user service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\civicrm\Civicrm $civicrm
* The CiviCRM service.
*/
public function __construct(AccountInterface $user, ConfigFactoryInterface $config_factory, Civicrm $civicrm) {
$this->user = $user;
$this->configFactory = $config_factory;
$this->civicrm = $civicrm;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
$route = $route_match->getRouteObject();
// Some pages, like 404 pages, don't have a route objet.
if (!$route) {
return FALSE;
}
$parts = explode('/', ltrim($route->getPath(), '/'));
if ($parts[0] != 'civicrm') {
return FALSE;
}
if (count($parts) > 1 && $parts[1] == 'upgrade') {
return FALSE;
}
$config = $this->configFactory->get('civicrmtheme.settings');
$admin_theme = $config->get('admin_theme');
$public_theme = $config->get('public_theme');
if (!$admin_theme && !$public_theme) {
return FALSE;
}
// Attempt to initialize CiviCRM.
try {
$this->civicrm->initialize();
}
catch (\Exception $e) {
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function determineActiveTheme(RouteMatchInterface $route_match) {
$path = ltrim($route_match->getRouteObject()->getPath(), '/');
$config = $this->configFactory->get('civicrmtheme.settings');
$admin_theme = $config->get('admin_theme');
$public_theme = $config->get('public_theme');
// If neither the admin_theme or public theme have been set, we return NULL
// to let Drupal choose the correct active theme.
if (!$admin_theme && !$public_theme) {
return NULL;
}
// If the public theme is configured and the user does not have permission
// to access CiviCRM pages, use the public theme.
if (!$this->user->hasPermission('access CiviCRM')) {
if ($public_theme) {
return $public_theme;
}
return NULL;
}
// Initialize CiviCRM and get the CiviCRM menu item definition for this
// path.
$this->civicrm->initialize();
$item = \CRM_Core_Menu::get($path);
// If the current menu item is public, we use the public theme, in other
// cases the admin_theme is used.
if (\CRM_Utils_Array::value('is_public', $item) && $public_theme) {
return $public_theme;
}
// If the current menu item is not public apply civicrm admin theme.
if (!\CRM_Utils_Array::value('is_public', $item) && ($admin_theme)) {
return $admin_theme;
}
return NULL;
}
}