-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalert_banner.module
87 lines (76 loc) · 2.4 KB
/
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
<?php
/**
* @file
* Alert module.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function alert_banner_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the alert_banner module.
case 'help.page.alert_banner':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides ability to display site-wide alert messages and announcements at the top of all pages.') . '</p>';
return $output;
}
}
/**
* Implements hook_entity_bundle_field_info_alter().
*/
function alert_banner_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
if ($entity_type->id() == 'node' && $bundle == 'alert_banner') {
if (isset($fields['field_alert_message'])) {
$fields['field_alert_message']->addConstraint('HtmlNotEmpty', []);
}
}
}
/**
* Implements hook_page_top().
*/
function alert_banner_page_top(array &$page_top) {
$settings = \Drupal::config('alert_banner.settings');
if (!$settings->get('enable')) {
return;
}
// Do not show alerts on admin pages.
if (\Drupal::service('router.admin_context')->isAdminRoute() &&
!$settings->get('show_on_admin')) {
return;
}
$entity_type_manager = \Drupal::entityTypeManager();
// Retrieve the endpoint of Alert banners Rest.
$endpoint = '/alert-banners?_format=json';
try {
/** @var \Drupal\views\Entity\View $alert_view */
$alert_view = $entity_type_manager->getStorage('view')
->load('alert_banners');
if ($alert_view) {
$display = $alert_view->getDisplay('rest_export_alerts');
if (!empty($display['display_options']['path'])) {
$endpoint = '/' . $display['display_options']['path'] . '?_format=json';
}
}
}
catch (Exception $exception) {
watchdog_exception('alert_banner', $exception);
}
$library = ['alert_banner/alert_banner'];
$css_classes = 'alert-banners';
if ($settings->get('au_dta_design_system')) {
$css_classes .= ' au-grid au-body';
$library[] = 'alert_banner/alert_banner_au_dta';
}
$page_top['alert_banners'] = [
'#markup' => '<div class="' . $css_classes . '" data-alert-endpoint="' . $endpoint . '"></div>',
'#attached' => [
'library' => $library,
],
'#cache' => [
'tags' => ['config:views.view.alert_banners'],
],
];
}