Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display different content for anonymous and logged in users

What I need to accomplish is this:

  • If an anonymous user visits the site, show regular site content.

  • If a user logs in to the site, then user-related content appears in place of the regular content.

I would like to accomplish this using the Views module.

I have looked at the Premium module, but it seems to be abandoned. I would like to avoid using the content-access module if at all possible, since I already have other access controls in place.

like image 841
Jukebox Avatar asked Apr 27 '10 16:04

Jukebox


1 Answers

If you are creating a page display for the views, you can accomplish this with view's access controls. Views will show the first available display that the user has permissions to.

  1. Create a display for the authenticated user view
    • Set the page path
    • Set the Access restrictions (eg., by Authenticated role)
  2. Create a display for the anonymous user view
    • Set the page path to the same value
    • Optionally restrict access to the Anonymous role (not necessary, since if views can load the authenticated display for the user it will not bother with this one, but may keep it's use clear)

Since you cannot re-order the displays in a view (yet), you must define the views in the order of most restrictive to least restrictive.


For more complex displays, you can use the Panels Pages module to render the page differently based on a user's role.

If you only want to differentiate between anonymous and authenticated users, you can specify that different content blocks are visible to each role.

On my own site, I needed to differentiate between Administrators and everyone else, so I could not use the authenticated user role to define access for individual items. Similar to Views, with Panels Pages you can define multiple variants of pages that use the same path. Administrators have access to the first variant, and all other users fall through to using the second.

like image 96
gapple Avatar answered Oct 10 '22 04:10

gapple