I am working to change the standard display when pressing the "People" button. Instead of showing "Username" I want it to show a custom field. For example "Last name".
I almost made it work, but the pager is not working. When I click page 2, the same info shows as page 1. Sorting is working though.
Here's the slightly changed code, which originated from the core user module.
<?php
function user_custom_form_user_admin_account_alter(&$form, &$form_state, $form_id) {
$header = array(
'field_lastname_value' => array('data' => t('Username'), 'field' => 'dn.field_lastname_value', 'sort' => 'asc'),
'status' => array('data' => t('Status'), 'field' => 'u.status'),
'roles' => array('data' => t('Roles')),
'member_for' => array('data' => t('Member for'), 'field' => 'u.created'),
'access' => array('data' => t('Last access'), 'field' => 'u.access'),
'operations' => array('data' => t('Operations')),
);
$query = db_select('users', 'u');
$query->condition('u.uid', 0, '<>');
$query->leftJoin('field_data_field_lastname', 'dn', 'u.uid = dn.entity_id');
user_build_filter_query($query);
$count_query = clone $query;
$count_query->addExpression('COUNT(dn.entity_id)'); // or u.uid, does not make any diff.
$query = $query->extend('PagerDefault')->extend('TableSort');
$query
->fields('u', array('uid', 'name', 'status', 'created', 'access'))
->fields('dn', array('field_lastname_value'))
->limit(20)
->orderByHeader($header)
->setCountQuery($count_query);
$result = $query->execute();
...
$account->name = $account->field_lastname_value;
$options[$account->uid] = array(
'field_lastname_value' => theme('username', array('account' => $account)),
'status' => $status[$account->status],
'roles' => theme('item_list', array('items' => $users_roles)),
'member_for' => format_interval(REQUEST_TIME - $account->created),
'access' => $account->access ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $account->access))) : t('never'),
'operations' => array('data' => array('#type' => 'link', '#title' => t('edit'), '#href' => "user/$account->uid/edit", '#options' => array('query' => $destination))),
);
...
}
?>
Can anyone help? I have been struggling with this for hours. I did try changing the position of pagerDefault and TableSort to all kind of possible places like in the beginning before the join, after the join, tried with other joins then just left join, I'm stuck. Maybe I have to call SELECT FROM the custom field table and join the users table? But that's odd. Any help is really appreciated.