-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcweb.theme
executable file
·194 lines (166 loc) · 6.16 KB
/
gcweb.theme
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
<?php
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\views\Views;
/**
* @file
* Functions to support theming in the GCWEB theme
*/
/**
* Implements hook_preprocess_HOOK() for page.html.twig
* redirect non-admin users of external pages to the external page
* setup variables: date modified, canada.ca search form link and mega menu link for twig template
*/
function gcweb_preprocess_page(&$variables) {
// find language
$variables['langcode'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
if (is_object($node = \Drupal::request()->get('node'))) {
// redirect to dataset page
if ($node->getType() == 'external') {
$translated_node = $node->getTranslation($variables['langcode']);
$path = $translated_node->get('field_url')->getValue();
$response = new TrustedRedirectResponse($path[0]['uri']);
$response->send();
}
// date modified
$time = $node->getChangedTime();
$variables['date_modified'] = \Drupal::service('date.formatter')->format($time, 'custom', 'Y-m-d');
}
else {
$variables['date_modified'] = \Drupal::service('date.formatter')->format(time(), 'custom', 'Y-m-d');
}
// feedback link
$variables['feedback_link'] = $variables['langcode'] == 'fr'
? '/fr/formulaire/faites-nous-part-de-vos-commentaires'
: '/forms/contact-us';
// footer navigation
$variables['custom_footer'] = theme_get_setting('gcweb_footer', 'gcweb');
}
/**
* Implements hook_page_attachments_alter()
* attach libraries for WET-BOEW
* attach highlight library
* attach event tracking library to user pages
*/
function gcweb_page_attachments_alter(&$page) {
$langCode = \Drupal::languageManager()->getCurrentLanguage()->getId();
// specify the version for WET-BOEW CDTS
$gcweb_version = 'v5_0_1';
$gcweb_url = 'https://www.canada.ca/etc/designs/canada/cdts/gcweb/' . $gcweb_version;
// CDTS - Load template scripts
$page['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => [ 'src' => $gcweb_url . '/cdts/compiled/soyutils.js'],
],
'gcweb_utils',
];
$page['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => [ 'src' => $gcweb_url . '/cdts/compiled/wet-' . $langCode . '.js'],
],
'gcweb_wet',
];
// date modified
$time = is_object($node = \Drupal::request()->get('node')) ? $node->getChangedTime() : time();
$page['#attached']['drupalSettings']['date_modified'] = \Drupal::service('date.formatter')->format($time, 'custom', 'Y-m-d');;
// feedback link
$page['#attached']['drupalSettings']['feedback_link'] = $langCode == 'fr'
? '/fr/formulaire/faites-nous-part-de-vos-commentaires'
: '/forms/contact-us';
// Load WET-BOEW libraries
$page['#attached']['library'][] = 'gcweb/wet-boew';
$page['#attached']['library'][] = 'gcweb/wet-load';
// Load highlight library for Landing pages
if (is_object($node = \Drupal::request()->get('node'))) {
if ($node->getType() === 'landing_page') {
$page['#attached']['library'][] = 'gcweb/highlight';
}
}
// Load Event Tracking library for user pages
if (strpos(\Drupal::request()->getRequestUri(), '/user/') !== false) {
$page['#attached']['library'][] = 'gcweb/event-tracking';
}
// Load front library for homepage
if (is_object($node = \Drupal::request()->get('node'))) {
$is_front = \Drupal::service('path.matcher')->isFrontPage();
if ($is_front) {
$page['#attached']['library'][] = 'gcweb/front';
}
}
// Load Google Analytics library
$ga_key = \Drupal\Core\Site\Settings::get('GA4_KEY');
if ($ga_key and \Drupal::currentUser()->isAnonymous()) {
$page['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => [ 'src' => 'https://www.googletagmanager.com/gtag/js?id=' . $ga_key, 'async' => true ],
],
'ga4',
];
$ga_script = 'window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag("js", new Date());gtag("config", "' . $ga_key . '");';
$page['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => Drupal\Core\Render\Markup::create($ga_script),
],
'ga4_script',
];
}
}
/**
* Implements hook_preprocess_HOOK() for links--language-block.html.twig
* provide language variable to the twig template
*/
function gcweb_preprocess_links__language_block(&$variables) {
$variables['language'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}
/**
* Implements template_preprocess_user()
* add recent contents view to the login page
* add recent comments view to the login page
*/
function gcweb_preprocess_user(&$variables) {
// recent contents
if (\Drupal::currentUser()->hasPermission('view any unpublished content')) {
$content_recent = Views::getView('content_recent');
if (is_object($content_recent)) {
$content_recent->setDisplay('block_1');
$content_recent->execute();
$variables['content_recent'] = $content_recent->render();
}
}
}
/**
* Implements template_preprocess_pager
* SiteImprove enhancement
*/
function gcweb_preprocess_pager(&$variables){
$variables['heading_id'] = \Drupal\Component\Utility\Html::getUniqueId('pagination-heading');
}
/**
* @method void gcweb_form_alter()
* @param array $_form
* @param \Drupal\Core\Form\FormStateInterface $_formState
* @param string $_formID
* @return void
*
* @see https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!form.api.php/function/hook_form_alter/8.0.x
*/
function gcweb_form_alter( &$_form, $_formState, $_formID )
{
if(
$_formID === 'user_login_form' ||
$_formID === 'user_pass'
){
$_form['registryMessage'] = [
'#type' => 'markup',
'#markup' => "<br/><div class='row alert fade in alert-danger'><p><strong>" . t("Government of Canada users should use the <a href='https://registry.open.canada.ca/en/user/login' target='_blank'>Registry</a> to publish government resources on the Open Government Portal. (accessible only on the Government of Canada network)") . "</strong></p></div>",
'#weight' => -66
];
}
}