Configuring‌ ‌multilingual‌ ‌content‌ ‌Views‌ ‌in‌ ‌Drupal‌ ‌9‌

20 Sep 2021

When Drupal 8 was released in November 2015, its list of new features was really impressive. Among lots of other things on the list, there were multilingual capabilities and the Views built into the Drupal core. Now that many websites have upgraded to Drupal 9, the Views and multilingual functionalities in Drupal have not changed much. Both remain highly popular among customers, website admins, developers, content editors — here is why:

  • First, almost every Drupal website uses Views to display any kind of content listings that can be set up from the admin dashboard. It is incredibly convenient to have this functionality available out-of-the-box.
  • Second, having a multilingual website opens lots of new business opportunities, so many businesses are either planning to make their websites multilingual or have already done that. Drupal 9 offers a super easy multilingual setup and lots of options here.

What do these two features have in common? Yes, there is often a need to display multilingual content in Views, and there are some subtle nuances related to unwanted content duplication that we are going to discuss in this post. But first, we will quickly run through what Drupal Views and multilingual options have to offer.

Drupal 9 Views: the favorite tool of many developers

Drupal Views are a tool that enables you to list your content (or users, comments, taxonomy, etc.) in any organized collection (grid, list, table, etc.) based on the settings you make. There is a user interface for configuring every smallest detail of how your view should be displayed to users. This eliminates the need for writing SQL queries to the database in order to fetch and output the necessary content — Views do this for you.

Filtering and sorting in Views make it easy to only select content with particular parameters (e.g. only published content) or position content in a specified order (e.g. from most popular to least popular items). You can choose to make your filters exposed to visitors so they decide how they want to sort and/or filter the content in your listings.

Some of the more advanced settings include contextual filtering that enables you to output dynamic values based on the current context and Views relationships that unite data from different base tables.

Your view can be a separate page or a block that can be placed inside your site’s layout. With additional modules installed, you can get a Drupal Views slideshow, a Drupal Views carousel, a calendar, or anything else (see our collection of extra modules to extend Drupal Views). Even without extra modules, there is a relatively new built-in option to share content in the JSON format to third-party apps — Drupal Views REST export.

A glimpse at Drupal 9 multilingual features

Unlike Views, multilingual features are not enabled by default on every website because not all websites need them. After enabling the multilingual module package (Configuration translation, Content translation, Interface translation, and Language), you get all Drupal 9’s multilingual benefits.

You can easily add as many languages as you wish to your website (there is built-in support for almost a hundred languages that can be just selected from the dropdown). Interfaces for them are already community-translated. One of these languages will be your website’s default language at your choice in one click.

You configure which content types and their elements should be translatable. Drupal also provides you with a basic language switcher block that can be placed in the header region or whenever you wish on your website.

Finally, just a few clicks are needed for editors to get a handy interface where they can add translations to content nodes. The admin language for the convenient work of editors with different native languages is also configurable. The translations they create for the nodes are something you may want to properly output in your multilingual view, and we are now moving on to this chapter.

Multilingual content Views in Drupal 9: issues and ideas

Let’s see how to use Drupal Views for translated content. We will create a view with content that has more than one language version. In our example, the website uses English and French, with English as the default language. We have prepared an article in English and translated it into French, so there currently are two language versions of the node.

In Structure > Views > Add view, we create a view that shows content of the Article content type in the format of a list of fields (however, the format does not matter much for this experiment, so you can select any you like).

After hitting “Save and edit,” we arrive at the main Views UI. Our display already has the “Title” field by default, so let’s also add an image field from Article, which we can do under “Fields.”

So what do we see as a result? The view was supposed to display the article title and image. And so it does — but in both languages.