I get the search form back (instead of a list of users) when trying to search users in /admin/user/search.

Comments

ankur’s picture

I can confirm this and can also add that the problem occurs in the section of code in user.module:

function user_admin() {
  $edit = isset($_POST['edit']) ? $_POST['edit'] : '';
  $op = isset($_POST['op']) ? $_POST['op'] : '';

  if (empty($op)) {
    $op = arg(2);
  }

  switch ($op) {
    case 'search':
    case t('Search'):
      $output = search_form(url('admin/user/search'), $_POST['edit']['keys'], 'user') . search_data($_POST['edit']['keys'], 'user');
      break;
    case t('Create new account'):
    case 'create':
      $output = user_register();
      break;
    default:
      $output = user_admin_account();
  }
  return $output;
}

In this code, the call to search_data() should return the themed search results, however, the call to search_form() on the same line results in a call to drupal_get_form():

function search_form($action = '', $keys = '', $type = null, $prompt = null) {

  if (!$action) {
    $action = url('search/'. $type);
  }
  if (is_null($prompt)) {
    $prompt = t('Enter your keywords');
  }

  $form = array();
  $form['#action'] = $action;
  $form['#attributes'] = array('class' => 'search-form');
  $form['basic'] = array('#type' => 'item', '#title' => $prompt);
  $form['basic']['inline'] = array('#type' => 'markup', '#prefix' => '<div class="container-inline">', '#suffix' => '</div>');
  $form['basic']['inline']['keys'] = array('#type' => 'textfield', '#title' => '', '#default_value' => $keys, '#size' => $prompt ? 40 : 20, '#maxlength' => 255);
  $form['basic']['inline']['submit'] = array('#type' => 'submit', '#value' => t('Search'));

  $form_module = module_invoke($type, 'search', 'form', $keys);
  if (isset($form_module) && is_array($form_module)) {
    $form = array_merge($form, $form_module);
  }

  return drupal_get_form('search_form', $form);
}

I have verified, w/ print statements, that the call to drupal_get_form() results in the execution of the drupal_goto() at the very end of drupal_get_form(). This, in turn, forces a redirect which caues $_POST[] to lose its values. Not sure what the exact cause is. The problem doesn't occur in the search form at q=search, because the drupal_goto() includes the search terms as a $_GET[] variable appended to the URL. In short, because of the drupal_goto(), there are no values set in $_POST[]. So, the code executes again and an unset $_POST['edit']['keys'] value is passed to the search_data() call in user_admin(). That's why there're no search results and a blank search form.

drupal_get_form() is a maze of code I couldn't follow this late into the night, but it is defined in form.inc.

chx’s picture

Status: Active » Closed (duplicate)