admin.inc 14.4 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Provides the Views' administrative interface.
 */

8
use Drupal\Component\Utility\NestedArray;
9
use Drupal\Core\Form\FormStateInterface;
10
use Drupal\Core\Url;
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
/**
 * Converts a form element in the add view wizard to be AJAX-enabled.
 *
 * This function takes a form element and adds AJAX behaviors to it such that
 * changing it triggers another part of the form to update automatically. It
 * also adds a submit button to the form that appears next to the triggering
 * element and that duplicates its functionality for users who do not have
 * JavaScript enabled (the button is automatically hidden for users who do have
 * JavaScript).
 *
 * To use this function, call it directly from your form builder function
 * immediately after you have defined the form element that will serve as the
 * JavaScript trigger. Calling it elsewhere (such as in hook_form_alter()) may
 * mean that the non-JavaScript fallback button does not appear in the correct
 * place in the form.
 *
 * @param $wrapping_element
 *   The element whose child will server as the AJAX trigger. For example, if
 *   $form['some_wrapper']['triggering_element'] represents the element which
 *   will trigger the AJAX behavior, you would pass $form['some_wrapper'] for
 *   this parameter.
 * @param $trigger_key
 *   The key within the wrapping element that identifies which of its children
 *   serves as the AJAX trigger. In the above example, you would pass
 *   'triggering_element' for this parameter.
 * @param $refresh_parents
 *   An array of parent keys that point to the part of the form that will be
 *   refreshed by AJAX. For example, if triggering the AJAX behavior should
 *   cause $form['dynamic_content']['section'] to be refreshed, you would pass
 *   array('dynamic_content', 'section') for this parameter.
 */
function views_ui_add_ajax_trigger(&$wrapping_element, $trigger_key, $refresh_parents) {
44 45
  $seen_ids = &drupal_static(__FUNCTION__ . ':seen_ids', []);
  $seen_buttons = &drupal_static(__FUNCTION__ . ':seen_buttons', []);
46 47 48 49

  // Add the AJAX behavior to the triggering element.
  $triggering_element = &$wrapping_element[$trigger_key];
  $triggering_element['#ajax']['callback'] = 'views_ui_ajax_update_form';
50

51 52 53 54 55 56
  // We do not use \Drupal\Component\Utility\Html::getUniqueId() to get an ID
  // for the AJAX wrapper, because it remembers IDs across AJAX requests (and
  // won't reuse them), but in our case we need to use the same ID from request
  // to request so that the wrapper can be recognized by the AJAX system and
  // its content can be dynamically updated. So instead, we will keep track of
  // duplicate IDs (within a single request) on our own, later in this function.
57 58 59 60 61
  $triggering_element['#ajax']['wrapper'] = 'edit-view-' . implode('-', $refresh_parents) . '-wrapper';

  // Add a submit button for users who do not have JavaScript enabled. It
  // should be displayed next to the triggering element on the form.
  $button_key = $trigger_key . '_trigger_update';
62
  $element_info = \Drupal::service('element_info');
63
  $wrapping_element[$button_key] = [
64 65
    '#type' => 'submit',
    // Hide this button when JavaScript is enabled.
66 67
    '#attributes' => ['class' => ['js-hide']],
    '#submit' => ['views_ui_nojs_submit'],
68 69 70 71
    // Add a process function to limit this button's validation errors to the
    // triggering element only. We have to do this in #process since until the
    // form API has added the #parents property to the triggering element for
    // 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 75 76 77 78 79
    // Add an after-build function that inserts a wrapper around the region of
    // the form that needs to be refreshed by AJAX (so that the AJAX system can
    // detect and dynamically update it). This is done in #after_build because
    // it's a convenient place where we have automatic access to the complete
    // form array, but also to minimize the chance that the HTML we add will
    // get clobbered by code that runs after we have added it.
80 81
    '#after_build' => array_merge($element_info->getInfoProperty('submit', '#after_build', []), ['views_ui_add_ajax_wrapper']),
  ];
82 83
  // Copy #weight and #access from the triggering element to the button, so
  // that the two elements will be displayed together.
84
  foreach (['#weight', '#access'] as $property) {
85 86 87 88 89 90
    if (isset($triggering_element[$property])) {
      $wrapping_element[$button_key][$property] = $triggering_element[$property];
    }
  }
  // For easiest integration with the form API and the testing framework, we
  // 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 99 100
    $seen_buttons[$button_title] = 1;
  }
  else {
101
    $wrapping_element[$button_key]['#value'] = t('Update "@title" choice (@number)', [
102 103
      '@title' => $button_title,
      '@number' => ++$seen_buttons[$button_title],
104
    ]);
105 106 107 108
  }

  // Attach custom data to the triggering element and submit button, so we can
  // use it in both the process function and AJAX callback.
