1.20.x views_ui.admin.inc views_ui_ajax_form($js, $key, $view, $display_id = '')

Generic entry point to handle forms.

We do this for consistency and to make it easy to chain forms together.

File

modules/views_ui/views_ui.admin.inc, line 2821
Admin page callbacks for the Views UI module.

Code

function views_ui_ajax_form($js, $key, $view, $display_id = '') {
  // Reset the cache of IDs. Backdrop rather aggressively prevents ID
  // duplication but this causes it to remember IDs that are no longer even
  // being used.
  if (isset($_POST['ajax_html_ids'])) {
    unset($_POST['ajax_html_ids']);
  }

  // AJAX requests may not have the triggering element set in POST, but ajax.js
  // adds a triggering element. Add to POST to ensure the correct button submit
  // handlers fire.
  if (isset($_POST['_triggering_element_name']) && isset($_POST['_triggering_element_value'])) {
    $_POST[$_POST['_triggering_element_name']] = $_POST['_triggering_element_value'];
  }

  $form = views_ui_ajax_forms($key);
  if (empty($form)) {
    return MENU_NOT_FOUND;
  }

  views_include('ajax');
  $args = func_get_args();
  // Remove the known args
  array_splice($args, 0, 4);

  $form_state = views_ui_build_form_state($js, $key, $view, $display_id, $args);
  // check to see if this is the top form of the stack. If it is, pop
  // it off; if it isn't, the user clicked somewhere else and the stack is
  // now irrelevant.
  if (!empty($view->stack)) {
    $identifier = views_ui_build_identifier($key, $view, $display_id, $args);
    // Retrieve the first form from the stack without changing the integer keys,
    // as they're being used for the "2 of 3" progress indicator.
    reset($view->stack);
    $key = key($view->stack);
    $top = current($view->stack);
    unset($view->stack[$key]);

    if (array_shift($top) != $identifier) {
      $view->stack = array();
    }
  }

  // Automatically remove the form cache if it is set and the key does
  // not match. This way navigating away from the form without hitting
  // update will work.
  if (isset($view->form_cache) && $view->form_cache['key'] != $key) {
    unset($view->form_cache);
  }

  // With the below logic, we may end up rendering a form twice (or two forms
  // each sharing the same element ids), potentially resulting in
  // backdrop_add_js() being called twice to add the same setting. backdrop_get_js()
  // is ok with that, but until ajax_render() is (http://drupal.org/node/208611),
  // reset the backdrop_add_js() static before rendering the second time.
  $backdrop_add_js_original = backdrop_add_js();
  $backdrop_add_js = &backdrop_static('backdrop_add_js');
  $output = views_ajax_form_wrapper($form_state['form_id'], $form_state);
  if ($form_state['submitted'] && empty($form_state['rerender'])) {
    // Sometimes we need to re-generate the form for multi-step type operations.
    $object = NULL;
    if (!empty($view->stack)) {
      $backdrop_add_js = $backdrop_add_js_original;
      $stack = $view->stack;
      $top = array_shift($stack);
      $top[0] = $js;

      // Change view into a reference.
      $stepview = $top[2];
      $top[2] = &$stepview;

      $form_state = call_user_func_array('views_ui_build_form_state', $top);
      $form_state['input'] = array();
      $form_state['path'] = views_ui_build_form_path($form_state);
      if (!$js) {
        return backdrop_goto(views_ui_build_form_path($form_state));
      }
      $output = views_ajax_form_wrapper($form_state['form_id'], $form_state);
    }
    elseif (!$js) {
      // if nothing on the stack, non-js forms just go back to the main view editor.
      return backdrop_goto("admin/structure/views/view/$view->name/configure");
    }
    else {
      $output = array();
      $output[] = ajax_command_close_modal_dialog();
      $output[] = views_ajax_command_show_buttons();
      $output[] = views_ajax_command_trigger_preview();
      if (!empty($form_state['#page_title'])) {
        $output[] = views_ajax_command_replace_title($form_state['#page_title']);
      }
    }
    // If this form was for view-wide changes, there's no need to regenerate
    // the display section of the form.
    if ($display_id !== '') {
      views_ui_regenerate_tab($view, $output, $display_id);
    }
  }

  return $js ? array('#type' => 'ajax', '#commands' => $output) : $output;
}