views:

34

answers:

2

Hi,

I need a function for a plugin to check, if the shown account is the user's account or that from another one. I am using this function for that:

global $user; $account;
$account = user_load(array('uid' => arg(1)));
if ( $user->uid == $account->uid ) {

} 

I am doing that inside a module but it does not work. When I go to my profile, I never see the output from the function.

Why?

Edit

Original context of this code:

function tpzclassified_menu() { 
  global $user; 
  $account = user_load(array('uid' => 1)); 
  $account = user_load(array('uid' => 1)); 

  if ($user->uid == $account1->uid) { 
    $items['user/%user/classifieds'] = array(
      'title' => 'Meine Kleinanzeigen', 
      'type' => MENU_LOCAL_TASK, 
      'page callback' => 'tpzclassified_user_page', 
      'page arguments' => array(1), 
      'access callback' => 'user_view_access', 
      'access arguments' => array(1), 
      'weight' => 4, 
    ); 
  } 

  return $items; 
} 
A: 

Edit

Based on your comment, the issue is where you're putting this code: hook_menu() is only called when the menu is rebuilt: that's why you're not seeing anything happening.

You can't conditionally add items to the menu system like that: you have to register a menu item, then use the menu item's access callback to determine if the menu will show up for a specific user. The access callback works just like the page callback, but must return TRUE or FALSE. You might do something like this instead:

function tpzclassified_menu() { 
  $items['user/%user/classifieds'] = array( 
    'title' => 'Meine Kleinanzeigen', 
    'type' => MENU_LOCAL_TASK, 
    'page callback' => 'tpzclassified_user_page', 
    'page arguments' => array(1), 
    'access callback' => 'tpzclassified_user_access', 
    'access arguments' => array(1), 
    'weight' => 4, 
  );

  return $items;  
} 

function tpzclassified_user_access($account) {
  global $user; 

  if ($user->uid == $account->uid) {
    return user_view_access($account);
  }
}
Mark Trapp
hmmmm...I tried it in the way you posted it, but it still does not work.
Lars
I used this function:$account1 = user_load(array('uid' => arg(1)));To get the ID from the account. On a normal page it works, but not in the plugin.
Lars
Can you update your question with the entire hook, function, or context in which the code snippet you posted appears? Or at least how the function in which this appears is called?
Mark Trapp
Yep, sure, btw: How do I post code, here?function tpzclassified_menu() {global $user;$account = user_load(array('uid' => 1)); $account = user_load(array('uid' => 1)); if ($user->uid == $account1->uid) { $items['user/%user/classifieds'] = array( 'title' => 'Meine Kleinanzeigen', 'type' => MENU_LOCAL_TASK, 'page callback' => 'tpzclassified_user_page', 'page arguments' => array(1), 'access callback' => 'user_view_access', 'access arguments' => array(1), 'weight' => 4, );} return $items;}
Lars
`hook_menu()` is only called when the menu is rebuilt: that's why you're not seeing anything happening. Check my revised answer for more info and a possible workaround.
Mark Trapp
Now it works. Many many Thanks
Lars
No problem; you can find out how to format code in the [Editing Help](http://stackoverflow.com/editing-help) guide: it's either backticks or four spaces depending on your need. Also consider accepting my answer if it solved your problem. :)
Mark Trapp
+1  A: 

I need a function for a plugin to check, if the shown account is the user's account or that from another one.

What the reported code is doing is to verify if the logged in user is the Drupal super user (aka the user #1). If that is what you really need, then there is no need to call user_load() to load the user object of that user account. It's enough you use the following code:

global $user; 

if ($user->uid == 1) { 
  $items['user/%user/classifieds'] = array(
    'title' => 'Meine Kleinanzeigen', 
    'type' => MENU_LOCAL_TASK, 
    'page callback' => 'tpzclassified_user_page', 
    'page arguments' => array(1), 
    'access callback' => 'user_view_access', 
    'access arguments' => array(1), 
    'weight' => 4, 
  ); 
} 

return $items; 

In Drupal, there aren't two users with the same user ID, and 1 is the user ID for the Drupal super user (which is also called user #1 for the fact its user ID is 1).
The problem with this solution is that since Drupal 6, the menu callbacks are cached; conditionally adding menu callback doesn't have any effect, as the menu cache is cleared only when a new module is installed, or when a module is updated (and update.php is called). The only way to force Drupal to clear the menu cache is to use the following code:

if (!variable_get('menu_rebuild_needed', FALSE)) {
  variable_set('menu_rebuild_needed', TRUE);
}

If you want to check if the current logged in user is accessing his own account you can use the following code:

function tpzclassified_menu() { 
  $items['user/%user/classifieds'] = array( 
    'title' => 'Meine Kleinanzeigen', 
    'type' => MENU_LOCAL_TASK, 
    'page callback' => 'tpzclassified_user_page', 
    'page arguments' => array(1), 
    'access callback' => 'user_view_access', 
    'access arguments' => array(1), 
    'weight' => 4, 
  );

  return $items;  
} 

There is no reason to use a custom function, as user_view_access() already checks if the current user is looking his own account.

function user_view_access($account) {
  return $account && $account->uid &&
    (
      // Always let users view their own profile.
      ($GLOBALS['user']->uid == $account->uid) ||
      // Administrators can view all accounts.
      user_access('administer users') ||
      // The user is not blocked and logged in at least once.
      ($account->access && $account->status && user_access('access user profiles'))
  );
}
kiamlaluno

related questions