Hello,

I am new to Drupal and intend to present certain custom pages to certain user roles. I already read How Do I ... hide certain menu's from users in certain role's and Show block to certain users only but can not reach my goal.

I created menu items linked to my pages. Ok with that.
I created blocks using the following code to display them according to a_certain_role of connected user:

global $user;
$output = '';
if (in_array('a_certain_role',$user->roles))
{
  $output .= 'YOUR OUTPUT GOES HERE';
}
return $output;

It works but what do I put in place of 'YOUR OUTPUT GOES HERE'? I just want to see my menu items there...
How do I put my menu items in my custom block?

I use Drupal 4.6.3. Thanks in advance for your answer.

Comments

behindthepage’s picture

//Restrict a block to a set of roles
global $user;
$roles = $user->roles;
$approved_roles = array('authenticated user');

if (count(array_intersect($roles, $approved_roles)) > 0) {

//Print menu No. 80
         $sna = module_invoke('menu', 'block', 'view', 80);
         print $sna['content'];
}

OR For Multiple roles use
$approved_roles = array('authenticated user,story editor,authors');

to Find Menu No. go to menus and hover cursor over "edit" of the menu you want to include in the status bar of your browser you will see something like

http://yourdomain/?q=admin/menu/item/edit/80

the number on the end is the menu number

All the best
gpdinoz
"If we can see further it is because we stand on the shoulders of giants"

Regards
Geoff

dams-1’s picture

Thanks for your hint. It works well for the block, but in the same time the menu itself still displays: I have the menu items displayed in the block and in the menu. These items are the same by principle here.
How do you let these menu items appear only in the user-role-controlled block?

behindthepage’s picture

When you say "Menu" I assume you mean the "Navigation" Block Menu that is the default. You can turn it off by going to blocks and unchecking the "enabled" checkbox for "Navigation".

gpdinoz
"If we can see further it is because we stand on the shoulders of giants"

Regards
Geoff

baburajpv’s picture

Hello

Got the above code. I am new to drupal. but I do not know where to put the code to disable the menu for a particular user role.

It would be great if you could get back to me with the answer.

with regards,
Baburaj

behindthepage’s picture

Create a custom block in admin/blocks an put this code in the block.

gpdinoz
"If we can see further it is because we stand on the shoulders of giants"

Regards
Geoff

sangamreddi’s picture

dams-1’s picture

Hello,

After having tried many things, I realized I have to develop my own module. I made the tutorial. This way gives a solution to all the technical questions I had with Drupal to reach my goal.
Thank you for your answers. They contribute to teach me Drupal philosophy too.

dams