Need some help with your project? Contact me

How to hide from anonymous users content tagged with certain taxonomy terms

This tutorial will show you how restrict content from being displayed to from users based on the taxonomy terms the content has assigned. You can do this using Taxonomy and the Context module. It is a good use case for websites which post content that is both premium and free and need to have flexible ways of managing these characteristics.

Let’s imagine an example: You write content and some of it is free for reading but other pieces of content are premium (users need to pay/log in to the website to read them).

One way of implementing this is to have 2 Content Types (CT) with permissions for reading assigned to various roles (for example: anonymous users cannot read whereas authenticated users can). One problem that could however occur from this is that if you want to modify the layout on which this content is displayed, you’d have to do it twice: once for the CT for premium users and once for the CT for anonymous users (free). Or if you have other CTs you want to split into premium and free, you’d have to create duplicates. Most importantly however, it becomes a real drag if you want to switch between Premium and Free for a particular node. It is a rigid way of handling things, especially if you are uncertain about what will be free and what will not be free in the future.

A flexible and easy solution for these problems involves using Taxonomy (Drupal 7 core) and the Context module. You can essentially use the Context module to hide the content of nodes displaying content tagged with certain tags.

You can do this quite fast too:

Step 1

First, go to your Taxonomy vocabulary of choice (such as Tags) and add your ‘premium’ tag (the one that will identify the content hidden from anonymous users).

Next, to whatever Content Type you would like to differentiate between premium and free content, go to Manage Fields and add a new field of the type: Term Reference. You can choose a widget there; my personal favorite is autocomplete.

Next - and this is optional but indicated for this example - create a new block which contains a message that indicates that the content requested is premium, and possibly contains a link to where the users can log in or register for an account in order to access the premium content. Do not add this block to any regions yet.

Step 2

After you have done this, you can go to Structure - Context and create a new Context. Add a unique name, and check the box Require all conditions (since there are more than one condition that have to be met before the Context reactions are triggered).

Then you can add the first condition: User roleAnonymous (this means the context is in place only when the user is not logged in). For the second condition, add Taxonomy Term – and locate and select your premium tag (in my case it is Premium under Tags).

Next, you can add your reactions. First, add RegionsDisable region in [your_theme_name] (for me this is Bartik) and check to disable the Content region. The name of this region can differ from theme to theme, but it will usually be Content or a variation of this word. If uncertain, go to Structure – Blocks and check which region holds the main content of the node.

The second reaction you should then add is Blocks. Select the block you created earlier and add it to another prominent region. In Bartik for instance, there is the region called Highlighted which is placed just above Content (the region you just disabled for this case).

Save the newly created Context and you can go and test. Create new content, tag it Premium and then check to see if you can view it both as Anonymous and as Admin. You should see it if you are logged in, but you should not see it if you are logged out – instead, you should see the block redirecting you to the log in page.

Tip...

Word of advice: add a new region to your page template for this particular purpose: to add this message block to. The best place to put it in my opinion is right below the main content region. This way, when the premium content node is loaded, the message block will appear just below the title. You can also read this post on how to add new regions to your page template.

Hope this helps.

Comments

It helps me terribly :)
Thanx for this tuto

Add new comment

You must have Javascript enabled to use this form.