Commit 66ec59e0 authored by webchick's avatar webchick

#664042 by coltrane, effulgentsia: Fixed TableSort order error when no sort set in header

parent 34a78487
......@@ -128,12 +128,14 @@ protected function order() {
}
else {
// The first column specified is initial 'order by' field unless otherwise specified
if (is_array($this->header[0])) {
$this->header[0] += array('data' => NULL, 'field' => NULL);
return array('name' => $this->header[0]['data'], 'sql' => $this->header[0]['field']);
$headers = array_values($this->header);
$header = $headers[0];
if (is_array($header)) {
$header += array('data' => NULL, 'field' => NULL);
return array('name' => $header['data'], 'sql' => $header['field']);
}
else {
return array('name' => $this->header[0]);
return array('name' => $header);
}
}
}
......
......@@ -66,7 +66,11 @@ function database_test_menu() {
'access callback' => TRUE,
'page callback' => 'database_test_tablesort_first',
);
$items['database_test/tablesort_default_sort'] = array(
'access callback' => TRUE,
'page callback' => 'drupal_get_form',
'page arguments' => array('database_test_theme_tablesort'),
);
return $items;
}
......@@ -191,3 +195,48 @@ function database_test_tablesort_first() {
));
exit;
}
/**
* Output a form without setting a header sort.
*/
function database_test_theme_tablesort($form, &$form_state) {
$header = array(
'username' => array('data' => t('Username'), 'field' => 'u.name'),
'status' => array('data' => t('Status'), 'field' => 'u.status'),
);
$query = db_select('users', 'u');
$query->condition('u.uid', 0, '<>');
user_build_filter_query($query);
$count_query = clone $query;
$count_query->addExpression('COUNT(u.uid)');
$query = $query->extend('PagerDefault')->extend('TableSort');
$query
->fields('u', array('uid', 'name', 'status', 'created', 'access'))
->limit(50)
->orderByHeader($header)
->setCountQuery($count_query);
$result = $query->execute();
$options = array();
$status = array(t('blocked'), t('active'));
$accounts = array();
foreach ($result as $account) {
$options[$account->uid] = array(
'username' => check_plain($account->name),
'status' => $status[$account->status],
);
}
$form['accounts'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No people available.'),
);
return $form;
}
......@@ -2314,6 +2314,15 @@ class DatabaseSelectTableSortDefaultTestCase extends DatabaseTestCase {
$this->assertEqual($last->task, $sort['last'], t('Items appear in the correct order sorting by @field @sort.', array('@field' => $sort['field'], '@sort' => $sort['sort'])));
}
}
/**
* Confirm that if a sort is not set in a tableselect form there is no error thrown when using the default.
*/
function testTableSortDefaultSort() {
$this->drupalGet('database_test/tablesort_default_sort');
// Any PHP errors or notices thrown would trigger a simpletest exception, so
// no additional assertions are needed.
}
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment