You are here

function views_ajax_form_wrapper in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 8.3 includes/ajax.inc \views_ajax_form_wrapper()
  2. 6.3 includes/ajax.inc \views_ajax_form_wrapper()
  3. 6.2 includes/ajax.inc \views_ajax_form_wrapper()

Wrapper around drupal_build_form to handle some AJAX stuff automatically.

This makes some assumptions about the client.

Related topics

1 call to views_ajax_form_wrapper()
views_ui_ajax_form in includes/admin.inc
Generic entry point to handle forms.

File

includes/ajax.inc, line 245
Handles the server side AJAX interactions of Views.

Code

function views_ajax_form_wrapper($form_id, &$form_state) {
  ctools_include('dependent');

  // This won't override settings already in.
  $form_state += array(
    'rerender' => FALSE,
    'no_redirect' => !empty($form_state['ajax']),
    'no_cache' => TRUE,
    'build_info' => array(
      'args' => array(),
    ),
  );
  $form = drupal_build_form($form_id, $form_state);
  $output = drupal_render($form);

  // These forms have the title built in, so set the title here.
  if (empty($form_state['ajax']) && !empty($form_state['title'])) {
    drupal_set_title($form_state['title']);
    drupal_add_css(drupal_get_path('module', 'views_ui') . '/css/views-admin.css');
  }
  if (!empty($form_state['ajax']) && (empty($form_state['executed']) || !empty($form_state['rerender']))) {

    // If the form didn't execute and we're using ajax, build up a AJAX command
    // list to execute.
    $commands = array();
    $display = '';
    if ($messages = theme('status_messages')) {
      $display = '<div class="views-messages">' . $messages . '</div>';
    }
    $display .= $output;
    $title = empty($form_state['title']) ? '' : $form_state['title'];
    if (!empty($form_state['help_topic'])) {
      $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
      if (module_exists('advanced_help')) {
        $title = theme('advanced_help_topic', array(
          'module' => $module,
          'topic' => $form_state['help_topic'],
        )) . $title;
      }
    }
    $url = empty($form_state['url']) ? url($_GET['q'], array(
      'absolute' => TRUE,
    )) : $form_state['url'];
    $commands[] = views_ajax_command_set_form($display, $title, $url);
    if (!empty($form_state['#section'])) {
      $commands[] = views_ajax_command_hilite('.' . drupal_clean_css_identifier($form_state['#section']));
    }
    return $commands;
  }

  // These forms have the title built in, so set the title here.
  if (empty($form_state['ajax']) && !empty($form_state['title'])) {
    drupal_set_title($form_state['title']);
  }
  return $output;
}