Skip to content
Snippets Groups Projects
Unverified Commit 873d34c0 authored by weaver299's avatar weaver299 Committed by GitHub
Browse files

Merge pull request #220 from ASCWebServices/canini

adding new module - buckeye alert
parents e6bd7013 4f3e06bc
No related branches found
No related tags found
No related merge requests found
Showing
with 385 additions and 0 deletions
Drupal 8 Buckeye Alerts
=======================
This module adds [BuckeyeAlerts](code.osu.edu/ucom/buckeye-alert) to the top of your Drupal 8 pages. The JavaScript and CSS was pulled directly from the BuckeyeAlert code.
Usage Notes
-----------
The settings page can be found under configuration->system->buckeye alerts. From there you will be able to manage the following settings:
* **Use test Buckeye Alerts** -- Determines whether or not to use the testing Buckeye Alerts feed. Defaults to off.
* **Alert feed URL** -- The RSS feed to check for alert messages.
* **Container class** -- Optional classes to add to message container.
* **Animate** -- Enables jQuery animations.
* **Add styles** -- Include the recommended alert message styles.
* **Responsive styles** -- Use the included responsive style sheet.
* **Breakpoints** -- Responsive CSS tablet and phone breakpoints.
To add the module to your site add the following to your theme's info.yml
libraries:
- buckeye_alert/buckeye_alert
Add the "Administer Buckeye Alerts" permission to any necessary roles.
name: 'Buckeye Alert'
type: module
description: 'Drupal 8 Module for displaying Buckeye Alert messages at the top of your webpage.'
core: 8.x
package: 'Buckeye Alert'
dependencies:
- 'drupal:datetime'
buckeye_alert:
version: 1.x
css:
theme:
css/buckeye-alert.css: {}
js:
js/buckeye-alert.js: {}
js/jquery.buckeye-alert.min.js: {}
js/jquery.d8-buckeye-alert.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
- core/drupal.ajax
buckeye_alert.settings:
title: 'Buckeye Alert'
parent: system.admin_config_system
route_name: 'buckeye_alert.config'
<?php
use Drupal\Core\Datetime\DrupalDateTime;
function buckeye_alert_preprocess_node(&$variables) {
$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;
}
administer buckeye alerts:
title: 'Administer Buckeye Alerts'
description: 'Gives users permissions to manage Buckeye Alerts.'
buckeye_alert.config:
path: 'admin/config/buckeye_alert'
defaults:
_form: '\Drupal\buckeye_alert\Form\BuckeyeAlertsConfigurationForm'
_title: 'Buckeye Alerts'
requirements:
_permission: 'administer buckeye alerts'
buckeye_alert_url: //www.osu.edu/feeds/emergency-alert.rss
buckeye_alert_test: 0
buckeye_alert_class: l-constrained
buckeye_alert_animate: 1
buckeye_alert_addl: ''
buckeye_alert_addl_date: null
buckeye_alert_addl_expire: '24'
.osu-semantic {position: absolute; left: 0; top: -500px; width: 1px; height: 1px; overflow: hidden;}
/* Buckeye Alert */
#buckeye_alert {background:#900; color:#fff; margin: 0; padding:0; display:none; font-family:Arial,sans-serif; font-weight: 700;}
#buckeye_alert h3 {color:#fff; padding:.5em 0 0 0; margin-bottom:0;}
#buckeye_alert p {padding:0.8em 0; margin:0; color: #fff;}
#buckeye_alert a {color:#fff; text-decoration:underline;}
#buckeye_alert_msg p {font-size:1.1em;}
#buckeye_alert_extra p {font-size:1em; padding-top:0;}
#buckeye_alert {font-family:'proximanova',Arial,sans-serif; font-weight:700;}
#buckeye_alert_extra {font-weight: 400;}
/**
* buckeyeAlert library
* Ohio State University - Interactive Communications
* http://ucom.osu.edu
*/
function buckeyeAlert(settings) {
if(typeof(settings)==='undefined') settings = new Object();
if(typeof(settings.element_id)==='undefined') settings.element_id = "buckeye_alert";
if(typeof(settings.url)==='undefined') settings.url = "//www.osu.edu/feeds/emergency-alert.rss";
if(typeof(settings.callback)==='undefined') settings.callback = function () { };
if(typeof(settings.displayType)==='undefined') settings.display = "block";
if(window.XDomainRequest){ // For IE
var xdr = new XDomainRequest();
xdr.open("GET", settings.url);
xdr.onprogress = function () { };
xdr.ontimeout = function () { };
xdr.onerror = function () { };
xdr.onload = function() {
var response;
if (window.DOMParser) {
var parser = new window.DOMParser();
response = parser.parseFromString(xdr.responseText, "text/xml");
} else {
response = new ActiveXObject("Microsoft.XMLDOM");
response.async = false;
response.loadXML(xdr.responseText);
}
displayResponse(response);
}
setTimeout(function () {xdr.send();}, 0);
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
displayResponse(xmlhttp.responseXML);
}
}
xmlhttp.open("GET", settings.url, true);
xmlhttp.send();
}
function displayResponse(response) {
var items = response.getElementsByTagName("item");
if (items.length) {
var heading = document.createElement("h2");
heading.className = "osu-semantic";
heading.innerHTML = "Emergency alert message";
var message = document.createElement("div");
message.className = settings.messageClass;
message.setAttribute("id", "buckeye_alert_msg");
for (var i=0; i<items.length; i++) {
var description = items[i].getElementsByTagName("description")[0];
var thisMessage = description.textContent || description.text;
if (thisMessage != 'undefined') {
message.innerHTML += thisMessage;
}
}
var container = document.getElementById(settings.element_id);
if (container.childNodes[0]) {
container.insertBefore(message, container.childNodes[0]);
} else {
container.appendChild(message);
}
container.insertBefore(heading, container.childNodes[0]);
container.setAttribute("role", "alert");
container.removeAttribute("hidden");
container.style.display = settings.display;
settings.callback();
}
}
}
function buckeyeAlert(a){function d(b){var c=b.getElementsByTagName("item");if(c.length){var d=document.createElement("h2");d.className="osu-semantic",d.innerHTML="Emergency alert message";var e=document.createElement("div");e.className=a.messageClass,e.setAttribute("id","buckeye_alert_msg");for(var f=0;f<c.length;f++){var g=c[f].getElementsByTagName("description")[0],h=g.textContent||g.text;"undefined"!=h&&(e.innerHTML+=h)}var i=document.getElementById(a.element_id);i.childNodes[0]?i.insertBefore(e,i.childNodes[0]):i.appendChild(e),i.insertBefore(d,i.childNodes[0]),i.setAttribute("role","alert"),i.removeAttribute("hidden"),i.style.display=a.display,a.callback()}}if("undefined"==typeof a&&(a=new Object),"undefined"==typeof a.element_id&&(a.element_id="buckeye_alert"),"undefined"==typeof a.url&&(a.url="//www.osu.edu/feeds/emergency-alert.rss"),"undefined"==typeof a.callback&&(a.callback=function(){}),"undefined"==typeof a.displayType&&(a.display="block"),window.XDomainRequest){var b=new XDomainRequest;b.open("GET",a.url),b.onprogress=function(){},b.ontimeout=function(){},b.onerror=function(){},b.onload=function(){var a;if(window.DOMParser){var c=new window.DOMParser;a=c.parseFromString(b.responseText,"text/xml")}else a=new ActiveXObject("Microsoft.XMLDOM"),a.async=!1,a.loadXML(b.responseText);d(a)},setTimeout(function(){b.send()},0)}else{var c=new XMLHttpRequest;c.onreadystatechange=function(){4==c.readyState&&200==c.status&&d(c.responseXML)},c.open("GET",a.url,!0),c.send()}}
\ No newline at end of file
!function(a){a.fn.buckeyeAlert=function(b){var c=a.extend({url:"//www.osu.edu/feeds/emergency-alert.rss",callback:function(){},messageClass:null,animate:!0},b);return this.each(function(){var b=a(this);a.get(c.url,function(d){var e=a(d).find("item");if(e.length){var f=a('<div id="buckeye_alert_msg"></div>').addClass(c.messageClass);e.each(function(){f.append(a(this).find("description").text())}),b.prepend(f).prepend('<h2 class="osu-semantic">Emergency alert message</h2>').attr("role","alert").removeAttr("hidden"),c.animate?b.slideDown("fast"):b.show(),c.callback()}})})}}(jQuery),function(a){if(!a.support.cors&&a.ajaxTransport&&window.XDomainRequest){var b=/^https?:\/\//i,c=/^get|post$/i,d=new RegExp("^"+location.protocol,"i"),e=/text\/html/i,f=/\/json/i,g=/\/xml/i;a.ajaxTransport("+*",function(h,i){if(h.crossDomain&&h.async&&c.test(h.type)&&b.test(h.url)&&d.test(h.url)){var k=null,l=(i.dataType||"").toLowerCase();return{send:function(b,c){k=new XDomainRequest,/^\d+$/.test(i.timeout)&&(k.timeout=i.timeout),k.ontimeout=function(){c(500,"timeout")},k.onload=function(){var b="Content-Length: "+k.responseText.length+"\r\nContent-Type: "+k.contentType,d={code:200,message:"success"},h={text:k.responseText};try{if("html"===l||e.test(k.contentType))h.html=k.responseText;else if("json"===l||"text"!==l&&f.test(k.contentType))try{h.json=a.parseJSON(k.responseText)}catch(i){d.code=500,d.message="parseerror"}else if("xml"===l||"text"!==l&&g.test(k.contentType)){var j=new ActiveXObject("Microsoft.XMLDOM");j.async=!1;try{j.loadXML(k.responseText)}catch(i){j=void 0}if(!j||!j.documentElement||j.getElementsByTagName("parsererror").length)throw d.code=500,d.message="parseerror","Invalid XML: "+k.responseText;h.xml=j}}catch(m){throw m}finally{c(d.code,d.message,h,b)}},k.onprogress=function(){},k.onerror=function(){c(500,"error",{text:k.responseText})};var d="";i.data&&(d="string"===a.type(i.data)?i.data:a.param(i.data)),k.open(h.type,h.url),k.send(d)},abort:function(){k&&k.abort()}}}})}}(jQuery);
\ No newline at end of file
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.BuckeyeAlert = {
attach: function (context, settings) {
// Only add alert if page is not embedded in another
if (window.location == window.parent.location) {
$('body', context).once('buckeye-alert-init').prepend('<div id="buckeye_alert"></div>');
$("#buckeye_alert").once('buckeye-alert').buckeyeAlert({
url: drupalSettings.buckeye_alert.feed_url,
messageClass: drupalSettings.buckeye_alert.messageClass,
animate: drupalSettings.buckeye_alert.animate,
callback: function() {
if (drupalSettings.buckeye_alert.additional) {
$("#buckeye_alert").find("#buckeye_alert_msg").append('<div id="buckeye_alert_extra">'+drupalSettings.buckeye_alert.additional+'</div>');
}
}
});
}
}
};
})(jQuery, Drupal, drupalSettings);
<?php
namespace Drupal\buckeye_alert\Form;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Xss;
class BuckeyeAlertsConfigurationForm extends ConfigFormBase {
protected function getEditableConfigNames() {
return ['buckeye_alert.buckeye_alert_config'];
}
public function getFormId() {
return 'buckeye_alert_configuration_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('buckeye_alert.buckeye_alert_config');
//get stored configuration values
$buckeye_alert_url = $config->get('buckeye_alert_url');
$buckeye_alert_test = $config->get('buckeye_alert_test');
$buckeye_alert_class = $config->get('buckeye_alert_class');
$buckeye_alert_animate = $config->get('buckeye_alert_animate');
$buckeye_alert_addl = $config->get('buckeye_alert_addl');
$buckeye_alert_addl_date = $config->get('buckeye_alert_addl_date');
$buckeye_alert_addl_expire = $config->get('buckeye_alert_addl_expire');
$form['intro'] = array(
'#markup' => t('<p>Controls the display of Buckeye Alert messages.</p>'),
);
$form['buckeye_alert_url'] = array(
'#type' => 'textfield',
'#title' => t('Alert feed URL'),
'#default_value' => isset($buckeye_alert_url) ? $buckeye_alert_url : '//www.osu.edu/feeds/emergency-alert.rss',
'#description' => t('Default: //www.osu.edu/feeds/emergency-alert.rss'),
'#required' => TRUE,
);
$form['buckeye_alert_test'] = array(
'#type' => 'checkbox',
'#title' => t('Use the test feed for Buckeye Alerts'),
'#default_value' => isset($buckeye_alert_test) ? $buckeye_alert_test : FALSE,
);
$form['buckeye_alert_class'] = array(
'#type' => 'textfield',
'#title' => t('Message container class'),
'#description' => t('A space separated list of classes for the inner alert container'),
'#default_value' => isset($buckeye_alert_class) ? $buckeye_alert_class : 'l-constrained',
);
$form['buckeye_alert_animate'] = array(
'#type' => 'checkbox',
'#title' => t('Animate alert messages'),
'#default_value' => isset($buckeye_alert_animate) ? $buckeye_alert_animate : TRUE,
);
$form['additional'] = array(
'#type' => 'fieldset',
'#title' => t('Additional messages'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['additional']['intro'] = array(
'#markup' => t('<p>Additional messages to display. To be used ONLY during a weather-related alert.</p>'),
);
$form['additional']['buckeye_alert_addl'] = array(
'#type' => 'textarea',
'#title' => t('Messages'),
'#default_value' => isset($buckeye_alert_addl) ? $buckeye_alert_addl : '',
'#description' => t('Can contain: p a ul ol li'),
'#required' => FALSE,
);
$form['additional']['buckeye_alert_addl_date'] = array(
'#type' => 'datetime',
'#title' => t('Publish on'),
'#date_increment' => 1,
'#default_value' => isset($buckeye_alert_addl_date) ? new DrupalDateTime($buckeye_alert_addl_date) : null, //convert config string to date object
'#required' => FALSE,
);
$form['additional']['buckeye_alert_addl_expire'] = array(
'#type' => 'textfield',
'#attributes' => array(
' type' => 'number',
),
'#title' => t('Expires'),
'#default_value' => isset($buckeye_alert_addl_expire) ? $buckeye_alert_addl_expire : 24,
'#description' => t('Number of hours after the publish time that the message expires.'),
'#required' => FALSE,
);
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('buckeye_alert.buckeye_alert_config');
$config->set('buckeye_alert_url', $form_state->getValue('buckeye_alert_url'))->save();
$config->set('buckeye_alert_test', $form_state->getValue('buckeye_alert_test'))->save();
$config->set('buckeye_alert_class', $form_state->getValue('buckeye_alert_class'))->save();
$config->set('buckeye_alert_animate', $form_state->getValue('buckeye_alert_animate'))->save();
$config->set('buckeye_alert_addl', $form_state->getValue('buckeye_alert_addl'))->save();
//convert date object to string for storage in config
if($form_state->getValue('buckeye_alert_addl_date') !== null){
$config->set('buckeye_alert_addl_date', $form_state->getValue('buckeye_alert_addl_date')->format('Y-m-d H:i:s'))->save();
} else {
$config->set('buckeye_alert_addl_date', $form_state->getValue('buckeye_alert_addl_date'))->save();
}
$config->set('buckeye_alert_addl_expire', $form_state->getValue('buckeye_alert_addl_expire'))->save();
parent::submitForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
//makes sure additional message date is in the future if it is set
//if($form_state->getValue('buckeye_alert_addl_date') !== null && $form_state->getValue('buckeye_alert_addl_date') < date('Y-m-d H:i:s') ){
// $form_state->setErrorByName('buckeye_alert_addl_date', $this->t('Publish date cannot be in the past'));
//}
//makes sure the additional message length is a positive integer
if($form_state->getValue('buckeye_alert_addl_expire') <= 0){
$form_state->setErrorByName('buckeye_alert_addl_expire', $this->t('Invalid expiration time'));
}
//disallow restricted html characters
$allowed = array('p', 'a', 'ul', 'ol', 'li');
$message = Xss::filter($form_state->getValue('buckeye_alert_addl'), $allowed);
//ensure p tag
if(!empty($message) && stripos($message, '<p') === false) {
$message = '<p>' . $message . '</p>';
$form_state->setValue('buckeye_alert_addl', $message);
}
parent::validateForm($form, $form_state);
}
}
......@@ -27,6 +27,7 @@ libraries:
- 'asc_bootstrap/bootstrap-scripts'
- 'asc_bootstrap/osu-webfonts'
- 'asc_bootstrap/font-awesome'
- 'buckeye_alert/buckeye_alert'
ckeditor_stylesheets:
- css/style.css
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment