-
Notifications
You must be signed in to change notification settings - Fork 8
/
localgov_alert_banner.module
152 lines (132 loc) · 4.74 KB
/
localgov_alert_banner.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
145
146
147
148
149
150
151
152
<?php
/**
* @file
* Contains localgov_alert_banner.module.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function localgov_alert_banner_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the localgov_alert_banner module.
case 'help.page.localgov_alert_banner':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Alert Banner Support module') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_preprocess_localgov_alert_banner().
*/
function localgov_alert_banner_preprocess(&$variables) {
if (isset($variables['elements']['#localgov_alert_banner'])) {
// Get token.
$token = $variables['elements']['#localgov_alert_banner']->getToken();
// Add a hidden class.
$variables['attributes']['class'][] = 'hidden';
// Token as attribute.
$variables['attributes']['data-dismiss-alert-token'] = $token;
// Remove the content moderation form if it is present.
// Do this only on the confirmation form page.
$route_match = \Drupal::routeMatch();
$route_name = $route_match->getRouteName();
if ($route_name == 'entity.localgov_alert_banner.status_form') {
unset($variables['content']['content_moderation_control']);
}
// Set is_front variable.
try {
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
}
catch (Exception $e) {
$variables['is_front'] = FALSE;
}
}
}
/**
* Implements hook_theme().
*/
function localgov_alert_banner_theme() {
$theme = [];
$theme['localgov_alert_banner'] = [
'render element' => 'elements',
'file' => 'localgov_alert_banner.page.inc',
'template' => 'localgov-alert-banner',
];
$theme['localgov_alert_banner_content_add_list'] = [
'render element' => 'content',
'variables' => ['content' => NULL],
'file' => 'localgov_alert_banner.page.inc',
];
return $theme;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function localgov_alert_banner_theme_suggestions_localgov_alert_banner(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#localgov_alert_banner'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'localgov_alert_banner__' . $sanitized_view_mode;
$suggestions[] = 'localgov_alert_banner__' . $entity->bundle();
$suggestions[] = 'localgov_alert_banner__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'localgov_alert_banner__' . $entity->id();
$suggestions[] = 'localgov_alert_banner__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_modules_installed().
*/
function localgov_alert_banner_modules_installed($modules, $is_syncing) {
// Configure scheduled transitions if it's being installed.
if (in_array('scheduled_transitions', $modules, TRUE)) {
localgov_alert_banner_configure_scheduled_transitions();
}
}
/**
* Configure scheduled transitions.
*/
function localgov_alert_banner_configure_scheduled_transitions() {
// Configure scheduled transitions for alert banners.
$scheduled_transitions_config = \Drupal::service('config.factory')->getEditable('scheduled_transitions.settings');
$alert_banner_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('localgov_alert_banner');
$bundles = $scheduled_transitions_config->get('bundles');
foreach ($alert_banner_bundles as $bundle => $info) {
$bundles[] = [
'entity_type' => 'localgov_alert_banner',
'bundle' => $bundle,
];
}
$scheduled_transitions_config->set('bundles', $bundles);
$scheduled_transitions_config->save();
Cache::invalidateTags([
'scheduled_transition_settings',
'config:scheduled_transitions.settings',
]);
// Add scheduling permissions to the emergency publisher user.
$permissions = [];
foreach ($alert_banner_bundles as $bundle => $info) {
$permissions = array_merge($permissions, [
'add scheduled transitions localgov_alert_banner ' . $bundle,
'reschedule scheduled transitions localgov_alert_banner ' . $bundle,
'view scheduled transitions localgov_alert_banner ' . $bundle,
]);
}
user_role_grant_permissions('emergency_publisher', $permissions);
}
/**
* Implements hook_preprocess_field().
*/
function localgov_alert_banner_preprocess_field(&$variables) {
if ($variables['element']['#field_name'] == 'link' && $variables['element']['#bundle'] == 'localgov_alert_banner') {
foreach ($variables['element']['#items'] as $item) {
if (empty($item->getValue()['title'])) {
$default_text = t('More information');
$variables['items'][0]['content']['#title'] = $default_text;
}
}
}
}