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