[development] hook_user('categories')

Jakob Petsovits jpetso at gmx.at
Wed Apr 29 06:14:15 UTC 2009

On Wednesday 29 April 2009, David Cohen wrote:
> Can someone point me to general doc, or an example of how to emulate the
> following logic in D6.  The logic is basically to show the user an edit
> category only if they have permission to change their own settings.
>   if ($op == 'categories') {
>     if (user_access('edit own extended permissions') &&
>         $user->uid == $account->uid) {
>       // build the data structure
>     }
>   }

First up, hook_user($op='categories') is one giant hack, and results in 
nothing else than inserting a couple of menu items. In Drupal 6 it adapted to 
the new menu system, which means you don't return categories depending on 
dynamic permission tests. Instead, use access callbacks to check for 
permissions. Something like this worked for me (another hack, so to say):

function versioncontrol_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'categories':
      $categories = array();
      $categories[] = array(
        'name' => 'versioncontrol',
        // user_menu() pipes 'title' though check_plain() already.
        'title' => 'Repository accounts',
        'weight' => 99,
      return $categories;

function versioncontrol_menu() {
  $items['user/%versioncontrol_user_accounts/edit/versioncontrol'] = array(
    'title callback' => 'versioncontrol_user_accounts_title_callback',
    'title arguments' => array(1),
    'page callback' => 'versioncontrol_account_page',
    'page arguments' => array(1),
    'access callback' => 'versioncontrol_private_account_access',
    'access arguments' => array(1),
    'file' => 'versioncontrol.pages.inc',
    'weight' => 99,
    'type' => MENU_LOCAL_TASK,

Somehow, these two are getting merged in the end. (I guess they aren't
really getting merged but my item is preferred so it works. Probably 
hook_menu_alter() is a cleaner solution and would result in a genuinely merged 
item.) I'd prefer to skip hook_user() altogether, but that results in bugs 
with tab display.

If someone has a proper solution for this common piece of functionality,
I'm all ears.


More information about the development mailing list