-
Notifications
You must be signed in to change notification settings - Fork 0
/
stanford_soe_helper.module
executable file
·429 lines (389 loc) · 14.5 KB
/
stanford_soe_helper.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
/**
* @file
* Code for the Stanford SOE Helper module.
*/
/**
* Implements hook_help().
*/
function stanford_soe_helper_help($path, $arg) {
if ($path == 'admin/help#stanford_soe_helper') {
$output = '<h3>' . t('About the Stanford SoE Helper') . '</h3>';
$output .= '<p>' . t('The Stanford SoE Helper module provides settings specific for the SoE site.') . '</p>';
$output .= l(t("See README.md"), drupal_get_path('module', 'stanford_soe_helper') . '/README.md');
return $output;
}
}
/**
* Implements template_preprocess_page().
*/
function stanford_soe_helper_preprocess_page(&$variables) {
$file_path = drupal_get_path('module', 'stanford_soe_helper')
. "/css/soe_helper.css";
// CSS Injector file weights start at 200.
// See http://www.rit.edu/drupal/api/drupal/sites!all!modules!css_injector!css_injector.module/7.43.
$weight = 190;
$conditions = "admin
admin*
admin/*
batch*
block/*/edit*
block/add*
block/*/delete
js/admin_menu/cache/*
node/*/edit
node/add*
node/*/delete
user/*/edit";
$css_rules = array();
$css_rules['rule_conditions'] = $conditions;
$css_rules['rule_type'] = CSS_INJECTOR_PAGES_NOTLISTED;
$pass = _css_injector_evaluate_rule($css_rules);
if ($pass && (!path_is_admin(current_path()))) {
$variables['page']['content']['#attached']['css'] = array(
$file_path => array(
'group' => CSS_THEME,
'weight' => $weight,
),
);
}
// Programmatically adding views title to main_top region.
$views_page = views_get_page_view();
if (is_object($views_page)) {
$test = _stanford_soe_helper_view_title($views_page->name, $views_page->current_display);
if ($test) {
$current_display = $views_page->current_display;
if (isset($views_page->display[$current_display]->display_options['title'])) {
$title = $views_page->display[$current_display]->display_options['title'];
}
else {
$path = drupal_get_path_alias(current_path());
$parts = explode('/', $path);
if (isset($parts[1]) && $parts[1] != 'all') {
$title = $parts[1];
}
else {
$title = $parts[0];
}
$title = ucwords(str_replace('-', ' ', $title));
}
$variables['page']['main_top'][] = [
'#markup' => '<h1>' . $title . '</h1>',
];
}
}
$collections = FALSE;
// If the page is part of a collection, take note.
if (isset($variables['page']['content']['system_main']['nodes'])) {
foreach ($variables['page']['content']['system_main']['nodes'] as $node) {
if (isset($node['field_s_mag_article_collection'])) {
$collections = TRUE;
}
}
}
// If we are on a mag article that is not part of a collection.
if (!$collections) {
if (isset($variables['page']['content_bottom']) && isset($variables['page']['fullwidth_bottom']['bean_stanford-soe-mag-news-signup'])) {
// Move the mag news signup between related depts and related articles.
if (!isset($variables['page']['full_width_middle']['bean_stanford-soe-mag-news-signup'])) {
array_unshift($variables['page']['content_bottom'], $variables['page']['fullwidth_bottom']);
}
// Unset the block from showing in the fullwidth bottom region.
unset($variables['page']['fullwidth_bottom']['bean_stanford-soe-mag-news-signup']);
}
}
}
/**
* Implements hook_preprocess_field().
*/
function stanford_soe_helper_preprocess_field(&$vars) {
// On the magazine article page we need to set the color
// for the link back to the collection if it exists.
switch ($vars['element']['#field_name']) {
case 'field_s_mag_article_collection':
$term_accent_color = taxonomy_term_load($vars['element']['#object']->field_s_mag_article_collection['und'][0]['entity']->field_s_col_article_accent_color['und'][0]['tid']);
$accent_color = $term_accent_color->name;
$vars['items'][0]['#markup'] = str_replace('<a ', '<a class="' . $accent_color . '" ', $vars['items'][0]['#markup']);
break;
}
}
/**
* Implements hook_page_build().
*/
function stanford_soe_helper_page_build(&$page) {
// Get the collection if there is one.
if (isset($page['content']['system_main']['nodes'])) {
foreach ($page['content']['system_main']['nodes'] as $node) {
if (!empty($node['body']['#object']->field_s_mag_article_collection)) {
// If we are on an Article that is part of a collection,
// hide the related article block and the issues block.
if (isset($page['content_bottom']['views_a06b957e34c741c20a352da1b7ce0e12'])) {
unset($page['content_bottom']['views_a06b957e34c741c20a352da1b7ce0e12']);
}
if (isset($page['content_bottom']['views_ed6257626b15a2207dd0724b1a8cd3c4'])) {
unset($page['content_bottom']['views_ed6257626b15a2207dd0724b1a8cd3c4']);
}
if (isset($page['content_bottom']['stanford_soe_helper_magazine_soe_all_issues_link_2'])) {
unset($page['content_bottom']['stanford_soe_helper_magazine_soe_all_issues_link_2']);
}
}
}
}
}
/**
* Implements hook_menu().
*/
function stanford_soe_helper_menu() {
$items['pingdom/webhook'] = array(
'page callback' => 'stanford_soe_helper_cc',
'access callback' => 'verify_pingdom',
'type' => MENU_CALLBACK,
);
$items['pingdom/rr'] = array(
'page callback' => 'stanford_soe_helper_rr',
'access callback' => 'verify_pingdom',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Verify that we have a valid Pingdom token.
*
* @return bool
* Returns true if the token validates
*/
function verify_pingdom() {
$stanford_soe_helper_pingdom_token = variable_get('stanford_soe_helper_pingdom_token');
if (isset($_GET['token']) && $_GET['token'] == $stanford_soe_helper_pingdom_token) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Custom page callback.
*/
function stanford_soe_helper_cc() {
$flush_caches = &drupal_static(__FUNCTION__);
$fifteen_minutes_ago = strtotime('15 minutes ago');
$stanford_soe_helper_last_cc = variable_get('stanford_soe_helper_last_cc', 0);
if (!isset($flush_caches) && ($fifteen_minutes_ago > $stanford_soe_helper_last_cc)) {
$stanford_soe_helper_pingdom_token = variable_get('stanford_soe_helper_pingdom_token');
if (isset($_GET['token']) && $_GET['token'] == $stanford_soe_helper_pingdom_token) {
drupal_flush_all_caches();
watchdog('pingdom', 'drupal cache was cleared after pingdom alert');
variable_set('stanford_soe_helper_last_cc', REQUEST_TIME);
}
}
return '';
}
/**
* Implements hook_form_alter().
*/
function stanford_soe_helper_form_alter(
&$form,
&$form_state,
$form_id) {
// Handle deprecation of image credits.
switch ($form_id) {
case "stanford_page_node_form":
case "bean_form":
case "stanford_landing_page_node_form":
case "stanford_private_page_node_form":
case "stanford_publication_node_form":
case "stanford_event_node_form":
case "stanford_news_item_node_form":
case "stanford_event_series_node_form":
_stanford_soe_helper_deprecate_image_credits($form, $form_id);
break;
}
}
/**
* Implements hook_form_FORM_ID_alter() for stanford_search_api_search_block_form.
*/
function stanford_soe_helper_form_stanford_search_api_search_block_form_alter(
&$form,
&$form_state,
$form_id) {
$form['keywords']['#attributes']['placeholder'] = t("Search");
}
/**
* Handle tasks for deprecating the image credits.
*
* - Don't display the credits field unless there is text in that field.
* - Update all the appropriate title and description fields to handle
* deprecation.
*
* @param array $form
* The form to update.
* @param string $form_id
* The form ID. Make sure we're making changes to the right form!
*/
function _stanford_soe_helper_deprecate_image_credits(&$form, $form_id) {
// Don't display the credits!
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits']['#access'] =
FALSE;
// Well, okay, if there *is* text in the credits field,
// then let's display the credits field.
if (isset($form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits'][LANGUAGE_NONE]['0']['value']['#default_value'])) {
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits']['#access'] =
TRUE;
}
// Update caption title.
// Not sure which fields to target, so target all the things!
$caption_title = t("Caption and Credits");
// We want a different title if it's a Stanford Page node.
if ($form_id == "stanford_page_node_form") {
$caption_title = t("Banner Text");
}
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_caption'][LANGUAGE_NONE]['#title'] =
$caption_title;
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_caption'][LANGUAGE_NONE]['0']['#title'] =
$caption_title;
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_caption'][LANGUAGE_NONE]['0']['value']['#title'] =
$caption_title;
// Caption help text.
$caption_text = t('Use this field to display <strong>both</strong> the image caption and credits for the photographer or organization. Enter the caption, a pipe "|", and the credits.');
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_caption'][LANGUAGE_NONE]['#description'] =
$caption_text;
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_caption'][LANGUAGE_NONE]['0']['#description'] =
$caption_text;
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits'][LANGUAGE_NONE]['0']['value']['#description'] =
$caption_text;
// Credits title.
$credits_title =
t("Credits ** Please use the @caption_title field ** see @caption_title field help text below.",
array('@caption_title' => $caption_title));
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits'][LANGUAGE_NONE]['#title'] =
$credits_title;
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits'][LANGUAGE_NONE]['0']['#title'] =
$credits_title;
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits'][LANGUAGE_NONE]['0']['value']['#title'] =
$credits_title;
// Credits help text.
$credits_text = t("This field is no longer supported. Please move the text from this field into the @caption_title field.",
array('@caption_title' => $caption_title));
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits'][LANGUAGE_NONE]['#description'] =
$credits_text;
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits'][LANGUAGE_NONE]['0']['#description'] =
$credits_text;
$form['field_s_image_info'][LANGUAGE_NONE]['0']['field_s_image_credits'][LANGUAGE_NONE]['0']['value']['#description'] =
$credits_text;
}
/**
* Custom page callback.
*/
function stanford_soe_helper_rr() {
$registry_rebuild = &drupal_static(__FUNCTION__);
$fifteen_minutes_ago = strtotime('15 minutes ago');
$stanford_soe_helper_last_rr = variable_get('stanford_soe_helper_last_rr', 0);
if (!isset($registry_rebuild) && ($fifteen_minutes_ago > $stanford_soe_helper_last_rr)) {
$stanford_soe_helper_pingdom_token = variable_get('stanford_soe_helper_pingdom_token');
if (isset($_GET['token']) && $_GET['token'] == $stanford_soe_helper_pingdom_token) {
registry_rebuild();
drupal_flush_all_caches();
watchdog('pingdom', 'drupal registry was rebuilt after pingdom alert');
variable_set('stanford_soe_helper_last_rr', REQUEST_TIME);
}
}
return '';
}
/**
* Test to see if a bean exists.
*
* @return string
* If the bean exists, return the bean ID (BID).
*/
function _stanford_soe_helper_get_bean($delta) {
$query = db_select('bean', 'b');
$query->addField('b', 'bid');
$query->condition('delta', $delta, '=');
$result = $query->execute()->fetchField();
return $result;
}
/**
* Delete the given bean.
*
* If it exists, delete the bean.
*/
function _stanford_soe_helper_delete_bean($delta) {
$bid = _stanford_soe_helper_get_bean($delta);
if (!empty($bid)) {
$bean = bean_load($bid);
bean_delete($bean);
}
}
/**
* Implements hook_views_post_execute().
*/
function stanford_soe_helper_views_post_execute(&$view) {
// Removing title on view as we have placed it in the main_top region.
$test = _stanford_soe_helper_view_title($view->name, $view->current_display);
if ($test) {
$current_display = $view->current_display;
unset($view->display[$current_display]->handler->view->display[$current_display]->handler->options['title']);
$view->display[$current_display]->handler->view->display[$current_display]->handler->view->display[$current_display]->handler->view->build_info['substitutions']['%1'] = '';
if ($view->name == 'soe_opportunity_grid' && $view->current_display == 'gep_page') {
unset($view->display[$current_display]->handler->view->display[$current_display]->handler->view->display[$current_display]->handler->view->build_info['title']);
}
}
}
/**
* Callback to test view title should be placed in main_top and removed.
*/
function _stanford_soe_helper_view_title($viewname, $displayname) {
$views = array('stanford_events_views',
'soe_school_news_with_teaser',
'stanford_person_grid',
'soe_opportunity_grid',
);
$displays = array('page', 'page_1', 'grouped_page', 'gep_page');
$viewtest = in_array($viewname, $views);
$displaytest = in_array($displayname, $displays);
if ($viewtest && $displaytest) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Implements hook_cron().
*
* Testing for solr being up and dropping a watchdog statement into dblog.
*/
function stanford_soe_helper_cron() {
if (function_exists('search_api_server_load_multiple')) {
$conditions = array('class' => 'search_api_solr_service', 'enabled' => TRUE);
foreach (search_api_server_load_multiple(FALSE, $conditions) as $server) {
$myService = new SearchApiSolrService($server);
$ping = $myService->ping();
if ($ping) {
watchdog('stanford_soe_helper', 'Solr is working');
}
else {
watchdog('stanford_soe_helper', 'Solr is down');
}
}
}
}
/**
* Implements hook_views_pre-render().
*
* De-duping view on collectiions.
*/
function stanford_soe_helper_views_pre_render(&$view) {
$used_nids = array();
if ($view->name == 'stanford_magazine_collection_mag_landing_page') {
if ($view->current_display == 'collection_group_1_block') {
foreach ($view->result as $row) {
if (!in_array($row->nid, $used_nids)) {
$new_view_result[] = $row;
$used_nids[] = $row->nid;
}
}
$view->result = $new_view_result;
}
}
}