Hello, I was trying to find some code I can use but most of them deal with roles condition for users that are viewing pages not roles of a user's profile.

I wish to setup new tab in user profile, but would like to have this tab only on those profiles where profile owner is certain role.

Example:
UserXYZ is assigned with role nr.3. I want every user that is a member of role 3 to have new TAB in their profile called "MyStories" (i will set-up this content with views).

If you open user profile that is not a member of role nr.3, I do not want to display this TAB menu item on his profile.

Any suggestion will be very appreciated.
Thank you.

Comments

nevets’s picture

I have not tried this but you can control access to the view by role (part of basic information for view) and you can make the view available as a tab. So combining the two should get you what you want.

Richard_’s picture

Thank you for your reply nevets, but what you suggest is "who is able to access" the view and that is not what I am after.
I need to show content on user profile only if the owner of the profile is certain role - not the user that is viewing profile...

There is an option in the views where I can filter out content by role, but it doesnt affect constructing of the TAB menu - only the content is empty.

So what i need is new module with menu hook where I put condition/limit to certain role.
Something simmilar to this }but that is for node not for user profile:
http://drupal.org/node/162566

<?php
  $user = user_load(array('uid' => $node->uid));
  foreach ($user->roles as $rid => $role) {
    if ($role == 'authenticated user') {
      drupal_set_message('This is an authenticated user.');
    }
  }
?>

Basically something like this:

IF (profile owner == role3) {
print tab }

Hmm but I am not coder and do not know how excatly should I get these data...

a_c_m’s picture

Íve almost finished a module/plug in for the Bio module. Once set up it automatically assigns the Bio node a taxonomy term based on the owners role.

The module matches the owners role against a preset vocabulary (e.g. Roles vocabulary containing {admin, authenticated user, privileged user} ), when matches are found, they are applied.

With the taxonomy in place, you can use things like Taxonomy Access Control or similar (there may be a module to display menu items based on taxonomy).

Once its published (still one tweak left) i will post a link to it... if your impatient, email me and i will see if i can send you a pre-released copy).

Alternatively you will need to do some query work (or use user_load) based on the UID of the profile.

a_c_m

--
acmconsulting.eu

a_c_m’s picture

Richard_’s picture

Thank you for your reply,

meanwhile I have tried to setup new module with hook for menu tab.
I am not coder and I do not know how to build querry so that i can work with UID and RID (role ide).

I believe that it needs only little (but very important changes). It works without user role conditional.

<?php

/**
* Valid permissions for this module
* @return array An array of valid permissions for the onthisdate module
*/

function nastovar_perm() {
  return array('access onthisdate content');
} // function onthisdate_perm()




/** 
 * Implementation of hook_menu(). 
 * 
 */ 
 
function nastovar_menu($may_cache) { 
  $items = array(); 


 // if ($may_cache ) { 


 

    // Adds tabs to the user page
    if (arg(0) == 'user' && is_numeric(arg(1)) && arg(1) > 0) {
      $account = user_load(array('uid' => arg(1)));

      if ($account ) {
 

/** 
 * ######################################################
 * Querry for user role.  * 
 * ######################################################
 */ 
    $role = arg(1);
	$uid=$view_user->uid;
    $result = db_query('SELECT rid FROM {users_roles} WHERE uid = "$role"');
	while ($users_roles = db_fetch_object($result)) {
	$roles = rid ;
	}
	if ($roles == 3) {

       $items[] = array(
         'path' => 'user/'. arg(1) .'/nastovar',
         'title' => t('Tovar'),
         'description' => t('zoznam pridaneho tovaru'),
//         'callback' => '',
//         'callback arguments' => array($account),
         'access' => TRUE,
         'type' => MENU_LOCAL_TASK);
	}     	
      }
//    }  

  }

  return $items; 
} 

 
 
  

Thank you for any hints.

nevets’s picture

I would change the test for role like this (goes inside if ( $account ) {)

$desired_rid = 3;
if ( array_key_exists($desired_rid, $user->roles) ) {
  $items[] = array(
       ...
   ;
}
Richard_’s picture

Thank you for your help nevets. I have got one error: "The second argument should be either an array or an object"

So I made it an array (at least I think so).
But anyway, this doesnt seem to work :( I have leaved out querry for role, because I do not know wheter it was done correctly ...

See what I have after your suggeston:

<?php

/**
* Valid permissions for this module
* @return array An array of valid permissions for the onthisdate module
*/

function nastovar_perm() {
  return array('access nastovar content');
} 



/** 
 * Implementation of hook_menu(). 
 * 
 */ 
 
function nastovar_menu($may_cache) { 
  $items = array(); 


    // Adds tabs to the user page
    if (arg(0) == 'user' && is_numeric(arg(1)) && arg(1) > 0) {
      $account = user_load(array('uid' => arg(1)));

	     if ($account ) {
		     $desired_rid = 3;
			   if ( array_key_exists($desired_rid, array($user->roles)) ) {
		          $items[] = array(
        		 'path' => 'user/'. arg(1) .'/nastovar',
		         'title' => t('Tovar'),
		         'description' => t('zoznam pridaneho tovaru'),
//		         'callback' => '',
//      		 'callback arguments' => array($account),
		         'access' => TRUE,
        		 'type' => MENU_LOCAL_TASK);
	           }     	
             }
    }

  return $items; 
} 

nevets’s picture

Opps, typo on my part, array_key_exists($desired_rid, array($user->roles)) should have been array_key_exists($desired_rid, array($account->roles))

Richard_’s picture

Again, thank you for your effort nevets.
Unfortunatelly your suggestion didnt work, but you gave me some direction into my thinking and I have figured it out somehow and it works perfectly.

Here is the final code:

<?php

/**
* Valid permissions for this module
* @return array An array of valid permissions for the onthisdate module
*/

function nastovar_perm() {
  return array('access nastovar content');
} 



/** 
 * Implementation of hook_menu(). 
 * 
 */ 
 
function nastovar_menu($may_cache) { 
  $items = array(); 


    // Adds tabs to the user page
    if (arg(0) == 'user' && is_numeric(arg(1)) && arg(1) > 0) {
      $account = user_load(array('uid' => arg(1)));

	     if ($account ) {
			  foreach ($account->roles as $rid => $rolename) {
			    if ($rolename == 'obchodnik') {
		          $items[] = array(
        		 'path' => 'user/'. arg(1) .'/nastovar',
		         'title' => t('Tovar'),
		         'description' => t('zoznam pridaneho tovaru'),
//		         'callback' => '',
//      		 'callback arguments' => array($account),
		         'access' => TRUE,
        		 'type' => MENU_LOCAL_TASK);
	           }     	
             }
    }

  return $items; 
} 
}
 
fehin’s picture

Hi Richard, did you try this with nested tabs. I have three views that I would like to add as secondary tabs and only show them on profile of members in a certain role. Can you help please. Thanks