-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsilverback_publisher_monitor.module
144 lines (131 loc) · 3.89 KB
/
silverback_publisher_monitor.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
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
138
139
140
141
142
143
144
<?php
use Drupal\Core\Url;
/**
* Retrieve the url publisher is available at.
* Defaults to http://localhost:8000.
*
* @return string
*/
function publisher_url() {
return getenv('PUBLISHER_URL') ?: 'http://localhost:8000';
}
/**
* Implements hook_library_info_build().
*/
function silverback_publisher_monitor_library_info_build() {
return [
'ws_indicator' => [
'js' => [
'js/ws-indicator.js' => [],
],
'css' => [
'theme' => [
'css/indicator.css' => [],
],
],
],
];
}
function silverback_publisher_monitor_page_attachments(&$variables) {
$websocketUrl = publisher_url();
$websocketUrl = str_replace('http://', 'ws://', $websocketUrl);
$websocketUrl = str_replace('https://', 'wss://', $websocketUrl);
$websocketUrl = $websocketUrl . '/___status/updates';
$variables['#attached']['library'][] = 'silverback_publisher_monitor/ws_indicator';
$variables['#attached']['drupalSettings']['publisherUpdateWebsocketUrl'] = $websocketUrl;
}
/**
* Implements hook_toolbar().
*/
function silverback_publisher_monitor_toolbar() {
$items = [];
$currentUser = \Drupal::currentUser();
if (!$currentUser->hasPermission('view publisher status')) {
return $items;
}
$hasTriggerBuildPermission = $currentUser->hasPermission('trigger a gatsby build');
$hasAccessPublisherPermission = $currentUser->hasPermission('access publisher');
$statusElement = [
'#type' => 'link',
'#title' => t('Publisher status'),
'#url' => Url::fromRoute('<current>'),
'#options' => [
'attributes' => [
'class' => ['silverback-publisher__monitor-indicator'],
],
],
'#cache' => [
'max-age' => 0,
],
];
// Fallback to the status element if no permissions are set.
// As we don't want to have a drop button without any links.
$tab = [$statusElement];
$wrapperClass = 'silverback-publisher-indicator-tab';
if ($hasTriggerBuildPermission || $hasAccessPublisherPermission) {
try {
/* @var \Drupal\Core\Routing\RouteProviderInterface $routeProvider */
$routeProvider = \Drupal::service('router.route_provider');
$routeProvider->getRouteByName('silverback_gatsby.build');
$buildUrl = Url::fromRoute('silverback_gatsby.build');
} catch (\Throwable $e) {
// Backwards compatibility for older versions of silverback_gatsby.
$buildUrl = Url::fromUserInput('#');
$hasTriggerBuildPermission = FALSE;
}
$buildLink = [
'#type' => 'link',
'#title' => t('Queue Gatsby build'),
'#url' => $buildUrl,
'#options' => [
'attributes' => [
'class' => ['use-ajax'],
],
],
'#access' => $hasTriggerBuildPermission,
];
$logsLink = [
'#type' => 'link',
'#title' => t('Publisher logs'),
'#url' => Url::fromUri(publisher_url() . '/___status'),
'#options' => [
'attributes' => [
'target' => '_blank',
],
],
'#access' => $hasAccessPublisherPermission,
];
$dropButton = [
'#type' => 'dropbutton',
'#dropbutton_type' => 'small',
'#links' => [
'status' => [
'title' => $statusElement,
],
// Default #links children does not support options,
// so prepare a link render array and use the title
// as a placeholder.
// We could do some more advanced overrides or introduce
// a new type that extends dropbutton, but keeping
// it simple for now.
'build' => [
'title' => $buildLink,
],
'logs' => [
'title' => $logsLink,
],
],
];
$tab = $dropButton;
$wrapperClass = 'silverback-publisher-drop-button';
}
$items['silverback_publisher_monitor'] = [
'#type' => 'toolbar_item',
'tab' => $tab,
'#wrapper_attributes' => [
'class' => [$wrapperClass],
],
'#weight' => 2000,
];
return $items;
}