Community & Support

Adding a new column to the user list in admin/user/user

I'm trying to make a user subscription module (I know there is probably some module out there but this is a school project so I'm trying to do as much as possible myself.)

The thing I'm having trouble with is adding a new column to the list of users. I used "hook_form_alter" and have overridden the theme function. But as soon as I override it Drupal stops displaying the table and just prints out plain data. I tried clearing the cache but I'm not quite sure this is necessary because the theme registry is being rebuilt on every page request.

My hook_form_alter:

/**
* Implementation of hook_form_alter().
*/
function user_subscription_form_alter(&$form, $form_state, $form_id) {
    if ($form_id == 'user_admin_account') {
        $filter = user_build_filter_query();

        $header = array(
                array(),
                array('data' => t('Username'), 'field' => 'u.name'),
                array('data' => t('Status'), 'field' => 'u.status'),
                t('Roles'),
                array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
                array('data' => t('Last access'), 'field' => 'u.access'),
                t('Operations'),
                array('data' => t('Subscribed'), 'field' => 'u.subscribed') // <- new field
        );

        if ($filter['join'] != "") {
            $sql = 'SELECT DISTINCT u.uid, u.name, u.status, u.created, u.access, u.subscribed FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
            $query_count = 'SELECT COUNT(DISTINCT u.uid) FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
        }
        else {
            $sql = 'SELECT u.uid, u.name, u.status, u.created, u.access, u.subscribed FROM {users} u WHERE u.uid != 0 '. $filter['where'];
            $query_count = 'SELECT COUNT(u.uid) FROM {users} u WHERE u.uid != 0 '. $filter['where'];
        }

        $sql .= tablesort_sql($header);

        $result = pager_query($sql, 50, 0, $query_count, $filter['args']);

        $subscribed = array(t('no'), t('yes'));
        while ($account = db_fetch_object($result)) {
            $form['subscribed'][$account->uid] =  array('#value' => $subscribed[$account->subscribed]);
        }

        // override the default theme function to render the extra field
        $form['#theme'] = 'theme_user_subscription_form';
    }
}

My theme override method:

/**
* Theme the user list table.
*
* This is just theme_user_admin_account()
* from modules/user/user.admin.inc
* with support for the extra field.
*/
function theme_user_subscription_form($form) {
    // Overview table:
    $header = array(
            theme('table_select_header_cell'),
            array('data' => t('Username'), 'field' => 'u.name'),
            array('data' => t('Status'), 'field' => 'u.status'),
            t('Roles'),
            array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
            array('data' => t('Last access'), 'field' => 'u.access'),
            t('Operations'),
            array('data' => t('Subscribed'), 'field' => 'u.subscribed') // <- new field
    );

    $output = drupal_render($form['options']);
    if (isset($form['name']) && is_array($form['name'])) {
        foreach (element_children($form['name']) as $key) {
            $rows[] = array(
                    drupal_render($form['accounts'][$key]),
                    drupal_render($form['name'][$key]),
                    drupal_render($form['status'][$key]),
                    drupal_render($form['roles'][$key]),
                    drupal_render($form['member_for'][$key]),
                    drupal_render($form['last_access'][$key]),
                    drupal_render($form['operations'][$key]),
                    drupal_render($form['subscribed'][$key]) // <- new field
            );
        }
    }
    else {
        $rows[] = array(array('data' => t('No users available.'), 'colspan' => ' 8'));
    }

    $output .= theme('table', $header, $rows);
    if ($form['pager']['#value']) {
        $output .= drupal_render($form['pager']);
    }

    $output .= drupal_render($form);

    return $output;
}

It's pretty much straight out of user.admin.inc really, am I missing something?

Thanks in advance

Comments

Hi, does your module

Hi,

does your module implement the hook_theme? (http://api.drupal.org/api/function/hook_theme/6). I think it must declare first the theme in hook_theme in order to be able to use it in your module.

Vasi.

Hello vasi, No I did not

Hello vasi,

No I did not implement hook_theme. Would it be possible to use the theme from the user module? And if so, how?

Thanks in advance

*EDIT*

You were right!

I added

/**
* Implementation of hook_theme().
*/
function user_subscription_theme() {
  return array(
    'user_subscription_form' => array(
      'arguments' => array('form' => array()),
    ),
  );
}

And changed

$form['#theme'] = 'theme_user_subscription_form';

to
$form['#theme'] = 'user_subscription_form';

Thank you very much!

In your custom module (module: custom_user) {I required to show the company name in the user list of the admin which I did in the following way}

function custom_user_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_admin_account':
$form['company'] = array();
foreach($form['operations'] as $uid=>$current_html){
$form['company'][$uid]['#value'] = " Test";
}
$form['#theme'] = 'user_custom_form';
break;
}
}

function custom_user_theme() {
return array(
'user_custom_form' => array(
'arguments' => array('form'=> NULL),
),
);
}

function theme_user_custom_form($form) {

$header = array(
theme('table_select_header_cell'),
array('data' => t('Username'), 'field' => 'u.name'),
array('data' => t('Status'), 'field' => 'u.status'),
t('Roles'),
array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
array('data' => t('Last access'), 'field' => 'u.access'),
t('Operations'),
t('Company')// <- new field
);

$output = drupal_render($form['options']);
if (isset($form['name']) && is_array($form['name'])) {
foreach (element_children($form['name']) as $key) {
$rows[] = array(
drupal_render($form['accounts'][$key]),
drupal_render($form['name'][$key]),
drupal_render($form['status'][$key]),
drupal_render($form['roles'][$key]),
drupal_render($form['member_for'][$key]),
drupal_render($form['last_access'][$key]),
drupal_render($form['operations'][$key]),
drupal_render($form['company'][$key]) // <- new field
);
}
}
else {
$rows[] = array(array('data' => t('No users available.'), 'colspan' => ' 8'));
}

$output .= theme('table', $header, $rows);
if ($form['pager']['#value']) {
$output .= drupal_render($form['pager']);
}

$output .= drupal_render($form);

return $output;
}

steps 1: over ride the theme
2. add your new field in your custom theme
3. add the required data of the form through hook_form_alter
that all

nobody click here