Drupal 9: Setting Up Multilingual Content Views

Drupal Views is a powerful module. The ability to generate lists of things in Drupal means that it is used everywhere. It even has a powerful plugin system that allows other modules to interface with it and create more functionality.

With multilingual sites there is a common issue I come across quite a lot that is quite easy to fix. A View will be created (usually via the Views wizard) that is intended to list a type of content. When an item of content is translated the content is duplicated in the View and you end up with a single list containing both translations of a single content item.

Let's take an example of this on a site with English and Welsh as the two languages present. I set up a standard View that shows articles content types in a list and then generated a few items of content using devel generate. After translating one of the articles the list looks like the image below with the first article in the list being duplicated. Since the original version was in latin (thanks to devel generate) I have just added the name of the language to the start translation title to show the new content.

Views content translation, showing duplicate content in an article list.

This is clearly a problem as it creates lists of content containing duplicated items.

Here is the administration for this article listing View. As I said there is nothing wrong with how this is set up as it was done through the Views wizard, it is just that it doesn't support multilingual content.

Views content translation, showing the views admin interface.

Implementing The Fix

How do we add multilingual support for this kind of View? As it happens, adding multilingual support to a View is pretty simple.

The first part that is needed is to add the Default translation filter. This can be found in the filters list once you click "Add" in the Views admin interface.

Views content translation, adding the default translation field.

The filter settings are pretty simple, you just need to add the filter with the default translation set to "True". This will ensure that the content items are filtered by the default language present in the content item.

Views content translation, setting up the default translation field.

The second part is to set a different rendering language. Click on the rendering language option in the middle column and select "Interface text language selected for page" from the list of items.

Views content translation, setting the rendering language.

The finished View should now look like this, with the new filter in place and the rendering language set correctly.

Views content translation, showing the views admin interface in the correct state.

Now, when we look at the articles list in the English language we see that the duplicate items are no longer present.

Views content translation, the finished English view.

Swapping over to the Welsh version of the page we see the newly translated item in the list of articles without the other variant.

Views content translation, the finished Welsh view.

Using this simple change we now have a View that will show a list of content items that will fall back to the default language if no translation is present. This is useful as it allows content lists to be created and translated after the fact, without leaving gaps in the lists.

One thing to note is that you may occasionally still have duplicates if you include taxonomy terms in the View. If this happens you can solve it with adding distinct, or by using aggregation.

If you have had problems with duplication in multilingual Views then I hope this article helped you out.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
9 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.