Newer
Older
<?php
use Drupal\Core\Datetime\DrupalDateTime;
function buckeye_alert_preprocess_page(&$variables) {
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
$config = \Drupal::config('buckeye_alert.buckeye_alert_config');
$buckeye_alert_url = $config->get('buckeye_alert_url');
$buckeye_alert_test = $config->get('buckeye_alert_test');
$buckeye_alert_addl_date = $config->get('buckeye_alert_addl_date');
$buckeye_alert_addl_expire = $config->get('buckeye_alert_addl_expire');
$buckeye_alert_class = $config->get('buckeye_alert_class');
$buckeye_alert_animate = $config->get('buckeye_alert_animate');
$buckeye_alert_addl = $config->get('buckeye_alert_addl');
//use the test feed if checked
if ($buckeye_alert_test) {
$url = '//www.osu.edu/feeds/alert-test/feed.rss';
} else {
$url = $buckeye_alert_url;
}
$additional_html = '';
$now = new DrupalDateTime();
if($buckeye_alert_addl_date !== null) {
//get additional alert date and time
$buckeye_alert_addl_date = new DrupalDateTime($config->get('buckeye_alert_addl_date'));
//calculate expiration time
$expires = new DrupalDateTime($config->get('buckeye_alert_addl_date'));
$expires->add(new DateInterval('PT'.$buckeye_alert_addl_expire.'H'));
//show additional message if the time has not expired
if ($now >= $buckeye_alert_addl_date && $now < $expires) {
$additional_html = $buckeye_alert_addl;
}
}
//attach js libraries and pass drupal variables to dom
$variables['#attached']['library'][] = 'buckeye_alert/buckeye_alert';
$variables['#attached']['drupalSettings']['buckeye_alert']['feed_url'] = $url;
$variables['#attached']['drupalSettings']['buckeye_alert']['messageClass'] = $buckeye_alert_class;
$variables['#attached']['drupalSettings']['buckeye_alert']['animate'] = $buckeye_alert_animate;
$variables['#attached']['drupalSettings']['buckeye_alert']['additional'] = $additional_html;
}