Question

The Drupal 6 menu module hides links to content that the current user does not have permission to access. This makes sense, but there are times when you may want links to be shown and go to a 'you are not authorized to view this content' page (e.g. for marketing purposes).

Is there an easy way to get these links to show? I understand that previous versions of Drupal actually required you to download a module to HIDE the links to unauthorized pages!

Was it helpful?

Solution 7

I've found a module that actually seems to provide the functionality I want:

http://blog.davereid.net/content/restricted-content-yet-another-different-drupal-node-access-module

It's really simple, it just adds a selector within each node creation page where you can choose what roles will have access to it. Any that don't will still see the menu link, and this will lead to a custom 'you must register to view this' page. I've installed it on a test Drupal site and it seems to work fine; shame I didn't find it a couple of weeks ago!

OTHER TIPS

It has always been that way (at least since 4.7).

You cannot unhide unauthorized menu items out of the box. There might be a special module though...

What you could do, is to let the menu page point to a function that redirects to the page that you want your menu to point to. This is not ideal in many ways but since all can access the function that does the redirect this should work. You canjust put this code into a custom module you make.

There is no way to do this in the default Drupal, and I'm not aware of any contributed modules that provides this functionality.

Two solutions come come to mind to do this easily.

  1. With minimal coding is to hide the real menu item and make it invisible, then make a page with some PHP code that redirects to the proper menu item if the user has access, and showing some other text for people who don't. Then just make a menu item that points to your node.

  2. Involves more coding, but it will probably be more robust. Just make your own basic module with a hook_menu that has the opposite access check to the real menu item along with a small menu callback function with the text you want to show non-privileged users.

If you want to do this for a lot of menu items some more flexible solution should be found to manage it properly.

assuming that every content is a node you can keep menus without access rules and then use

hook_view($node, $teaser = FALSE, $page = FALSE)

you can refuse access to certain nodes ..

hope that's helpful.

Eventually I did it a slightly different way, by creating a taxonomy for "open" or "restricted" content, and put this in the header of my theme:

<?php

global $user;

//if the user is not logged in, and the node is in taxonomy term "restricted"
//redirect them to the login page
foreach ($node->taxonomy as $tax){
  if ($tax->name == "Restricted" && !$user->uid){
    $query = 'destination=node/'.$node->nid;
    drupal_goto('please-login-continue',$query);  
  }
}
?>

/please-login is a page I created which contains all the usual login/signup forms. It seems to work fine for me so far!

Note: If people deliberately disable redirection in their browser they won't be redirected, however when I tried in Opera with redirection turned off the page loaded up to the point where the redirection was trying to happen, and then nothing after that - so the content was still restricted in this case.

Kind of late but this is how i worked this out:

/**
 * Implementation of hook_menu()
 */
function mr_gm_menu() {

    $items = array();

    $items['x'] = array(
        'title' => 'X',        
        'page callback' => 'x_view',
        'access arguments' => array('access content'), 
        'type' => MENU_NORMAL_ITEM,
        'menu_name' => 'main-menu',
        'weight' => 10,
    );  

    return $items;
}

function x_view(){

  global $user;

  if ($user->uid):
    drupal_goto('node/add/X');
  else:
    drupal_goto('user');    
  endif;

  return;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top