1

I have a block with which I'm creating a menu for users

<?php global $user;?>
  <a href="/user/<?php print $user->uid; ?>/ideabooks">MENU1</a>
  <a href="/user/<?php print $user->uid; ?>">MENU2</a>
  <a href="/user/<?php print $user->uid; ?>/ideas">MENU3</a>

This block will only be visible on user profiles. Now I want to add another link only if the profile owner has a certain role.
So I guess I have to be able to get the node author(profile owner) and then check it's role.

How may I do this?

3

You can use menu_get_object() to easily and cleanly get information on what you looking at, eg, something along these lines:

$mgo=menu_get_object('user');

if ($mgo) { // we are looking at a user

  if (in_array('CERTAIN ROLE', $mgo->roles)) {

    // do whatever

  }

}

and if the user you are looking at, eg, user/###, has a CERTAIN ROLE, then you can do whatever.

Now, from your question, I am going to go out on a limb and assume you are creating this block from the Block UI and using the PHP Filter, so I guess it would look like this:

<?php $mgo=menu_get_object('user') ?>
<?php if ($mgo): ?>
  <?php if (in_array('CERTAIN ROLE', $mgo->roles)): ?>
    <a href="/user/<?php print $mgo->uid; ?>/menu4">MENU4</a>
  <?php endif; ?>
<?php endif; ?>

but I would strongly urge you to consider writing a module for this as the PHP filter opens up soooo many potential holes.

Also, as @olyn has pointed out, $user points to the logged in user, not to the owner of the profile you are looking at, so you will want to look into that in your code as well for your other links, eg, $user->uid is the uid of the currently logged in user; in my code above $mgo->uid is the user id of the profile you are looking at.

  • Thanks, @jimajamma's answer is better than mine, using menu_get_object(). :) – Ollie Feb 22 '14 at 17:01
0

I'm assuming your code goes into a template file for the block. Early on in the template, add the following, to set up the profile owner.

<?php
//get system path components.
$arg = arg();
//check that we're on a user profile
if($arg[0] = 'user' && is_numeric($arg[1])){
  $profile_owner = user_load($arg[1]);
} else {
//block isn't on user profile. set up an empty $profile_owner->roles array
//this prevents "trying to get property of non-object" errors.
  $profile_owner = (object) array('uid' => 0, 'roles' => array());
}
?>

Then, you can use the following to add content depending on the profile owner's roles.

<?php if(in_array('role_name', $profile_owner->roles)): ?>
<a href="" >link text</a>
<?php endif;?> 

To comply with best practices, you might want to consider creating a special permission in a custom module, and checking whether the user has that permission instead, though.

Btw. the code in your snippet will create links to the currently logged-in user's content (and there might not be any), not to the profile owner's. For the latter, use $profile_owner->uid instead :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.