I want to display different navigation links and data to a user depending upon their role.
What module(s) could I use to achieve this?
Use hook_menu_alter()
/*
* Implementation of hook_menu_alter()
*/
function MYMODULE_menu_alter(&$items)
{
$items['your/path']['access callback'] = _custom_access_callback_for_this_page();
}
And inside the access callback function (here: _custom_access_callback_for_this_page), write whatever the validation you want.
Don't forget to clear the cache after any changes inside your `hook_menu_alter() implementation.
Hope this helps.
I really like this module for Drupal 6, you should check it out:
Menu per Role
Related
On my website, The user can add the content and edit the content. On the edit page, there is one delete button to delete the content but I want to use that button to just hide/unpublish the content from the user and the public.
I have tried the below code but it is deleting the content.
function test_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
$nid = $entity->id();
$node =Node::load($nid);
$node->setPublished(FALSE);
$node->save();
}
You can try to overwrite action button using hook_form_alter().
$form['actions']['delete']
Instead standard action use your own.
Firstly, do not allow users to delete nodes - by the proper
permissions settings.
Secondly add Publish/Unpublish button or just
show users Publish checkbox on edit form and instruct them how to
use it and how it works.
Maybe combination of modules can be helpful:
https://www.drupal.org/project/publishcontent
https://www.drupal.org/project/view_unpublished
https://www.drupal.org/project/override_node_options
I am developing a small system using Symfony2, and I have reached a situation that I need to duplicate the same editAction, but only change the view it renders.
I use this action to edit basic information of all the registered budgets that is listed in a page. I have a special page listing all the inactive budgets, and I want a different editing page to change some statuses and add dates.
How can I use the same editAction to render different views depending on the URLs? The page that lists all the budgets is /budgets and the inactive ones is /budgets/inactive.
Use something like:
/**
* #Route("/budgets/{active}", defaults={"active" = "active"})
*/
public function editAction($active)
{
// ...
}
Then, when you go to /budgets $active will be "active" and when you go to /budgets/inactive $active will be "inactive". You can then use this variable to decide which template to show
More info here:
http://symfony.com/doc/current/book/routing.html#required-and-optional-placeholders
Nice answer by Carlos, thanks, but an edit action requires also validation, and after that the same page with errors or some kind of action on a valid form. So i would say that even a simple form is a bit complex. When you also make a "two views" action it gets 2 * 2 = 4 times more complex. besides of that the $active variable could have any value and not only "active" or as you want "inactive". It could be "joke", "stupid" and a lot more possibilities. My suggestion would be to add a second action and to use private functions for functionality that fits for both forms OR to use a custum class (or service) that handles the forms.
I need to change the menu links on my website (and leave the same items names) depending on the user is a guest, or authenticated user.
What's the standard way to do it ?
thanks
You cannot dynamically change a menu item's path, because menu items are cached.
Still, AFAIK, there are two ways to get what you want. Both methods require you to create your menu items with hook_menu in a custom module (not from the Menu UI).
The first method is to create two menu items with identical names and set the access rules so that one is only available for logged guests, the other for authenticated users. Since Drupal will only show menu items that the user is allowed to access, only one will show up at any given moment. In Drupal core, you can see how the user module creates a menu item for anonymous users by looking at the /user/login path in user_menu().
The second method is to create a single menu item and check in the menu callback if the user is logged in. If the user is logged in, you serve one page, if not you serve another. In Drupal core, the /user path works like this. See user_page to see how the code works.
You can dynamically change a menu item's path - see hook_translated_menu_link_alter.
This hook is called before every menu item is rendered IF it has the property ['options']['alter'] = TRUE.
You can set this property to menu items using hook_menu_link_alter.
Example code would be:
function MY_MODULE_menu_link_alter(&$item) {
$item['options']['alter'] = TRUE;
}
function MY_MODULE_translated_menu_link_alter(&$item, $map) {
if($item['mlid']==89) {
$item['link_path'] .= 'my-new-path';
}
}
Instead of altering the link, you coudl create the menues twice: once with the links for regular users and once with the links for registered/admin/... users
You can put a menu into a block and set it to only allow registered users to see the one block and non-registered users the other block. Either by selecting the proper radio button from the Drupal menu within the block creation form or via PHP that will evaluate and depending on it's return value (TRUE/FALSE) displays it. I suggest to go with the first approach.
You can change the menu by using a combination of nodeaccess module and linking to the corresponding pages.
For example, by default guest users cannot access /logout. If you create a link in a menu to logout, it will only display if a user is logged in. With nodeaccess, simply create a node, visit the grant tab and uncheck/check "authenticated users" or "anonymous users" for that node.
http://drupal.org/project/nodeaccess
Cheers,
A user might be in role X.
There exist a view, where display A is allowed for role X while display B is restricted.
How do i programmatically check whether a user belonging to role X can access the display or not?
What you should do, is to check the permission instead of the role using: user_access
Is there a specific reason why you want to do this programmatically? You can set access rules for Views displays in the Views UI:
Edit the view, select the display and look for "Access" in the "Basic settings" block. Click the value (default = "Untrestricted"), click the "Override" button to override the setting for that specific display and choose the settings you need.
Can be implemented inline in the theme, but better to break it up into module + theme. (assumes drupal-7) In your theme (node--contenttype.tpl.php) invoke a custom access method:
if (module_invoke('hottopicresearch', 'display_moderated_research_access_callback', 'update', $node)) {
Implement an this access callback in a module:
function hottopicsresearch_display_moderated_research_access_callback($permission, $node) {
And check roles
if (in_array("editorial board admin", $user->roles) || $user->uid == 1) {
and/or node access as noted in other answers:
if (!node_access($permission, $research_parent_node)) {
returning TRUE or FALSE.
This example gave access to people with 'editorial board admin' role and people who can write to the node. Nobody else can see the index. Of course this doesn't stop them accessing the node directly.
I'm using Drupal 6.16: I think I have a pretty simple question. How can I get the current user id and put it in a menu tab. What I would like to happen is when the user logs in and wants to change their name, email etc to click a menu tab. I image it would look something like this: http://domain.com/user/{userid}/edit
Thanks in advance!
msindle
That's more difficult than you would think, because menu items are cached. There is not a straightforward way to create dynamic menu items with the userID in it.
What you can do, is write a custom module and imitate the behavior of the 'user' path. With an implementation of hook_menu you create a menu item, with the path 'user/edit' (just like user_menu() creates $items['user']). Next you create a menu callback user_edit_page(), similar to user_page(), which gets the id of the current user and returns the user edit page:
function user_edit_page() {
global $user;
if ($user->uid) {
menu_set_active_item('user/'. $user->uid .'/edit');
return menu_execute_active_handler();
}
else {
return drupal_get_form('user_login');
}
}