@@ -34,6 +34,148 @@ function va_gov_facilities_form_alter(&$form, FormStateInterface $form_state, $f
34
34
_va_gov_facilities_reduce_service_options($form, $form_state);
35
35
_va_gov_facilities_add_ajax_to_reduce_service_options($form, $form_state);
36
36
}
37
+ // Reduce options on the VAMC facility service form.
38
+ if ($form_id === 'node_health_care_local_health_service_form') {
39
+ _va_gov_facilities_reduce_vamc_facility_service_options($form, $form_state);
40
+ _va_gov_facilities_add_ajax_to_reduce_vamc_facility_service_options($form, $form_state);
41
+
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Adds AJAX to form for VAMC facility service options.
47
+ *
48
+ * @param array $form
49
+ * The form.
50
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
51
+ * The form state.
52
+ */
53
+ function _va_gov_facilities_add_ajax_to_reduce_vamc_facility_service_options(array &$form, FormStateInterface $form_state) {
54
+ // Leave if the form does not have the field_office field.
55
+ if (!isset($form['field_facility_location'])) {
56
+ return;
57
+ }
58
+ // Add AJAX to the field_office dropdown.
59
+ $form['field_facility_location']['widget']['#ajax'] = [
60
+ 'callback' => '_va_gov_facilities_reduce_vamc_facility_service_options',
61
+ 'wrapper' => 'field-service-name-wrapper',
62
+ 'event' => 'change',
63
+ 'progress' => [
64
+ 'type' => 'throbber',
65
+ 'message' => t('Updating VAMC system health service options ...'),
66
+ ],
67
+ ];
68
+ // Wrap the field_service_name_and_descripti field for AJAX replacement.
69
+ $form['field_regional_health_service']['#prefix'] = '<div id="field-service-name-wrapper">';
70
+ $form['field_regional_health_service']['#suffix'] = '</div>';
71
+ }
72
+
73
+ /**
74
+ * AJAX callback to update the VAMC facility service name options.
75
+ *
76
+ * The result is based on selected VAMC facility.
77
+ *
78
+ * @param array $form
79
+ * The form.
80
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
81
+ * The form state.
82
+ *
83
+ * @return array
84
+ * The updated field_service_name_and_descripti field.
85
+ */
86
+ function _va_gov_facilities_reduce_vamc_facility_service_options(array &$form, FormStateInterface $form_state) {
87
+ // The form_state takes precedence over the url to get the office.
88
+ $selected_facility = $form_state->getValue('field_facility_location');
89
+ if (empty($selected_facility)) {
90
+ // But if the form_state doesn't have the office, check the URL.
91
+ $selected_facility_from_url = \Drupal::request()->get('field_facility_location');
92
+ if (empty($selected_facility_from_url)) {
93
+ return $form['field_regional_health_service'];
94
+ }
95
+ else {
96
+ $selected_facility_id = intval($selected_facility_from_url);
97
+ }
98
+ }
99
+ else {
100
+ $selected_facility_id = intval($selected_facility['0']['target_id']);
101
+ }
102
+
103
+ // Leave if the field_service_name_and_descripti field is empty.
104
+ if (empty($form['field_regional_health_service']['widget']['#options'])) {
105
+ return $form['field_regional_health_service'];
106
+ }
107
+
108
+ // Get the original options for the service name field.
109
+ $original_options = $form['field_regional_health_service']['widget']['#options'];
110
+
111
+ // Load the options for the service name field based on the selected office.
112
+ $options = _va_gov_facilities_get_vamc_system_service_names($selected_facility_id, $original_options, $form_state);
113
+ // Update the options of the field_service_name_and_descripti field.
114
+ $form['field_regional_health_service']['widget']['#options'] = $options;
115
+
116
+ // Return the updated field_service_name_and_descripti field.
117
+ return $form['field_regional_health_service'];
118
+ }
119
+
120
+ /**
121
+ * Helper function returns the remaining service names.
122
+ *
123
+ * The result is based on selected VAMC facility.
124
+ *
125
+ * @param int $selected_facility_id
126
+ * The selected VAMC facility.
127
+ * @param array $original_options
128
+ * The original options for the service name field.
129
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
130
+ * The form state.
131
+ *
132
+ * @return array
133
+ * The remaining service names.
134
+ */
135
+ function _va_gov_facilities_get_vamc_system_service_names(int $selected_facility_id, array $original_options, FormStateInterface $form_state) {
136
+ // Initialize the remaining services.
137
+ $remaining_services = $original_options;
138
+ // Query services based on the selected office.
139
+ $database = \Drupal::database();
140
+ $query_existing_services = $database->query(
141
+ "SELECT nfrhs.field_regional_health_service_target_id FROM node__field_facility_location AS nffl
142
+ INNER JOIN node__field_regional_health_service AS nfrhs
143
+ ON nffl.entity_id = nfrhs.entity_id
144
+ WHERE nffl.entity_id IN
145
+ (SELECT nffl.entity_id FROM node__field_facility_location AS nffl
146
+ WHERE nffl.field_facility_location_target_id = :nid
147
+ AND nffl.bundle = 'health_care_local_health_service')",
148
+ [
149
+ ':nid' => $selected_facility_id,
150
+ ]
151
+ );
152
+ // Get the existing services.
153
+ $existing_facility_services = $query_existing_services->fetchAll() ?? [];
154
+ $query_system_services = $database->query(
155
+ "SELECT nfa.entity_id FROM node__field_administration as nfa
156
+ WHERE nfa.bundle = 'regional_health_care_service_des'
157
+ AND nfa.field_administration_target_id =
158
+ (SELECT nfa.field_administration_target_id FROM node__field_administration as nfa
159
+ WHERE nfa.entity_id = :existing_service_id)",
160
+ [
161
+ ':existing_service_id' => $existing_facility_services[0]->field_regional_health_service_target_id,
162
+ ]
163
+ );
164
+ $none_option = [
165
+ '_none' => '- Select a value -',
166
+ ];
167
+ // Flip the existing services for comparison.
168
+ $existing_facility_services = array_flip(array_column($existing_facility_services, 'field_regional_health_service_target_id'));
169
+ $existing_facility_services = array_intersect_key($original_options, $existing_facility_services);
170
+ $system_services = $query_system_services->fetchAll() ?? [];
171
+ $system_services = array_flip(array_column($system_services, 'entity_id'));
172
+ $system_services = array_intersect_key($original_options, $system_services);
173
+ // Remove the existing services from the original options.
174
+ $remaining_services = array_diff_key($system_services, $existing_facility_services);
175
+ // Add the none option to the remaining services.
176
+ $remaining_services = $none_option + $remaining_services;
177
+
178
+ return $remaining_services;
37
179
}
38
180
39
181
/**
0 commit comments