109
  $ajax_data = [
110 111 112
    'wrapper' => $triggering_element['#ajax']['wrapper'],
    'trigger_key' => $trigger_key,
    'refresh_parents' => $refresh_parents,
113
  ];
114 115 116 117 118 119 120 121
  $seen_ids[$triggering_element['#ajax']['wrapper']] = TRUE;
  $triggering_element['#views_ui_ajax_data'] = $ajax_data;
  $wrapping_element[$button_key]['#views_ui_ajax_data'] = $ajax_data;
}

/**
 * Processes a non-JavaScript fallback submit button to limit its validation errors.
 */
122
function views_ui_add_limited_validation($element, FormStateInterface $form_state) {
123 124 125 126 127 128 129
  // Retrieve the AJAX triggering element so we can determine its parents. (We
  // know it's at the same level of the complete form array as the submit
  // button, so all we have to do to find it is swap out the submit button's
  // last array parent.)
  $array_parents = $element['#array_parents'];
  array_pop($array_parents);
  $array_parents[] = $element['#views_ui_ajax_data']['trigger_key'];
130
  $ajax_triggering_element = NestedArray::getValue($form_state->getCompleteForm(), $array_parents);
131 132 133 134

  // Limit this button's validation to the AJAX triggering element, so it can
  // update the form for that change without requiring that the rest of the
  // 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 139 140 141 142
  // was clicked, the form API workflow in \Drupal::formBuilder()->doBuildForm()
  // will have already copied it to $form_state->getTriggeringElement() before
  // our #process function is run. So we need to make the same modifications in
  // $form_state as we did to the element itself, to ensure that
  // #limit_validation_errors will actually be set in the correct place.
143 144 145
  $clicked_button = &$form_state->getTriggeringElement();
  if ($clicked_button && $clicked_button['#name'] == $element['#name'] && $clicked_button['#value'] == $element['#value']) {
    $clicked_button['#limit_validation_errors'] = $element['#limit_validation_errors'];
146 147 148 149 150 151 152 153 154 155 156 157
  }

  return $element;
}

/**
 * After-build function that adds a wrapper to a form region (for AJAX refreshes).
 *
 * This function inserts a wrapper around the region of the form that needs to
 * be refreshed by AJAX, based on information stored in the corresponding
 * submit button form element.
 */
158
function views_ui_add_ajax_wrapper($element, FormStateInterface $form_state) {
159 160 161 162 163 164 165 166 167
  // Find the region of the complete form that needs to be refreshed by AJAX.
  // This was earlier stored in a property on the element.
  $complete_form = &$form_state->getCompleteForm();
  $refresh_parents = $element['#views_ui_ajax_data']['refresh_parents'];
  $refresh_element = NestedArray::getValue($complete_form, $refresh_parents);

  // The HTML ID that AJAX expects was also stored in a property on the
  // element, so use that information to insert the wrapper <div> here.
  $id = $element['#views_ui_ajax_data']['wrapper'];
168
  $refresh_element += [
169 170
    '#prefix' => '',
    '#suffix' => '',
171
  ];
172 173 174 175 176 177
  $refresh_element['#prefix'] = '<div id="' . $id . '" class="views-ui-ajax-wrapper">' . $refresh_element['#prefix'];
  $refresh_element['#suffix'] .= '</div>';

  // Copy the element that needs to be refreshed back into the form, with our
  // modifications to it.
  NestedArray::setValue($complete_form, $refresh_parents, $refresh_element);
178 179 180 181 182 183 184 185 186 187

  return $element;
}

/**
 * Updates a part of the add view form via AJAX.
 *
 * @return
 *   The part of the form that has changed.
 */
188
function views_ui_ajax_update_form($form, FormStateInterface $form_state) {
189 190 191
  // The region that needs to be updated was stored in a property of the
  // triggering element by views_ui_add_ajax_trigger(), so all we have to do is
  // retrieve that here.
192
  return NestedArray::getValue($form, $form_state->getTriggeringElement()['#views_ui_ajax_data']['refresh_parents']);
193 194 195 196 197
}

/**
 * Non-Javascript fallback for updating the add view form.
 */
