-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoda.module
273 lines (250 loc) · 8.78 KB
/
oda.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
<?php
/**
* @file
* Module containing custom oda code.
*/
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
use Drupal\views\ViewExecutable;
use Drupal\Core\Link;
/**
* Implements hook_page_attachments() to add library oda.node_page to
* data_report node type..
*/
function oda_page_attachments(array &$attachments) {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
if ($node->getType() == 'data_report') {
$attachments['#attached']['library'][] = 'oda/node_page';
}
}
}
/**
* Implements hook_page_attachments_alter().
*/
function oda_page_attachments_alter(array &$attachments) {
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == 'oda') {
$google_site_verify = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'google-site-verification',
'content' => 'sdDO7pnhCwXnZiU7VRq4lFwoo0TsvpyUUbcYq6s-9A4',
],
];
$attachments['#attached']['html_head'][] = [$google_site_verify, 'google-site-verification'];
}
}
/**
* Implements hook_views_pre_render().
*/
function oda_views_pre_view(ViewExecutable $view) {
if ($view->id() == 'oda_data') {
// Get current users roles.
$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();
// Get all taxonomy terms from 'oda_access_control' vocabulary.
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('oda_access_control');
// Get the permissions from taxonomy access control terms.
$oda_access_terms = [];
foreach ($terms as $term) {
$tid = $term->tid;
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
$key = $term->get("field_oda_role_machine_name")->getValue()[0]['value'];
$oda_access_terms[$key] = $tid;
}
$view_filters = $view->display_handler->getOption('filters');
$view_filters['field_oda_report_access']['value'] = [];
foreach ($oda_access_terms as $term_role=>$term_tid) {
if (in_array('administrator', $roles) || in_array('pseudo_admin', $roles) || in_array('oda_super_editors', $roles)) {
$view_filters['field_oda_report_access']['value'][$term_tid] = $term_tid;
}
elseif (in_array($term_role, $roles)) {
$view_filters['field_oda_report_access']['value'][$term_tid] = $term_tid;
}
}
$view->display_handler->setOption('filters', $view_filters);
}
}
/**
* Implements hook_views_pre_render().
*/
function oda_views_pre_render(ViewExecutable $view) {
if ($view->id() == 'oda_data') {
$view->element['#attached']['library'][] = 'oda/data_report_layout';
}
}
/**
* Implements hook_form_alter().
*/
function oda_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == 'oda') {
if (isset($form['field_domain_access'])) {
$form['field_domain_access']['widget']['#default_value'][] = 'oda_colorado_edu';
}
if (isset($form['field_domain_source'])) {
$form['field_domain_source']['widget']['#default_value'][] = 'oda_colorado_edu';
}
}
if ($form_id == "views_exposed_form" && $form['#id'] == "views-exposed-form-media-media-page-list") {
if ($domain == 'oda') {
$options = [];
foreach ($form['type']['#options'] as $key => $value) {
if ($key == 'oda_files' || $key == 'oda_images') {
$options[$key] = $value;
}
}
$form['type']['#options'] = $options;
}
}
// Add custom library to admin content page and set domain.
if ($form_id == "views_form_content_page_1") {
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == 'oda') {
$form['#attached']['library'][] = 'oda/admin_content';
// Get query string.
$query = \Drupal::request()->query->all();
$domain_set = FALSE;
foreach ($query as $key => $value) {
if ($key == 'field_domain_access_target_id') {
$domain_set = TRUE;
}
}
if (!$domain_set) {
// Redirect to query string "field_domain_access_target_id=oit_colorado_edu".
$query['field_domain_access_target_id'] = 'oda_colorado_edu';
$query = http_build_query($query);
$response = new RedirectResponse('/admin/content?' . $query);
$response->send();
}
}
}
if ($form_id == "search_block_form") {
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == 'oda') {
$form['keys']['#attributes']['placeholder'] = t('Search Site');
}
}
if ($form_id == "node_webform_form" || $form_id == "node_webform_edit_form") {
$domain = \Drupal::service('oit.domain')->getDomain();
if ($domain == 'oda') {
$form['field_oit_category']['#access'] = FALSE;
}
}
// Set defaults on new data report nodes.
if ($form_id == "node_data_report_form") {
$form['body']['widget'][0]['#default_value'] = '<p>ODA Data Report Info</p><p><strong>Tags:</strong> tag, tag, tag</p>';
}
// Set defaults on data report nodes for ODA type.
if ($form_id == "node_data_report_form" || $form_id == "node_data_report_edit_form") {
$form['#attached']['library'][] = 'webform/webform.element.select2';
$form['#attached']['library'][] = 'oda/oit_data_report_page_form';
// Group for access report TAC.
$form['oda_access_group'] = [
'#title' => t('Data Report Access Group'),
'#type' => 'details',
'#group' => 'advanced',
'#open' => 1,
'#weight' => 99,
];
$form['field_oda_report_access']['#group'] = 'oda_access_group';
// Group for page sub-type extra fields.
$form['oda_terms'] = [
'#title' => t('Terms'),
'#type' => 'details',
'#group' => 'advanced',
'#open' => 1,
'#weight' => 100,
];
$form['field_oda_population']['#group'] = 'oda_terms';
$form['field_oda_level_of_detail']['#group'] = 'oda_terms';
$form['field_oda_cu_stats']['#group'] = 'oda_terms';
$form['field_oda_course_data']['#group'] = 'oda_terms';
$form['field_oda_term']['#group'] = 'oda_terms';
$form['field_oda_type']['#group'] = 'oda_terms';
$form['field_oda_academic_terms_covered']['#group'] = 'oda_terms';
$form['field_oda_demographics']['#group'] = 'oda_terms';
$form['field_oda_peers_ext_org']['#group'] = 'oda_terms';
$form['field_oda_financial']['#group'] = 'oda_terms';
$form['field_oda_surveys']['#group'] = 'oda_terms';
// if current user is 'oda_super_editors' or 'administrator'.
$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();
if (in_array('oda_super_editors', $roles) || in_array('administrator', $roles)) {
// Add 'Add/Delete Terms' button to taxonomy fields.
$term_list = [
'oda_population',
'oda_level_of_detail',
'oda_cu_stats',
'oda_course_data',
'oda_term',
'oda_type',
'oda_academic_terms_covered',
'oda_demographics',
'oda_peers_ext_org',
];
foreach ($term_list as $term) {
$link = oda_term_edit($term);
$form_term = 'field_' . $term;
$form[$form_term]['widget']['#suffix'] = $link;
}
}
}
// Set defaults on data page nodes for ODA type.
if ($form_id == "node_oda_page_form" || $form_id == "node_oda_page_edit_form") {
$advanced = [
'#type' => 'inline_template',
'#template' => '<details>
<summary>{{ details_title }}</summary>
<ul>
<li>
<a href="{{ svg_link }}" class="edit-button use-ajax" data-dialog-type="dialog" data-dialog-renderer="off_canvas" data-dialog-options="{"width":400}">
{{ svg_title }}
</a>
</li>
</ul>
</details>',
'#context' => [
'details_title' => t('advanced html'),
'svg_link' => '/admin/config/content/shortcode_svg/svg_list',
'svg_title' => t('Icons shortcode panel'),
],
'#weight' => 13,
'#allowed_tags' => [
'button',
'details',
'summary',
'ul',
'li',
'a',
'p',
'h3',
],
];
$form['oit_advanced'] = $advanced;
}
}
/**
* Create term edit links.
*/
function oda_term_edit($term_id, $edit_label = 'Add/Delete Terms') {
$url = Url::fromUri("internal:/admin/structure/taxonomy/manage/$term_id/overview");
$link = Link::fromTextAndUrl(t($edit_label), $url);
return $link->toString()->getGeneratedLink();
}
/**
* Implements hook_shortcut_default_set().
*/
function oda_shortcut_default_set() {
$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();
// if roles array contains 'oda_editors' or 'oda_super_editors'.
if (in_array('oda_editors', $roles)) {
return 'oda';
}
if (in_array('oda_super_editors', $roles)) {
return 'oda-super-editor';
}
}