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.
Hi David, I hope this helps you out: You'll now note this function _user_categories($account) which invokes the hook_categories(), so you can in fact simply build your categories that way. Here is an example of some code from a project I was working on (it will give you an idea of what you can do with it): ====== function custom_categories($uid, $type, $type_name) { $content = array(); if (user_access('edit own extended permissions')) { $content['content_profile_'. $type] = array( '#theme' => 'content_profile_display_view', '#edit_link' => content_profile_get_settings($type, 'edit_link'), '#uid' => $uid, '#style' => $style, '#content_type' => $type, '#weight' => content_profile_get_settings($type, 'weight'), '#suffix' => '<br />', ); // Working around the bug described at http://drupal.org/node/302873 module_load_include('inc', 'content_profile', 'content_profile.theme'); } $content['#prefix'] = '<p id="content-profile-view">'; $content['#suffix'] = '</p>'; return $content; } ====== I'm also using the content_profile module as an API for added functions. But those can be replaced with whatever functions you want. I hope that is of use to you. -wilco Quoting "David Cohen" <drupal@dave-cohen.com>:
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.
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. Cheers, Jakob
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.
I had the same problem with content profile. I also worked around it by adding the (unwanted) user categories and I altered the menu items - works fine. Here is more background: http://drupal.org/node/367202#comment-1246922 regards, fago
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@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.
participants (4)
-
David Cohen -
fago -
Jakob Petsovits -
William Roboly