-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasu_brand.module
400 lines (348 loc) · 12.6 KB
/
asu_brand.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* ASU Brand module.
*
* Author: Marcus Anthony ([email protected])
* Created: 2011-09-21
*/
/*********************************************************************
CONFIG
*********************************************************************/
define('ASU_BRAND_HEADER_VERSION_XML_URL', 'http://asu.edu/asuthemes/headers.xml');
define('ASU_BRAND_HEADER_VERSION_DEFAULT', 'stable');
define('ASU_BRAND_HEADER_BASEPATH_DEFAULT', 'http://www.asu.edu/asuthemes');
define('ASU_BRAND_HEADER_TEMPLATE_DEFAULT', 'default');
define('ASU_BRAND_FOOTER_COLOR_DEFAULT', '');
define('ASU_BRAND_CACHE_LIFETIME', 172800); // 48 Hours ( 60 x 60 x 48 )
define('ASU_BRAND_PREVIEW_PAGES_DEFAULT', "system/ajax/*\npanels/ajax/*");
define('ASU_BRAND_DO_NOT_OVERRIDE', variable_get('asu_brand_do_not_override', '["www.asu.edu"]'));
/**
* Delta of ASU Header block (declared in asu_brand.module)
* @var string
*/
define('ASU_BRAND_HEADER_DELTA', 'asu_brand_header');
/**
* Default machine name of site menu to inject into the responsive header menu
* @var string
*/
define('ASU_BRAND_SITE_MENU_NAME_DEFAULT', 'main-menu');
/**
* Implements hook_init().
*/
function asu_brand_init() {
if (variable_get('asu_brand_sitemenu_injection_flag', 1)) {
$menu_array = asu_brand_get_site_menu_array();
drupal_alter('asu_brand_sitemenu', $menu_array); // Invoke alter
$site_name = asu_brand_get_site_name();
drupal_alter('asu_brand_sitename', $site_name); // Invoke alter
$site_url = variable_get('asu_brand_override_url', '/');
if (empty($site_url)) {
$site_url = '/';
}
drupal_alter('asu_brand_site_url', $site_url); // Invoke alter
$parent_org_title = variable_get('asu_brand_parent_org_title', '');
$parent_org_url = variable_get('asu_brand_parent_org_url', '');
$site_title_array = array(
'title' => $site_name,
'site_url' => $site_url
);
if (!empty($parent_org_title)) {
$site_title_array['parent_org'] = $parent_org_title;
}
if (!empty($parent_org_url)) {
$site_title_array['parent_org_url'] = $parent_org_url;
}
//ASUHeader.site_title = {title: "Name of Site", parent_org: "Name of Parent Organization"};
$js = 'window.ASUHeader = window.ASUHeader || {};';
$js .= 'ASUHeader.site_menu = ASUHeader.site_menu || {};';
$js .= 'ASUHeader.site_menu.json = \''.json_encode($menu_array, JSON_HEX_APOS).'\';';
$js .= 'ASUHeader.site_menu.site_name = '.json_encode($site_name, JSON_HEX_APOS).';';
$js .= 'ASUHeader.site_title = '.json_encode($site_title_array, JSON_HEX_APOS).';';
if (variable_get('asu_brand_ci_testing', 0) == 1) {
$js .= 'ASUHeader.disableCookieConsent = true;';
}
drupal_add_js($js, array('type' => 'inline', 'scope' => 'header', 'group' => JS_THEME, 'weight' => -10));
}
}
/**
* Implements hook_cron().
*/
function asu_brand_cron() {
// Reset CI testing bypass of GTM code back to 0, just in case testing failed
// to complete and left on filtering
variable_set('asu_brand_ci_testing', 0);
}
/**
* Implements hook_block_info().
*/
function asu_brand_block_info() {
// Never cache the header with Drupal's caching system because the contents of
// the block changes based on if the user is logged in. The included header files
// are cached manually when then block is viewed in _asu_brand_block_header().
$blocks[ASU_BRAND_HEADER_DELTA] = array(
'info' => t('ASU Brand: header'),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['asu_brand_footer'] = array(
'info' => t('ASU Brand: footer'),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['asu_brand_students_footer'] = array(
'info' => t('ASU Brand: students footer'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function asu_brand_block_configure($delta = '') {
$form = array();
switch ($delta) {
case ASU_BRAND_HEADER_DELTA:
case 'asu_brand_footer':
case 'asu_brand_students_footer':
module_load_include('inc', 'asu_brand', 'asu_brand.admin');
$form = asu_brand_get_block_configure_form($delta);
break;
}
return $form;
}
/**
* Implements hook_block_save().
*/
function asu_brand_block_save($delta = '', $edit = array()) {
switch ($delta) {
case 'asu_brand_header':
variable_set('asu_brand_header_version', $edit['asu_brand_header_version']);
variable_set('asu_brand_header_basepath', $edit['asu_brand_header_basepath']);
variable_set('asu_brand_header_template', $edit['asu_brand_header_template']);
variable_set('asu_brand_preview_pages', $edit['asu_brand_preview_pages']);
variable_set('asu_brand_gtm_override', $edit['asu_brand_gtm_override']);
variable_set('asu_brand_sitemenu_injection_flag', $edit['asu_brand_sitemenu_injection_flag']);
variable_set('asu_brand_sitemenu_name', $edit['asu_brand_sitemenu_name']);
if (isset($edit['asu_brand_do_not_override'])) {
// explode and trim input on commas, then check for plain non-empty input
$overrides = array_map('trim', explode(',', $edit['asu_brand_do_not_override']));
$overrides = array_filter($overrides, function($val) {
$val = check_plain($val);
return !empty($val);
});
variable_set('asu_brand_do_not_override', drupal_json_encode($overrides));
}
variable_set('asu_brand_parent_org_title', $edit['asu_brand_parent_org_title']);
variable_set('asu_brand_parent_org_url', $edit['asu_brand_parent_org_url']);
variable_set('asu_brand_override_url', $edit['asu_brand_override_url']);
asu_brand_cache_clear();
break;
case 'asu_brand_footer':
variable_set('asu_brand_header_version', $edit['asu_brand_header_version']);
variable_set('asu_brand_header_basepath', $edit['asu_brand_header_basepath']);
// No color options on footer anymore
//variable_set('asu_brand_footer_color', $edit['asu_brand_footer_color']);
variable_set('asu_brand_preview_pages', $edit['asu_brand_preview_pages']);
asu_brand_cache_clear();
break;
case 'asu_brand_students_footer':
variable_set('asu_brand_header_version', $edit['asu_brand_header_version']);
variable_set('asu_brand_header_basepath', $edit['asu_brand_header_basepath']);
variable_set('asu_brand_preview_pages', $edit['asu_brand_preview_pages']);
asu_brand_cache_clear();
break;
}
}
/**
* Implements hook_block_view().
*/
function asu_brand_block_view($delta = '') {
$block = array();
$is_preview_path = asu_brand_is_preview_path();
$preview_text = t('No preview available');
switch ($delta) {
case 'asu_brand_header':
$block['subject'] = NULL;
if ($is_preview_path) {
$block['content'] = $preview_text;
}
else {
module_load_include('inc', 'asu_brand', 'asu_brand.block');
$block['content'] = asu_brand_get_block_header();
}
break;
case 'asu_brand_footer':
$block['subject'] = NULL;
if ($is_preview_path) {
$block['content'] = $preview_text;
}
else {
module_load_include('inc', 'asu_brand', 'asu_brand.block');
$block['content'] = asu_brand_get_block_footer();
}
break;
case 'asu_brand_students_footer':
$block['subject'] = NULL;
if ($is_preview_path) {
$block['content'] = $preview_text;
}
else {
module_load_include('inc', 'asu_brand', 'asu_brand.block');
$block['content'] = asu_brand_get_block_students_footer();
}
break;
}
return $block;
}
/**
* Implements hook_page_alter().
* Adds a post_render callback and loads the include file.
* @see drupal_render_page()
*/
function asu_brand_page_alter(&$page) {
// Call sequence:
// - drupal_render_page()
// - hook_page_alter()
// - drupal_render()
// - drupal_render()
// - callbacks in $elements['#theme_wrappers']
// - hook_preprocess_html(): 'html' is the wrapper for page
// - templates may add tags after body tag
// - callbacks in $elements['#post_render']
// - asu_brand_page_process(): callback set here
// Add callback routine.
$page['#post_render'][] = 'asu_brand_page_process';
// Load include file.
module_load_include('inc', 'asu_brand', 'asu_brand.gtm');
}
/**
* Clear block caches.
*
* If called without arguments, clears all asu_brand related items. $cid can be
* any asu_brand item (without the 'asu_brand:' part.
*/
function asu_brand_cache_clear($cid = '', $wildcard = TRUE) {
cache_clear_all('asu_brand:' . $cid, 'cache', $wildcard);
}
/**
* Get list of cache IDs.
*/
function asu_brand_get_cache_ids($reset = FALSE) {
$cache_ids = &drupal_static(__FUNCTION__);
if ($reset || !isset($cache_ids)) {
$cache_ids = array('asu_brand:head', 'asu_brand:header', 'asu_brand:footer', 'asu_brand:students_footer', 'asu_brand:gtm');
}
return $cache_ids;
}
/**
* Determine if path is generating a preview.
*/
function asu_brand_is_preview_path($reset = FALSE) {
$page_match = &drupal_static(__FUNCTION__);
if ($reset || !isset($page_match)) {
$pages = variable_get('asu_brand_preview_pages', ASU_BRAND_PREVIEW_PAGES_DEFAULT);
// Convert the Drupal path to lowercase.
$path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
// Compare the lowercase internal and lowercase path alias (if any).
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
}
return $page_match;
}
/**
* Get site name displayed in mobile menu
* @return String
*/
function asu_brand_get_site_name() {
$site_name = variable_get('site_name', '');
return $site_name;
}
/**
* Returns site menu array
*/
function asu_brand_get_site_menu_array() {
$menu_tree = menu_tree_all_data(variable_get('asu_brand_sitemenu_name', ASU_BRAND_SITE_MENU_NAME_DEFAULT));
$menu_array = array();
$i=0;
foreach ($menu_tree as $item) {
if (isset($item['link']) && $item['link']['access'] && !$item['link']['hidden']) {
if ($menu_item = asu_brand_get_menu_item($item['link']['title'], $item['link']['link_path'], $item['link']['options'])) {
$menu_array[$i] = array_map('trim', $menu_item);
} else {
$i--;
}
// Render child items.
if (asu_brand_menuitem_has_active_children($item)) {
$j=0;
foreach ($item['below'] as $child) {
if (isset($child['link']) && !$child['link']['hidden']) {
if ($menu_item = asu_brand_get_menu_item($child['link']['title'], $child['link']['link_path'], $child['link']['options'])) {
$menu_array[$i]['children'][$j] = array_map('trim', $menu_item);
} else {
$j--;
}
// Render grandchild items.
if (asu_brand_menuitem_has_active_children($child)) {
$k=0;
foreach ($child['below'] as $grandchild) {
if (isset($grandchild['link']) && !$grandchild['link']['hidden']) {
if ($menu_item = asu_brand_get_menu_item($grandchild['link']['title'], $grandchild['link']['link_path'], $grandchild['link']['options'])) {
$menu_array[$i]['children'][$j]['children'][$k] = array_map('trim', $menu_item);
} else {
$k--;
}
$k++;
}
}
}
$j++;
}
}
}
$i++;
}
}
return $menu_array;
}
/**
* Compose and return menu item
* Special handling for 'Special menu items' module
* Return NULL for path if <nolink>
* Return empty array if <separator>
* @param atring $title
* @param atring $path
* @param assoc array $options
* @return assoc array>
*/
function asu_brand_get_menu_item($title, $path, $options = array()) {
// default
$menu_item = array(
'title' => t(strip_tags(htmlspecialchars_decode($title))),
'path' => url($path, $options)
);
// special handling for 'Special menu items' module
if (module_exists('special_menu_items')) {
switch ($path) {
case '<nolink>':
$menu_item['path'] = NULL;
break;
case '<separator>':
return array(); // return empty array if separator
break;
}
}
return $menu_item;
}
/**
* Parses menuitem and returns true if there are active child menu items.
* @param array $menuitem
* @return boolean
*/
function asu_brand_menuitem_has_active_children($menuitem) {
if (is_array($menuitem) && isset($menuitem['below']) && !empty($menuitem['below'])) {
foreach ($menuitem['below'] as $child) {
if (isset($child['link']) && $child['link']['access'] && ($child['link']['hidden'] == 0)) return true;
}
}
return false;
}