Need some help with your project? Contact me

Taxonomy terms from the same vocabulary filtered and referenced on multiple content types

In this tutorial I am going to show you how to reference one taxonomy vocabulary on multiple content types with the possibility to restrict the availability of the terms per content type. What do I mean? Well, you’ll be able to have some terms in the vocabulary available in a select list of content type A and other terms of the same vocabulary in a select list for the content type B. Why not use two different vocabularies for this? Because you may need some terms available for referencing in both content types.

To do this you’ll need to have the Entity Reference module installed. So get that out of the way quickly with drush dl entityreference -y and drush en entityreference -y if you haven't already. So let’s get started.

First up, we need the taxonomy set up. I have my Tags vocabulary containing the following terms:

You’ll notice that these terms could be more or less grouped into two categories: Transport and Sleeping. One term (couchette) kinda fits both criteria and is there to exemplify the reason for not using 2 different vocabularies to handle the issue. Now we need to technically make the distinction between these terms as well and since they are entities, we can use a field for that.

So we can now go ahead and add a new field to this vocabulary of the type List (text) with whatever widget you’d like (and let’s call the field Type). You should make the allowed number of values unlimited and put the following in the allowed values list:

Edit each term and select the appropriate type. As expected, for the term chouchette, you should select both options.

Now that we have our taxonomy squared away, it’s time to create a View that will handle the filtering of these terms. Because if we now add the Tags taxonomy term reference field to our content types in the normal fashion (as it is already present in the Article content type) we’ll be able to select all the terms in the vocabulary for all the content types. And that's not what we want now.

So create a View that shows taxonomy terms of the Tags vocabulary - let’s call it Tags Filter. Add 2 displays of the type Entity Reference and a filter of the field we added to our terms earlier. So for instance the first display can show the terms which have Sleeping selected and the other one can show terms which have Transport. Save the View and you can close it.

Now finally we can edit our content types and add to each a new field of the type Entity Reference with a select list widget. For the target type select Taxonomy term and under Entity Selection choose the following: for Mode, go with Views: filter by entity reference view and then select the View in question - one of the two displays which we created earlier. In the picture below, I named one of the displays Tags for Articles:

Repeat this step for the other content type where you will select the other View display.

Now when you add a new node of the first type, you’ll have a select list containing only some of the terms in the vocabulary. When you add a node of the other type, you’ll have the other terms available. And, you’ll have couchette available for both.

Why not use 2 vocabularies? Well there are obviously cases in which you can and you should because it’s simpler. But that’s when you know the content types are very different from each other and you don’t have one term that categorizes 2 nodes of these different content types. But anyhow, this technique is not limited to taxonomy. With the Entity Reference module you can reproduce the same thing with referencing content instead of terms - or any other entity.

Hope this helps.

Add new comment

You must have Javascript enabled to use this form.