How To Reference Nodes in Drupal 8 + Views Relationships Display

| Drupal Development | 6 seen

Lately I feel very enthusiastic working with latest Drupal 8 release, though Drupal 8 have been around for a while already, there are still few drawbacks which keeps me avoiding developing full scale on Drupal 8.

I will write down my notes of pros and cons about switching from Drupal 7 to Drupal 8 in some of the future posts, in today's Drupal tutorial series I will provide information how to use reference fields and build something awesome, as usual I will use a live example. We are going to build a views display, listing tours in a blog article.

You can see my previous simple tutorial on the same functionality for Drupal 7 site here: Node reference views display using relationships

In fact if you are familiar with relationship fields and node reference fields in Drupal 7, there is basically nothing new in Drupal 7, with exception that this functionality is already built out of box in Drupal 8, you don't need to download and install any modules

Here is what the final result looks

Referenced node view

Referenced node view

You can see a live preview here: John Graham Tours joins Tourgeorgia

Add a reference field to content type

In Drupal 8, node references are already in core, you don't need to install and enable any new modules. In this tutorial I'm using default article content type to add reference field:

Under your content type manage fields settings, click to add a new field

  • Select Reference -> Content
  • Give it a nice label, for example Tour listings
  • Save 

On the next page choose content types that can be referenced and select display value to unlimitedReference Type Field settings

Reference Type Field settings

As this reference field is attached to article content type, I will reference tours, so I select tour content type.

Save settings and go to content creation page (or edit existing content), you should see this newly created field there - add few referenced nodes and save your node.

You will see ugly formatted referenced items under your content, now go to your content type manage display settings and hide this field, as we are going to display it nicely using block built by views.

Drupal 8 views for referenced field

  • Create a new view, make sure your are enabling block display
  • Under Advanced, add Contextual Filter: ID (provide default value)
  • then from Relationships add referenced field:content

Now you are good adding fields and styling your view. Once ready save your view, go to block admin display and place this newly created block in content section.

Hope it helps.