4

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.

  • I guess one can't do it this way with an alter... one must override the user_admin_account on a menu level... some insight is found on the languageassign project source code.. – Raf A. Dec 23 '11 at 14:03
2

You can use Administration Views module which will allow you to work with that table as a View.

This means you can add fields, reorder them as you want and add custom filters for example.

User fields are supported out of the box, so you should be able to do what you want without any code.

It's much more flexible than altering the form, especially if you need some other custom functionality to it later on.

2

I faced a similar issue while adding a new field 'Mail' in the 'user_admin_account' form.

During my search I came across this post & also this post 'https://drupal.org/node/1899708' which address the same issue. I got a solution for this as described below:


/**
 * Implements hook_form_FORMID_alter()
 * @param $form
 * @param $form_state
 * @param $form_id
 * @return $form 
 */
function CUSTOMMODULE_form_user_admin_account_alter(&$form, &$form_state, $form_id) {
  // Adding User Mail Field in the 'user_admin_account' form
  $mail_header = array(
    'mail' => array('data' => t('Mail'), 'field' => 'u.mail'),
  );
  // Placing Mail at 2nd position
  array_splice($form['accounts']['#header'], 1, 0, $mail_header);

  foreach($form['accounts']['#options'] as $key =>$value){
       $query = db_select('users', 'u')
        ->fields('u', array('mail'))
        ->condition('u.uid', $key)
        ->execute()
    ->fetchAssoc();

    array_unshift($form['accounts']['#options'][$key], $query['mail']);
  }

  $form['pager'] = array('#markup' => theme('pager'));

  return $form;
}

Although this post dates back in '11, but as i did not get any solution based on my search so considered to post this answer. Hope it helps.

  • order by column mail not work – Michael Jan 30 '18 at 10:22
1

To answered Oct 7 '13 at 12:49 of dhrubaj

Is need to do like that:

$form['pager'] = array('#markup' => theme('pager', array('element' => 1)));

where 1 from 'element' => 1 is the number of needed pager from page, in that case we have two pager first (0) from user_admin_account (it exists in pager array) form and second our pager(1) from form user_custom_form_user_admin_account_alter.

Without 'element' => 1 theme('pager') render first pager that have limit 50 pages and we have some users and pager not show.

To see all pagers use:

 global $pager_page_array, $pager_total;
 var_dump($pager_page_array, $pager_total);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.