-
Notifications
You must be signed in to change notification settings - Fork 0
/
access.module
84 lines (73 loc) · 2.84 KB
/
access.module
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
<?php
/**
* @file
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Menu\MenuTreeParameters;
/**
* Implements hook_page_attachments().
*/
function access_page_attachments(array &$attachments) {
$token = \Drupal::token();
$domain_name = Html::getClass($token->replace(t('[domain:name]')));
if ($domain_name == "access-support") {
// Get 'access-secondary-menu' menu items and links.
$tree = \Drupal::menuTree()->load('access-secondary-menu', new MenuTreeParameters());
$menu = [];
foreach ($tree as $item) {
$title = $item->link->getTitle();
// Get weight.
$weight = $item->link->getWeight();
// Counteract negative weights.
$weight = $weight + 200;
$menu[$weight]['name'] = $title;
foreach ($item->subtree as $subitem) {
$sub_weight = $subitem->link->getWeight() + 200;
$sub_title = $subitem->link->getTitle();
$sub_url_obj = $subitem->link->getUrlObject();
$sub_url = $sub_url_obj->toString();
$menu[$weight]['items'][$sub_weight]['name'] = $sub_title;
$menu[$weight]['items'][$sub_weight]['url'] = $sub_url;
}
$mask = \Drupal::service('masquerade');
if ($title == 'Quick Links') {
if ($mask->isMasquerading()) {
$sub_weight = 400;
$menu[$weight]['items'][$sub_weight]['name'] = 'Unmask';
$menu[$weight]['items'][$sub_weight]['url'] = '/unmask';
}
}
if ($title == 'MATCH Services') {
$user = \Drupal::currentUser();
$roles = $user->getRoles();
if (in_array('match_pm', $roles) || in_array('match_sc', $roles) || in_array('administrator', $roles)) {
$menu[$weight]['items'][300]['name'] = 'MATCH Interested Users';
$menu[$weight]['items'][300]['url'] = '/match-interested-users';
$menu[$weight]['items'][301]['name'] = 'MATCH Engagement Submissions';
$menu[$weight]['items'][301]['url'] = '/match-engagements-submissions';
}
}
}
ksort($menu);
$js_menu = '[';
foreach ($menu as $menu_items) {
$js_menu .= '{';
$js_menu .= '"name": "' . $menu_items['name'] . '", "items": [';
ksort($menu_items['items']);
foreach ($menu_items['items'] as $item) {
$js_menu .= '{"name": "' . $item['name'] . '", "href": "' . $item['url'] . '"},';
}
// Remove last comma.
$js_menu = rtrim($js_menu, ',');
$js_menu .= ']},';
}
// Remove last comma.
$js_menu = rtrim($js_menu, ',');
$js_menu .= ']';
$current_uri = \Drupal::request()->getRequestUri();
// Adding global library to all pages.
$attachments['#attached']['drupalSettings']['access']['current_uri'] = $current_uri;
$attachments['#attached']['drupalSettings']['access']['current_menu'] = $js_menu;
$attachments['#attached']['library'][] = 'access/header_footer_library';
}
}