The Drupal 8 core Views module is a big part of why Drupal 8 websites work so well. It takes advantage of Drupal’s structure to create features from recommended content to directories and search pages. However, you can quickly run into complications when implementing a view, especially if your website is multilingual. So get ready to learn how to display multilingual Drupal 8 views of entities (such as nodes, media, and taxonomy terms) as well as entities indexed through the Drupal 8 Search API module.
A multilingual Drupal 8 website with these guidelines:
- the default language is English
- users can choose to view the site in French or Spanish regardless of available translations
- if an entity does not have the requested French or Spanish translation, then display the English version
What could go wrong with multilingual Drupal 8 views? It starts with how views get data and how translations are stored in Drupal. The database contains separate rows for each translation of an entity, so when the view queries it without specifying a language, it can return duplicates of the same entity. However, if you filter by the page’s interface language, the database will only return entities translated in the page’s language. Additionally, if this is a view of indexed entities, then the language rendering options which fix the above issues are not available. Because of this discrepancy, regular entity views and indexed entity views need different solutions.
Regular entity views
If you are unfamiliar with adding a filter to a view, check out these instructions from drupal.org.
1. In the view edit page, find the FILTER CRITERIA section on the left side under the FIELDS section. Click ADD to get the FILTER CRITERIA pop-up.
2. Next, select Translation Language and click APPLY.
3. Then, in the next pop-up, select Site’s default language, and click APPLY again.
4. Back on the view edit page in the LANGUAGE section, click on the setting underneath Rendering Language.
5. In the pop-up, change the select box to Interface text language selected for page and click APPLY.
6. Save your view!
This means that all the entities in your view will display in the requested language, but default to your default language so nothing gets left out. This filter also removes any duplicates, because the language is held constant during the query.
Keep in mind that if any entities in your view do not have a version in the default language, they will not show up, even if the page is set to the language they do have. This was not an issue for the example site as all the content started with English and was later translated, but this could create problems if your content is originating from multiple languages.
Indexed entity views
This was done using version 8.x-3.8 of the Search API Solr module. Since Search API Multilingual Solr Search was not merged into this module until 8.x-2.x, older versions of Search API Solr may not have this functionality (namely the “Language (with Fallback)” field which was added to fix this problem).
1. In the admin menu, go to Configuration > Search and metadata > Search API.
2. Edit the search index your view is pulling from and go to the Fields tab.
3. Click ADD FIELDS.
4. In this pop-up under Global fields, click ADD next to Language (with Fallback).
5. Save this configuration and re-index.
6. In your view’s filter criteria, add the Language (with Fallback) field and set it to Interface text language selected for page.
Using multiple languages complicates everything in Drupal, but the Drupal community is always developing patches, updates, and modules to make it easier. If you have a multilingual Drupal website in need of custom solutions, see our contact page to get in touch!