Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Provides the Views' administrative interface.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\Component\Utility\NestedArray;
|
Chris@0
|
9 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
10 use Drupal\Core\Url;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Converts a form element in the add view wizard to be AJAX-enabled.
|
Chris@0
|
14 *
|
Chris@0
|
15 * This function takes a form element and adds AJAX behaviors to it such that
|
Chris@0
|
16 * changing it triggers another part of the form to update automatically. It
|
Chris@0
|
17 * also adds a submit button to the form that appears next to the triggering
|
Chris@0
|
18 * element and that duplicates its functionality for users who do not have
|
Chris@0
|
19 * JavaScript enabled (the button is automatically hidden for users who do have
|
Chris@0
|
20 * JavaScript).
|
Chris@0
|
21 *
|
Chris@0
|
22 * To use this function, call it directly from your form builder function
|
Chris@0
|
23 * immediately after you have defined the form element that will serve as the
|
Chris@0
|
24 * JavaScript trigger. Calling it elsewhere (such as in hook_form_alter()) may
|
Chris@0
|
25 * mean that the non-JavaScript fallback button does not appear in the correct
|
Chris@0
|
26 * place in the form.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @param $wrapping_element
|
Chris@0
|
29 * The element whose child will server as the AJAX trigger. For example, if
|
Chris@0
|
30 * $form['some_wrapper']['triggering_element'] represents the element which
|
Chris@0
|
31 * will trigger the AJAX behavior, you would pass $form['some_wrapper'] for
|
Chris@0
|
32 * this parameter.
|
Chris@0
|
33 * @param $trigger_key
|
Chris@0
|
34 * The key within the wrapping element that identifies which of its children
|
Chris@0
|
35 * serves as the AJAX trigger. In the above example, you would pass
|
Chris@0
|
36 * 'triggering_element' for this parameter.
|
Chris@0
|
37 * @param $refresh_parents
|
Chris@0
|
38 * An array of parent keys that point to the part of the form that will be
|
Chris@0
|
39 * refreshed by AJAX. For example, if triggering the AJAX behavior should
|
Chris@0
|
40 * cause $form['dynamic_content']['section'] to be refreshed, you would pass
|
Chris@0
|
41 * array('dynamic_content', 'section') for this parameter.
|
Chris@0
|
42 */
|
Chris@0
|
43 function views_ui_add_ajax_trigger(&$wrapping_element, $trigger_key, $refresh_parents) {
|
Chris@0
|
44 $seen_ids = &drupal_static(__FUNCTION__ . ':seen_ids', []);
|
Chris@0
|
45 $seen_buttons = &drupal_static(__FUNCTION__ . ':seen_buttons', []);
|
Chris@0
|
46
|
Chris@0
|
47 // Add the AJAX behavior to the triggering element.
|
Chris@0
|
48 $triggering_element = &$wrapping_element[$trigger_key];
|
Chris@0
|
49 $triggering_element['#ajax']['callback'] = 'views_ui_ajax_update_form';
|
Chris@0
|
50
|
Chris@0
|
51 // We do not use \Drupal\Component\Utility\Html::getUniqueId() to get an ID
|
Chris@0
|
52 // for the AJAX wrapper, because it remembers IDs across AJAX requests (and
|
Chris@0
|
53 // won't reuse them), but in our case we need to use the same ID from request
|
Chris@0
|
54 // to request so that the wrapper can be recognized by the AJAX system and
|
Chris@0
|
55 // its content can be dynamically updated. So instead, we will keep track of
|
Chris@0
|
56 // duplicate IDs (within a single request) on our own, later in this function.
|
Chris@0
|
57 $triggering_element['#ajax']['wrapper'] = 'edit-view-' . implode('-', $refresh_parents) . '-wrapper';
|
Chris@0
|
58
|
Chris@0
|
59 // Add a submit button for users who do not have JavaScript enabled. It
|
Chris@0
|
60 // should be displayed next to the triggering element on the form.
|
Chris@0
|
61 $button_key = $trigger_key . '_trigger_update';
|
Chris@0
|
62 $element_info = \Drupal::service('element_info');
|
Chris@0
|
63 $wrapping_element[$button_key] = [
|
Chris@0
|
64 '#type' => 'submit',
|
Chris@0
|
65 // Hide this button when JavaScript is enabled.
|
Chris@0
|
66 '#attributes' => ['class' => ['js-hide']],
|
Chris@0
|
67 '#submit' => ['views_ui_nojs_submit'],
|
Chris@0
|
68 // Add a process function to limit this button's validation errors to the
|
Chris@0
|
69 // triggering element only. We have to do this in #process since until the
|
Chris@0
|
70 // form API has added the #parents property to the triggering element for
|
Chris@0
|
71 // us, we don't have any (easy) way to find out where its submitted values
|
Chris@0
|
72 // will eventually appear in $form_state->getValues().
|
Chris@0
|
73 '#process' => array_merge(['views_ui_add_limited_validation'], $element_info->getInfoProperty('submit', '#process', [])),
|
Chris@0
|
74 // Add an after-build function that inserts a wrapper around the region of
|
Chris@0
|
75 // the form that needs to be refreshed by AJAX (so that the AJAX system can
|
Chris@0
|
76 // detect and dynamically update it). This is done in #after_build because
|
Chris@0
|
77 // it's a convenient place where we have automatic access to the complete
|
Chris@0
|
78 // form array, but also to minimize the chance that the HTML we add will
|
Chris@0
|
79 // get clobbered by code that runs after we have added it.
|
Chris@0
|
80 '#after_build' => array_merge($element_info->getInfoProperty('submit', '#after_build', []), ['views_ui_add_ajax_wrapper']),
|
Chris@0
|
81 ];
|
Chris@0
|
82 // Copy #weight and #access from the triggering element to the button, so
|
Chris@0
|
83 // that the two elements will be displayed together.
|
Chris@0
|
84 foreach (['#weight', '#access'] as $property) {
|
Chris@0
|
85 if (isset($triggering_element[$property])) {
|
Chris@0
|
86 $wrapping_element[$button_key][$property] = $triggering_element[$property];
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89 // For easiest integration with the form API and the testing framework, we
|
Chris@0
|
90 // always give the button a unique #value, rather than playing around with
|
Chris@0
|
91 // #name. We also cast the #title to string as we will use it as an array
|
Chris@0
|
92 // key and it may be a TranslatableMarkup.
|
Chris@0
|
93 $button_title = !empty($triggering_element['#title']) ? (string) $triggering_element['#title'] : $trigger_key;
|
Chris@0
|
94 if (empty($seen_buttons[$button_title])) {
|
Chris@0
|
95 $wrapping_element[$button_key]['#value'] = t('Update "@title" choice', [
|
Chris@0
|
96 '@title' => $button_title,
|
Chris@0
|
97 ]);
|
Chris@0
|
98 $seen_buttons[$button_title] = 1;
|
Chris@0
|
99 }
|
Chris@0
|
100 else {
|
Chris@0
|
101 $wrapping_element[$button_key]['#value'] = t('Update "@title" choice (@number)', [
|
Chris@0
|
102 '@title' => $button_title,
|
Chris@0
|
103 '@number' => ++$seen_buttons[$button_title],
|
Chris@0
|
104 ]);
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 // Attach custom data to the triggering element and submit button, so we can
|
Chris@0
|
108 // use it in both the process function and AJAX callback.
|
Chris@0
|
109 $ajax_data = [
|
Chris@0
|
110 'wrapper' => $triggering_element['#ajax']['wrapper'],
|
Chris@0
|
111 'trigger_key' => $trigger_key,
|
Chris@0
|
112 'refresh_parents' => $refresh_parents,
|
Chris@0
|
113 ];
|
Chris@0
|
114 $seen_ids[$triggering_element['#ajax']['wrapper']] = TRUE;
|
Chris@0
|
115 $triggering_element['#views_ui_ajax_data'] = $ajax_data;
|
Chris@0
|
116 $wrapping_element[$button_key]['#views_ui_ajax_data'] = $ajax_data;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * Processes a non-JavaScript fallback submit button to limit its validation errors.
|
Chris@0
|
121 */
|
Chris@0
|
122 function views_ui_add_limited_validation($element, FormStateInterface $form_state) {
|
Chris@0
|
123 // Retrieve the AJAX triggering element so we can determine its parents. (We
|
Chris@0
|
124 // know it's at the same level of the complete form array as the submit
|
Chris@0
|
125 // button, so all we have to do to find it is swap out the submit button's
|
Chris@0
|
126 // last array parent.)
|
Chris@0
|
127 $array_parents = $element['#array_parents'];
|
Chris@0
|
128 array_pop($array_parents);
|
Chris@0
|
129 $array_parents[] = $element['#views_ui_ajax_data']['trigger_key'];
|
Chris@0
|
130 $ajax_triggering_element = NestedArray::getValue($form_state->getCompleteForm(), $array_parents);
|
Chris@0
|
131
|
Chris@0
|
132 // Limit this button's validation to the AJAX triggering element, so it can
|
Chris@0
|
133 // update the form for that change without requiring that the rest of the
|
Chris@0
|
134 // form be filled out properly yet.
|
Chris@0
|
135 $element['#limit_validation_errors'] = [$ajax_triggering_element['#parents']];
|
Chris@0
|
136
|
Chris@0
|
137 // If we are in the process of a form submission and this is the button that
|
Chris@0
|
138 // was clicked, the form API workflow in \Drupal::formBuilder()->doBuildForm()
|
Chris@0
|
139 // will have already copied it to $form_state->getTriggeringElement() before
|
Chris@0
|
140 // our #process function is run. So we need to make the same modifications in
|
Chris@0
|
141 // $form_state as we did to the element itself, to ensure that
|
Chris@0
|
142 // #limit_validation_errors will actually be set in the correct place.
|
Chris@0
|
143 $clicked_button = &$form_state->getTriggeringElement();
|
Chris@0
|
144 if ($clicked_button && $clicked_button['#name'] == $element['#name'] && $clicked_button['#value'] == $element['#value']) {
|
Chris@0
|
145 $clicked_button['#limit_validation_errors'] = $element['#limit_validation_errors'];
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 return $element;
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * After-build function that adds a wrapper to a form region (for AJAX refreshes).
|
Chris@0
|
153 *
|
Chris@0
|
154 * This function inserts a wrapper around the region of the form that needs to
|
Chris@0
|
155 * be refreshed by AJAX, based on information stored in the corresponding
|
Chris@0
|
156 * submit button form element.
|
Chris@0
|
157 */
|
Chris@0
|
158 function views_ui_add_ajax_wrapper($element, FormStateInterface $form_state) {
|
Chris@0
|
159 // Find the region of the complete form that needs to be refreshed by AJAX.
|
Chris@0
|
160 // This was earlier stored in a property on the element.
|
Chris@0
|
161 $complete_form = &$form_state->getCompleteForm();
|
Chris@0
|
162 $refresh_parents = $element['#views_ui_ajax_data']['refresh_parents'];
|
Chris@0
|
163 $refresh_element = NestedArray::getValue($complete_form, $refresh_parents);
|
Chris@0
|
164
|
Chris@0
|
165 // The HTML ID that AJAX expects was also stored in a property on the
|
Chris@0
|
166 // element, so use that information to insert the wrapper <div> here.
|
Chris@0
|
167 $id = $element['#views_ui_ajax_data']['wrapper'];
|
Chris@0
|
168 $refresh_element += [
|
Chris@0
|
169 '#prefix' => '',
|
Chris@0
|
170 '#suffix' => '',
|
Chris@0
|
171 ];
|
Chris@0
|
172 $refresh_element['#prefix'] = '<div id="' . $id . '" class="views-ui-ajax-wrapper">' . $refresh_element['#prefix'];
|
Chris@0
|
173 $refresh_element['#suffix'] .= '</div>';
|
Chris@0
|
174
|
Chris@0
|
175 // Copy the element that needs to be refreshed back into the form, with our
|
Chris@0
|
176 // modifications to it.
|
Chris@0
|
177 NestedArray::setValue($complete_form, $refresh_parents, $refresh_element);
|
Chris@0
|
178
|
Chris@0
|
179 return $element;
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 /**
|
Chris@0
|
183 * Updates a part of the add view form via AJAX.
|
Chris@0
|
184 *
|
Chris@0
|
185 * @return
|
Chris@0
|
186 * The part of the form that has changed.
|
Chris@0
|
187 */
|
Chris@0
|
188 function views_ui_ajax_update_form($form, FormStateInterface $form_state) {
|
Chris@0
|
189 // The region that needs to be updated was stored in a property of the
|
Chris@0
|
190 // triggering element by views_ui_add_ajax_trigger(), so all we have to do is
|
Chris@0
|
191 // retrieve that here.
|
Chris@0
|
192 return NestedArray::getValue($form, $form_state->getTriggeringElement()['#views_ui_ajax_data']['refresh_parents']);
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * Non-Javascript fallback for updating the add view form.
|
Chris@0
|
197 */
|
Chris@0
|
198 function views_ui_nojs_submit($form, FormStateInterface $form_state) {
|
Chris@0
|
199 $form_state->setRebuild();
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 /**
|
Chris@0
|
203 * Add a <select> dropdown for a given section, allowing the user to
|
Chris@0
|
204 * change whether this info is stored on the default display or on
|
Chris@0
|
205 * the current display.
|
Chris@0
|
206 */
|
Chris@0
|
207 function views_ui_standard_display_dropdown(&$form, FormStateInterface $form_state, $section) {
|
Chris@0
|
208 $view = $form_state->get('view');
|
Chris@0
|
209 $display_id = $form_state->get('display_id');
|
Chris@0
|
210 $executable = $view->getExecutable();
|
Chris@0
|
211 $displays = $executable->displayHandlers;
|
Chris@0
|
212 $current_display = $executable->display_handler;
|
Chris@0
|
213
|
Chris@0
|
214 // @todo Move this to a separate function if it's needed on any forms that
|
Chris@0
|
215 // don't have the display dropdown.
|
Chris@0
|
216 $form['override'] = [
|
Chris@0
|
217 '#prefix' => '<div class="views-override clearfix form--inline views-offset-top" data-drupal-views-offset="top">',
|
Chris@0
|
218 '#suffix' => '</div>',
|
Chris@0
|
219 '#weight' => -1000,
|
Chris@0
|
220 '#tree' => TRUE,
|
Chris@0
|
221 ];
|
Chris@0
|
222
|
Chris@0
|
223 // Add the "2 of 3" progress indicator.
|
Chris@0
|
224 if ($form_progress = $view->getFormProgress()) {
|
Chris@0
|
225 $form['progress']['#markup'] = '<div id="views-progress-indicator" class="views-progress-indicator">' . t('@current of @total', ['@current' => $form_progress['current'], '@total' => $form_progress['total']]) . '</div>';
|
Chris@0
|
226 $form['progress']['#weight'] = -1001;
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 // The dropdown should not be added when :
|
Chris@0
|
230 // - this is the default display.
|
Chris@0
|
231 // - there is no master shown and just one additional display (mostly page)
|
Chris@0
|
232 // and the current display is defaulted.
|
Chris@0
|
233 if ($current_display->isDefaultDisplay() || ($current_display->isDefaulted($section) && !\Drupal::config('views.settings')->get('ui.show.master_display') && count($displays) <= 2)) {
|
Chris@0
|
234 return;
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 // Determine whether any other displays have overrides for this section.
|
Chris@0
|
238 $section_overrides = FALSE;
|
Chris@0
|
239 $section_defaulted = $current_display->isDefaulted($section);
|
Chris@0
|
240 foreach ($displays as $id => $display) {
|
Chris@0
|
241 if ($id === 'default' || $id === $display_id) {
|
Chris@0
|
242 continue;
|
Chris@0
|
243 }
|
Chris@0
|
244 if ($display && !$display->isDefaulted($section)) {
|
Chris@0
|
245 $section_overrides = TRUE;
|
Chris@0
|
246 }
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 $display_dropdown['default'] = ($section_overrides ? t('All displays (except overridden)') : t('All displays'));
|
Chris@0
|
250 $display_dropdown[$display_id] = t('This @display_type (override)', ['@display_type' => $current_display->getPluginId()]);
|
Chris@0
|
251 // Only display the revert option if we are in a overridden section.
|
Chris@0
|
252 if (!$section_defaulted) {
|
Chris@0
|
253 $display_dropdown['default_revert'] = t('Revert to default');
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 $form['override']['dropdown'] = [
|
Chris@0
|
257 '#type' => 'select',
|
Chris@0
|
258 // @TODO: Translators may need more context than this.
|
Chris@0
|
259 '#title' => t('For'),
|
Chris@0
|
260 '#options' => $display_dropdown,
|
Chris@0
|
261 ];
|
Chris@0
|
262 if ($current_display->isDefaulted($section)) {
|
Chris@0
|
263 $form['override']['dropdown']['#default_value'] = 'defaults';
|
Chris@0
|
264 }
|
Chris@0
|
265 else {
|
Chris@0
|
266 $form['override']['dropdown']['#default_value'] = $display_id;
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 }
|
Chris@0
|
270
|
Chris@0
|
271 /**
|
Chris@0
|
272 * Create the menu path for one of our standard AJAX forms based upon known
|
Chris@0
|
273 * information about the form.
|
Chris@0
|
274 *
|
Chris@0
|
275 * @return \Drupal\Core\Url
|
Chris@0
|
276 * The URL object pointing to the form URL.
|
Chris@0
|
277 */
|
Chris@0
|
278 function views_ui_build_form_url(FormStateInterface $form_state) {
|
Chris@0
|
279 $ajax = !$form_state->get('ajax') ? 'nojs' : 'ajax';
|
Chris@0
|
280 $name = $form_state->get('view')->id();
|
Chris@0
|
281 $form_key = $form_state->get('form_key');
|
Chris@0
|
282 $display_id = $form_state->get('display_id');
|
Chris@0
|
283
|
Chris@0
|
284 $form_key = str_replace('-', '_', $form_key);
|
Chris@0
|
285 $route_name = "views_ui.form_{$form_key}";
|
Chris@0
|
286 $route_parameters = [
|
Chris@0
|
287 'js' => $ajax,
|
Chris@0
|
288 'view' => $name,
|
Chris@17
|
289 'display_id' => $display_id,
|
Chris@0
|
290 ];
|
Chris@0
|
291 $url = Url::fromRoute($route_name, $route_parameters);
|
Chris@0
|
292 if ($type = $form_state->get('type')) {
|
Chris@0
|
293 $url->setRouteParameter('type', $type);
|
Chris@0
|
294 }
|
Chris@0
|
295 if ($id = $form_state->get('id')) {
|
Chris@0
|
296 $url->setRouteParameter('id', $id);
|
Chris@0
|
297 }
|
Chris@0
|
298 return $url;
|
Chris@0
|
299 }
|
Chris@0
|
300
|
Chris@0
|
301 /**
|
Chris@0
|
302 * #process callback for a button; determines if a button is the form's triggering element.
|
Chris@0
|
303 *
|
Chris@0
|
304 * The Form API has logic to determine the form's triggering element based on
|
Chris@0
|
305 * the data in POST. However, it only checks buttons based on a single #value
|
Chris@0
|
306 * per button. This function may be added to a button's #process callbacks to
|
Chris@0
|
307 * extend button click detection to support multiple #values per button. If the
|
Chris@0
|
308 * data in POST matches any value in the button's #values array, then the
|
Chris@0
|
309 * button is detected as having been clicked. This can be used when the value
|
Chris@0
|
310 * (label) of the same logical button may be different based on context (e.g.,
|
Chris@0
|
311 * "Apply" vs. "Apply and continue").
|
Chris@0
|
312 *
|
Chris@0
|
313 * @see _form_builder_handle_input_element()
|
Chris@0
|
314 * @see _form_button_was_clicked()
|
Chris@0
|
315 */
|
Chris@0
|
316 function views_ui_form_button_was_clicked($element, FormStateInterface $form_state) {
|
Chris@0
|
317 $user_input = $form_state->getUserInput();
|
Chris@0
|
318 $process_input = empty($element['#disabled']) && ($form_state->isProgrammed() || ($form_state->isProcessingInput() && (!isset($element['#access']) || $element['#access'])));
|
Chris@0
|
319 if ($process_input && !$form_state->getTriggeringElement() && !empty($element['#is_button']) && isset($user_input[$element['#name']]) && isset($element['#values']) && in_array($user_input[$element['#name']], array_map('strval', $element['#values']), TRUE)) {
|
Chris@0
|
320 $form_state->setTriggeringElement($element);
|
Chris@0
|
321 }
|
Chris@0
|
322 return $element;
|
Chris@0
|
323 }
|