[development] hook_user('categories')

David Cohen drupal at dave-cohen.com
Wed May 6 20:24:51 UTC 2009


Thanks to those who replied to this thread.  Clearly this problem has
been encountered before and solved in different ways.  Here's what I
ended up with...

hook_user('categories') causes new menu items to be created.  And it
honors the 'access callback' and 'access arguments' that are returned by
hook_user('categories').  Because these are honored I did not have to
implement hook_menu or menu_alter.  But its undocumented, tricky to get
parameters right, and likely to break in future drupal (but then what
isn't?)

in hook_user('categories') I return an array containing:
      $items[] = array('name' => $fb_app->label,
                       'title' => $fb_app->title,  
                       'access callback' => 'fb_permission_access_own',
                       'access arguments' => array(1, 'edit own extended
                       permissions'),
                       'weight' => 2);

Then I define fb_permission_access_own(), a wrapper around user_access
that ensures the user is operating on her own account:
function fb_permission_access_own($account, $perm) {
  return ($GLOBALS['user']->uid == $account->uid && user_access($perm));
}

Et viola!

-Dave


On Tue, 28 Apr 2009 16:52 -0700, "David Cohen" <drupal at dave-cohen.com>
wrote:
> Sometime in the 5.x-6.x timeframe, the API for hook_user($op ==
> 'categories') changed in a major way.  No longer is the $account
> parameter passed in, and I think the data structure to be returned
> gained a lot of options.  But I can't find the documentation for this. 
> Its not in the 5.x to 6.x page on d.o (http://drupal.org/node/114774),
> nor is it reflected on http://api.drupal.org/api/function/hook_user/6.
> 
> 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
>     }
>   }
> 
> The above logic wont apply in D6 because $account is not passed in, and
> the return value is expected to have new menu data structure elements
> which take the place of that logic.  But I'm struggle to figure out, in
> part because I can't find the doc.
> 
> thanks.
> 


More information about the development mailing list