-
Notifications
You must be signed in to change notification settings - Fork 59
/
va_gov_consumers.module
353 lines (316 loc) · 13 KB
/
va_gov_consumers.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* @file
* Contains va_gov_consumers.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Implements hook_module_implements_alter().
*/
function va_gov_consumers_module_implements_alter(&$implementations, $hook) {
switch ($hook) {
// Move our hook_form_alter() implementation to the end of the list.
// This was added to compensate for a similar call ignoring weights.
// See: docroot/modules/contrib/field_group/field_group.module.
case 'form_alter':
$va_gov_consumers = $implementations['va_gov_consumers'];
unset($implementations['va_gov_consumers']);
$implementations['va_gov_consumers'] = $va_gov_consumers;
break;
}
}
/**
* Implements hook_form_alter().
*/
function va_gov_consumers_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_va_gov_consumers_modify_va_form_fields($form, $form_id, $form_state);
_va_gov_consumers_modify_vamc_system_related_fields($form, $form_id, $form_state);
_va_gov_consumers_modify_facility_fields($form, $form_id, $form_state);
_va_gov_consumers_disable_vha_api_form_fields($form, $form_id);
_va_gov_consumers_disable_non_vha_facilities_api_form_fields($form, $form_id);
}
/**
* Disable api sourced fields on vha local facility to prevent human editing.
*
* @param array $form
* A drupal form array by reference.
* @param string $form_id
* The machine name for the form.
*/
function _va_gov_consumers_disable_vha_api_form_fields(array &$form, $form_id) {
if ($form_id === 'node_health_care_local_facility_edit_form' || $form_id === 'node_health_care_local_facility_form') {
if (empty($form['title']['widget'][0]['value']['#default_value'])) {
$form['title']['widget'][0]['value']['#default_value'] = t('API Data');
}
// Disable the facility locator API field if it is populated because it
// should not be altered, or it will deviate from the API.
if (!empty($form['field_facility_locator_api_id']['widget'][0]['value']['#default_value'])) {
$form['field_facility_locator_api_id']['widget'][0]['value']['#attributes']['disabled'] = TRUE;
}
}
}
/**
* Returns the names of the add and edit Vet center forms.
*
* @return array
* The Vet center form names.
*/
function _va_gov_consumers_return_vet_center_form_names() {
return [
'node_vet_center_edit_form',
'node_vet_center_form',
'node_vet_center_outstation_edit_form',
'node_vet_center_outstation_form',
];
}
/**
* Disable api sourced fields on non vha facilities to prevent human editing.
*
* @param array $form
* A drupal form array by reference.
* @param string $form_id
* The machine name for the form.
*/
function _va_gov_consumers_disable_non_vha_facilities_api_form_fields(array &$form, $form_id) {
$target_forms = [
'node_nca_facility_edit_form',
'node_nca_facility_form',
'node_vba_facility_edit_form',
'node_vba_facility_form',
'node_vet_center_edit_form',
'node_vet_center_form',
'node_vet_center_mobile_vet_center_form',
'node_vet_center_mobile_vet_center_edit_form',
'node_vet_center_outstation_form',
'node_vet_center_outstation_edit_form',
];
if (in_array($form_id, $target_forms)) {
if (empty($form['title']['widget'][0]['value']['#default_value'])) {
// Prepoulate the title to warn it will be overwritten by api data.
$form['title']['widget'][0]['value']['#default_value'] = t('May be overwritten by Facility API Data');
}
// Only allow admins to alter vc titles.
elseif (Drupal::service('va_gov_user.user_perms')->hasAdminRole() && in_array($form_id, _va_gov_consumers_return_vet_center_form_names())) {
$form['title']['widget'][0]['value']['#attributes']['disabled'] = FALSE;
}
// Lockdown titles for all roles for all other types.
else {
$form['title']['widget'][0]['value']['#attributes']['disabled'] = TRUE;
}
// Disable the facility locator API field if it is populated because it
// should not be altered, or it will deviate from the API.
if (!empty($form['field_facility_locator_api_id']['widget'][0]['value']['#default_value'])) {
$form['field_facility_locator_api_id']['widget'][0]['value']['#attributes']['disabled'] = TRUE;
}
}
}
/**
* Modify the display of the fields on va_form.
*
* @param array $form
* A drupal form array by reference.
* @param string $form_id
* The machine name for the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state by reference.
*/
function _va_gov_consumers_modify_va_form_fields(array &$form, $form_id, FormStateInterface &$form_state) {
if ($form_id === 'node_va_form_edit_form' || $form_id === 'node_va_form_form') {
$node = $form_state->getformObject()->getEntity();
$title = "<h1>{$node->getTitle()}</h1>";
$form_name = $form['field_va_form_name'];
// We want the form name in a different location, so remove it from here.
unset($form['field_va_form_name']);
$form['header'] = [
'#type' => 'fieldset',
'#weight' => 0,
'#collapsible' => FALSE,
'title' => [
'#markup' => $title . '<div class="description">This non-editable page title is automatically set by the VA forms database.</div>',
],
'field_va_form_name' => $form_name,
];
_va_gov_consumers_disable_external_content_editing($form, $form_state, 'field_va_form_number');
// Show benefit categories only if benefit is selected.
$form['field_benefit_categories']['#states'] = [
'visible' => [
':input[id="edit-field-va-form-type"]' => ['value' => 'benefit'],
],
];
$form['field_va_form_tool_intro']['#states'] = [
'visible' => [
':input[id="edit-field-va-form-tool-url-0-uri"]' => ['filled' => TRUE],
],
];
}
}
/**
* Modify the display of the fields on vet center form.
*
* @param array $form
* A drupal form array by reference.
* @param string $form_id
* The machine name for the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state by reference.
*/
function _va_gov_consumers_modify_facility_fields(array &$form, $form_id, FormStateInterface &$form_state) {
// We want to modify display of fields for specific form id's.
$external_content_form_ids = [
'node_nca_facility_edit_form',
'node_nca_facility_form',
'node_health_care_local_facility_form',
'node_health_care_local_facility_edit_form',
'node_vba_facility_edit_form',
'node_vba_facility_form',
'node_vet_center_form',
'node_vet_center_edit_form',
'node_vet_center_mobile_vet_center_form',
'node_vet_center_mobile_vet_center_edit_form',
'node_vet_center_outstation_form',
'node_vet_center_outstation_edit_form',
];
// We want to confirm we are on a specific form.
if (in_array($form_id, $external_content_form_ids)) {
$node = $form_state->getformObject()->getEntity();
$title = "<h4>{$node->getTitle()}</h4>";
$form['top_of_page_information'] = [
'#weight' => 1,
'title' => [
'#markup' => $title,
],
];
// We want the title to appear within the page_title fieldgroup.
$content_types_hiding_title = [
'health_care_local_facility',
'nca_facility',
'vet_center_outstation',
];
if (!in_array($node->getType(), $content_types_hiding_title)) {
$form['group_page_title_tooltip']['top_of_page_information'] = $form['top_of_page_information'];
}
// We want the page title in a different location, so remove it from here.
unset($form['top_of_page_information']);
_va_gov_consumers_disable_external_content_editing($form, $form_state, 'field_facility_locator_api_id');
}
if (in_array($form_id, _va_gov_consumers_return_vet_center_form_names())) {
$form['#attached']['library'][] = 'va_gov_consumers/text_field_tooltips';
$form['field_official_name']['#disabled'] = TRUE;
// If common name and title are the same, don't show both unless admin.
if (!\Drupal::service('va_gov_user.user_perms')->hasAdminRole() && strcmp($form['field_official_name']['widget'][0]['value']['#default_value'], $form['title']['widget'][0]['value']['#default_value']) === 0) {
$form['title']['#attributes']['class'][] = 'hidden css-tooltip-toggle';
}
}
}
/**
* Modify the display of the fields on vamc system form.
*
* @param array $form
* A drupal form array by reference.
* @param string $form_id
* The machine name for the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state by reference.
*/
function _va_gov_consumers_modify_vamc_system_related_fields(array &$form, $form_id, FormStateInterface &$form_state) {
// We want to modify display of fields for specific form id's.
$external_content_form_ids = [
'node_health_care_region_page_form',
'node_health_care_region_page_edit_form',
'node_vamc_system_va_police_form',
'node_vamc_system_va_police_edit_form',
];
// We want to confirm we are on a specific form.
if (in_array($form_id, $external_content_form_ids)) {
_va_gov_consumers_disable_external_content_editing($form, $form_state, TRUE);
}
}
/**
* Displays selected content as text, not form fields.
*
* Adds node rendered as 'external_content' to form and removes the first
* fieldgroup with id of 'external-content'.
*
* @param array $form
* The form content.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
* @param string|bool $display_if_empty_field
* The field to check for empty. If empty, external content fields appear.
* TRUE indicates absence of field and external content should appear.
*/
function _va_gov_consumers_disable_external_content_editing(array &$form, FormStateInterface $form_state, $display_if_empty_field) {
$node = $form_state->getformObject()->getEntity();
$is_admin = \Drupal::service('va_gov_user.user_perms')->hasAdminRole(TRUE);
// TRUE means to skip field value check.
if ($display_if_empty_field === TRUE) {
// TRUE indicates we need to use external content.
$has_field_value = TRUE;
}
else {
$has_field_value = !empty($node->get($display_if_empty_field)->value);
}
// If the user is not admin or and there is migrated data, hide the fields.
// We want admins to have access to fields in case they have to temporarily
// fix bad data from the data source. It will get wiped out the next time
// the source row changes.
if ((!$is_admin) && ($has_field_value) && (!empty($form) && !empty($form['#fieldgroups']))) {
// Hide the actual fields.
foreach ($form['#fieldgroups'] as $fieldgroup) {
if (!empty($fieldgroup->format_settings['id']) && $fieldgroup->format_settings['id'] === 'external-content') {
foreach ($fieldgroup->children as $field) {
unset($form[$field]);
}
break;
}
}
// Display the readonly version of the fields.
$viewBuilder = \Drupal::entityTypeManager()->getViewBuilder('node');
$readonly_content = $viewBuilder->view($node, 'external_content');
$form['external_content'] = [
'#markup' => \Drupal::service('renderer')->render($readonly_content),
];
// We want the external content to appear in the fieldgroup
// group_ehr_info on the bottom of system forms.
if ($node->getType() === 'health_care_region_page') {
$form['group_ehr_info']['#weight'] = 10;
$form['group_ehr_info']['external_content'] = $form['external_content'];
unset($form['external_content']);
}
elseif (isset($form['external_content'])) {
// We want the external content to appear in the fieldgroup
// group_locations_and_contact_info on other node types
// and at the bottom when other elements are present.
$form['group_locations_and_contact_info']['external_content'] = $form['external_content'];
$form['group_locations_and_contact_info']['external_content']['#weight'] = 10;
unset($form['external_content']);
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function va_gov_consumers_preprocess_node(&$variables) {
/** @var \Drupal\node\Entity\Node $node */
$node = $variables['node'];
if ($variables['view_mode'] === 'external_content') {
$label = 'Non-editable content (managed outside the CMS)';
$message = '';
if ($node->getType() === 'va_form') {
$label = 'Forms DB Data';
$message = 'The following information about this VA form comes from the VA forms database, and any changes are updated here nightly. Contact the form administration for further information.';
}
// phpcs:disable
// Disabling phpcs here due to the warning
// "Only string literals should be passed to t() where possible".
$variables['external_content_label'] = new TranslatableMarkup($label);
$variables['external_content_message'] = new TranslatableMarkup($message);
// phpcs:enable
}
elseif ($variables['view_mode'] === 'full' && $node->getType() === "va_form") {
$viewBuilder = \Drupal::entityTypeManager()->getViewBuilder('node');
$readonly_content = $viewBuilder->view($variables['node'], 'external_content');
$variables['external_content'] = \Drupal::service('renderer')->render($readonly_content);
}
}