Views relationships
A Views relationship links a second table to a view, which opens up that table's fields, filters and sorts in the same list.
A Views relationship joins another table to the base table of a view. It makes the data of a related item available to the view's fields, filters and sorts. Take a Content view that needs the author's account data: the content table holds only a reference to the author, and a relationship to the user brings in user fields such as User: Name or User: Email.
How relationships work in Drupal 11
When you create a view, you pick its base table, such as Content, Comments or Taxonomy terms. That choice cannot be changed later. Until you add a relationship, you can select fields only from the base table.
The class behind a basic relationship is RelationshipPluginBase. Its documentation calls it a simple relationship handler that allows a new version of the primary table to be linked in, and it can handle a single join. Two options matter in the editor:
- Administrative title. A name for the relationship. The class documentation says a relationship's default label is shown in parentheses next to any field, sort, filter or argument that uses it, so a clear name helps when a view has several relationships.
- Require this relationship. The code sets its default to off. When it is on, the code sets the join type to INNER, and the checkbox explains that it hides items that do not contain the relationship.
Example
The Drupal documentation walks through this case. Open the edit screen of a Content view, click Advanced in the right column and click Add in the Relationships area. Search for author, select the relationship that describes the content author (it is titled User and sits in the Content category) and apply. Then, under Fields, add a field and search for the relationship: the user fields now appear. Select User: Name, change the label to Author and check the preview. The list shows each author's username.
Common mistakes
- Looking for related fields before adding the relationship. Fields of the joined table are not on offer until the relationship exists.
- Switching on Require this relationship by reflex. Items without a related record vanish from the list, so an article with no related author disappears.
- Repeated rows in the result. If identical rows show up, check the Distinct setting under Query settings. Its description says it displays identical items only once, and warns that this does not always work.
- Filtering by something outside the URL without a relationship. The documentation for contextual filters says you may need to create a relationship when the filter is based on something other than the URL.
For marketing and content teams
Ask for a relationship whenever a list should show information that lives on another item: the author's picture next to an article, or the description of the category a product is tagged with. Say which list needs which value and from where, for example "the author's picture in the news list". Also decide whether items without that value should still appear, because that is what the Require this relationship checkbox controls.