I have a list of items visible for all anonymous user and authenticated user. Then when the user click through, if the user is anonymous, I want to tell them, "sorry must be authenticated". But if they are already authenticated I want to show them the node.

I am using:

if($logged_in) {
   //display node 

In my node--mycontenttype.tpl.php. How do I make this work?

  • 1
    This is wrong because: 1. Don't put this kind of logic in templates. 2. You don't really describe what you're trying to do. 3. Don't reinvent the wheel: drupal.org/project/nodeaccess does what you want already I think.
    – beth
    Commented Oct 15, 2013 at 16:03
  • I can not use nodeacces and content_acces, Because I shows the list of items to anonymous users
    – Jansel
    Commented Oct 15, 2013 at 16:27
  • Please clarify, then, with some background about what you're attempting to do.
    – beth
    Commented Oct 15, 2013 at 16:31
  • You might be able to reuse some code from github.com/bbinkovitz/foreshadow_menu which is a little proto-module that allows a menu to display items the user lacks permissions for as non-linked text, but only links to them if the user has permission.
    – beth
    Commented Oct 15, 2013 at 16:34
  • You wouldn't put that in a template though, for many reasons. That would go in a module that defines or alters the list that you show to anonymous users.
    – beth
    Commented Oct 15, 2013 at 16:35

2 Answers 2


Showing different content on the same page to user depending on role could be accomplished with the Panels module like so:

  1. Install the Panels module and its dependencies.
  2. Go to admin » structure » panels.
  3. Enable and edit the page "node_view" template.
  4. Click "add new variant".
  5. Click "continue" on each screen until there is a button with "Add variant." Click "Add variant".
  6. Under panel » content, click the gear in the upper left to add a panel of content for the logged in user to see. Save.
  7. Click the gear in the upper right of the new content to get a menu. Select visibility rules » add new rule. Select role and set it how you want. Save.
  8. If you want to show something different to anonymous users, repeat step 6 and 7 to add content and make it visible only to anonymous users.
  9. Don't forget to save.

Or you can use the Context Module. Its better for developers, I think panels is better for users.

  • Can you explain how one would use Context module for this? I looked at the UI and it was not obvious how that would work.
    – beth
    Commented Oct 15, 2013 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.