How To Insert Node Reference in Content Using Views

| Drupal Development | 2 seen

I this article I'll cover some details on how to programmatically reference nodes in content using Drupal views.

Such behavior I've already implemented in this blog, see for example 83 reasons to Visit Georgia This year article to see it in action. Today I was working on one of my side projects - a laptop buying guide - laptop500.com and decided to implement a new feature - best selling items of the month, by programmatically referencing laptops (nodes) from archive.

Here is how - at start you will need to download and install references module, then visit Content type you would like to add such behavior and under manage fields (admin/structure/types/manage/article/fields) add a new Node Reference field

Node Reference field

Node Reference field

I will set widget to Auto complete text field as later it will be much easier to enter just first letters of title to find and reference nodes.

In the next window (Field Settings) select Content types that can be referenced, articles for example

In the next window, under REFERENCED ITEMS FIELD SETTINGS Number of Values, make sure you select unlimited

Next go and create a new article to whom you just add a new node reference field, you should see new field enabled and you could start referencing nodes

Referencing nodes in Drupal

Referencing nodes in Drupal

Now, after you will save your content all referenced items will be listed unformatted under content area, you could leave this as it is, or you could use manage display section from content type and hide referenced item field, and instead build a new views block to style those items

Referenced items under content

Referenced items under content

I'm going to hide default display and build a new views block. 

Create a new view, add Contextual filter nid, and add Relationships - referenced nodes, then add your fields and make them related to relationship, then just put block in your content area. 

Node reference views display using relationships

Referenced nodes displayed as views block

Referenced nodes displayed as views block

See live demo here: Top 20 Best Selling Laptop / Notebook Computers in April 2017