198
function views_ui_nojs_submit($form, FormStateInterface $form_state) {
199
  $form_state->setRebuild();
200 201 202 203 204 205 206
}

/**
 * Add a <select> dropdown for a given section, allowing the user to
 * change whether this info is stored on the default display or on
 * the current display.
 */
207
function views_ui_standard_display_dropdown(&$form, FormStateInterface $form_state, $section) {
208 209
  $view = $form_state->get('view');
  $display_id = $form_state->get('display_id');
210
  $executable = $view->getExecutable();
211 212
  $displays = $executable->displayHandlers;
  $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 219 220
    '#suffix' => '</div>',
    '#weight' => -1000,
    '#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 227 228
    $form['progress']['#weight'] = -1001;
  }

229 230 231 232 233
  // The dropdown should not be added when :
  // - this is the default display.
  // - there is no master shown and just one additional display (mostly page)
  //   and the current display is defaulted.
  if ($current_display->isDefaultDisplay() || ($current_display->isDefaulted($section) && !\Drupal::config('views.settings')->get('ui.show.master_display') && count($displays) <= 2)) {
234 235 236 237 238
    return;
  }

  // Determine whether any other displays have overrides for this section.
  $section_overrides = FALSE;
239
  $section_defaulted = $current_display->isDefaulted($section);
240 241 242 243
  foreach ($displays as $id => $display) {
    if ($id === 'default' || $id === $display_id) {
      continue;
    }
244
    if ($display && !$display->isDefaulted($section)) {
245 246 247 248 249
      $section_overrides = TRUE;
    }
  }

  $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 252 253 254 255
  // Only display the revert option if we are in a overridden section.
  if (!$section_defaulted) {
    $display_dropdown['default_revert'] = t('Revert to default');
  }

256
  $form['override']['dropdown'] = [
257
    '#type' => 'select',
258 259
    // @TODO: Translators may need more context than this.
    '#title' => t('For'),
260
    '#options' => $display_dropdown,
261
  ];
262
  if ($current_display->isDefaulted($section)) {
263 264 265 266 267 268 269 270 271
    $form['override']['dropdown']['#default_value'] = 'defaults';
  }
  else {
    $form['override']['dropdown']['#default_value'] = $display_id;
  }

}

/**
272
 * Create the menu path for one of our standard AJAX forms based upon known
273
 * information about the form.
274 275 276
 *
 * @return \Drupal\Core\Url
 *   The URL object pointing to the form URL.
277
 */
278
function views_ui_build_form_url(FormStateInterface $form_state) {
279 280
  $ajax = !$form_state->get('ajax') ? 'nojs' : 'ajax';
  $name = $form_state->get('view')->id();
281 282
  $form_key = $form_state->get('form_key');
  $display_id = $form_state->get('display_id');
283 284 285 286 287 288

  $form_key = str_replace('-', '_', $form_key);
  $route_name = "views_ui.form_{$form_key}";
  $route_parameters = [
    'js' => $ajax,
    'view' => $name,
289
    'display_id' => $display_id,
290 291
  ];
  $url = Url::fromRoute($route_name, $route_parameters);
292
  if ($type = $form_state->get('type')) {
293
    $url->setRouteParameter('type', $type);
294
  }
295
  if ($id = $form_state->get('id')) {
296
    $url->setRouteParameter('id', $id);
297
  }
298
  return $url;
299 300 301 302 303 304
}

/**
 * #process callback for a button; determines if a button is the form's triggering element.
 *
 * The Form API has logic to determine the form's triggering element based on
305
 * the data in POST. However, it only checks buttons based on a single #value
306 307
 * per button. This function may be added to a button's #process callbacks to
 * extend button click detection to support multiple #values per button. If the
308
 * data in POST matches any value in the button's #values array, then the
309 310 311 312 313 314 315
 * button is detected as having been clicked. This can be used when the value
 * (label) of the same logical button may be different based on context (e.g.,
 * "Apply" vs. "Apply and continue").
 *
 * @see _form_builder_handle_input_element()
 * @see _form_button_was_clicked()
 */
316
function views_ui_form_button_was_clicked($element, FormStateInterface $form_state) {
317
  $user_input = $form_state->getUserInput();
318
  $process_input = empty($element['#disabled']) && ($form_state->isProgrammed() || ($form_state->isProcessingInput() && (!isset($element['#access']) || $element['#access'])));
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)) {
320
    $form_state->setTriggeringElement($element);
321 322 323
  }
  return $element;
}