By pjdevries on
In an attempt to implement some kind of role based visibility of menu items in Drupal 4.6, I have modified menu.module. The "edit menu item" form now shows a list of access roles with checkboxes, which enables me to select the roles I want a menu item to be visible for. The challenge now is to implement a filter in the right place of the Drupal core. I assume I must modify menu.inc but I am not sure which function to modify.
Can anybody give me a bit of advice?
Comments
Different approach, same solution
Hi, I was working on a solution to solve a similar issue.
I was trying to understand the block.module and the menu.module and I looked at a lot of PHP snippets.
But, after a while someone pointed out to me a different approach would come in much better.
This is what I did:
First I installed the following modules:
- Menu on the Fly (menu_otf)
- Node_privacy_byrole
This gives you the abilities to set access rights for nodes and to create a new menu-link for a node upon creating the node.
Then I used a patched block.module which gives you the ability to hide/show blocks for certain roles.
Patched block.module: http://drupal.org/node/49154
So you just create a new menu, then make the menu visible (administer->blocks) and then you set the access rights for the block.
Now when you create a new node, you set the access rights immediately with node_privacy_byrole and insert a new menu-item in the correct menu/block with menu_otf.
Works fine for me on Drupal 4.6.x but it should also work similar on 4.7
Greets, Flow
Role based visibility of menu items
Thanks for the response Floris. Always nice to get feed back from a fellow countryman.
I solved the problem of hiding a menu blocks as a whole in a similar way though with a different patch. The patch I used gives me functionality comparable to the new functionality in Drupal 4.7. Apart from role based block visibility, It allows you to do all kind of validation tricks, by inserting a snippet of PHP code in the 'Page specific visibility settings' of a block. Unfortunately I didn't keep track of where I obtained the patch.
Neither of those solutions solves the problem of conditonally visible menu items though. Although it prevents the underlying page of a menu item from showing up when an unauthorised visitor selects the menu item, it doesn't prevent the menu item itself from showing up in the menu, which is what I want.
For the the time being I applied a filter (based on my modifications of the menu.module) to the menu_tree() function in menu.inc. It works like a charm but I do prefer a solution like yours though, because it prevents messing with the Drupal core. So if you have any additional suggestions how to hide the actual menu items, I'm anxious to hear them.
Menu_access
The other solution I was first working on was like this:
Create a new table 'menu_access' with your SQL-manager with the following columns:
- mid (menu-item id)
- rid (role id)
- grant_view
- grant_update
- grant_delete
Then you should implement something in the form where you create a new story or page with which you can set the menu access permissions. This is almost the same as node_access (like the node_permission_byrole module does).
You can also use such a method within the menu_otf module.
Then you can implement a new module using hook_block() or edit the menu.module.
You should write something that checks the role of the global $user with the permitted roles upon creating the menu (inside a block). Take a look at the PHP code snippets over here: http://drupal.org/node/21867
Also you can take a look at my other forumtopic over here: http://drupal.org/node/44259
I didn't work out this idea, because I don't understand Drupal well enough and thought the solution I have now almost does the same.
Het doel heiligt de middelen. ;-)
Post your fix ?
I have the same problem. I wonder if you'd mind posting your menu.module (and whatever other changes you might have made) here ?
Thanks!
Which fix?
Which fix? FlowDesign's or mine? Beware: my fix is my first Drupal customization attempt!
Your fix
I looked at the menu.inc and menu.module code to try to reproduce your customization. But I've been looking at Drupal for only about a week (and so far am pleasantly impressed), and as of now, I have no clue about hacking up the core. Like you, I would prefer a new module to messing with the core distribution. But any solution is better than none :-)
So if your modification works, I'd be very happy to use that (since I need the solution). Thanks!
My fix
Sorry for the delay. I'm struggeling with an eye injury and couldn't use my computer for a couple of days.
Apparently it's not possible to add an attachment, so if you drop me a line, I'll send you my fix per e-mail. B.t.w.: the fix is applied to the standard Drupal 4.6.5 menu module and include file.
Maybe this can be
Maybe this can be useful:
http://drupal.org/node/227418