Relating nodes to each other in Drupal can be done more than one way. I find it the easiest way using Views and the References module. As for this example I assume you have installed both these modules. Here we go:

Related nodes in a Block with Drupal 7

28/10/2015 - 19:10

Let's say we have a band and we want to relate songs to this band. We start by creating a content type called 'Band'.

  1. Add a Content type and give it a name, in this case it will 'Song'. All other options are not mandatory but you can set them as you like. Click on 'Save'.
  2. Add another Content type, the same way as above, which in this example will be the 'Band' content type. Click on 'Save and add fields'.
  3. You can add any fields you like but you HAVE to add a 'Node Reference' field. Below you can see I've added a 'Related Song' field which is 'Node Reference' field. You can choose whatever you like for 'the form element to edit data'. I tend to use the 'Autocomplete text field' when there are more than ten nodes to be referenced to.
    Create a new references field.
  4. A screen will popup asking you which Nodes of which type you want to reference. In this case it will be the 'Songs' content type.
  5. In the following screen you can set some extra options. All options can be set as you like. Do take note of the 'Number of Values' setting. We will set this to 'Unlimited' as a 'Band' can have an unlimited amount of 'Songs'. Click below on 'Save Settings'.
    Set the number of values to unlimited.
  6. Now let's create some 'Songs' for our 'Band' by adding nodes via the 'Add Content' button. Done? Great let's move on to the next step.
  7. Let's create the band for which we have just added these songs. Go to 'Add Content' and choose 'Band'. Fill in the Band Name and (if you like) a description.
  8. As you can see there is a field 'Songs' below the 'Description' field. Here you can type in the songs which you have already created in step 6.
    Add songs.
  9. Click on 'Save' and as you will see the songs are now mentioned on the 'Band' node. But in most cases you want more control about how you display these related nodes. For this we are going to create Views.
  10. First off we need to remove the 'Songs' from the display on the 'Band' node. Go to 'Structure > Content Types' and click on 'Manage Display' for the 'Band' content type.
  11. For the 'Songs' field choose 'Hidden' as a Format as shown below and click 'Save'. You can also drag the songs field to the 'Hidden' area if you are more of a drag & drop person.
    Hide the songs in the display.
  12. Now can go and create the view. Go to 'Structure > Views' and 'Add new view'. I will use 'Songs' as the 'View Name'. Show 'Content' of type 'Songs' sorted by 'Newest First'. We don't need to create a page so you can uncheck the 'Create a Page' box. We do need to create a block though, so go ahead and check the 'Create a Block' box. You can change all the settings which pop-up. I will leave them as they are. Click on 'Continue & Edit'.
  13. First we are going to make sure only the 'Songs' related to the 'Band' are shown. On the right you click on the blue 'Advanced' link. A box will slide out with additional options. Click on the 'Add' button next to 'Relationships'.
  14. In the search box you can type 'reverse' and all the options will be filtered down to  'Content: Songs (field_songs) - reverse'. Check the box in front of it and click on 'Apply all displays'. The next screen can be ignored, click 'Apply all displays' again.
  15. Next click on the 'Add' button next to 'Contextual Filters'. In the search box type 'nid' and select the 'Content: nid' option. Click on 'Apply all Displays'.
  16. For 'Relationships' use the relationship you have created in step 14. Which in this case is 'field_songs'.
  17. Below 'When the filter value is NOT available' choose 'provide default value'  and for type choose 'Content ID from URL'. And click on 'Apply all Displays'.
    Create a contextual filter with views.
  18. Save the view by clicking on the 'Save' button in the right top corner.
  19. Now we want to display this block. Go to 'Structure > Blocks'. At the bottom you will see a disabled block called 'View: Songs'. Click on 'configure' on the right.
  20. Pick a region to show this block. In this case I'm using the default Bartik theme and will pick the 'Sidebar second'. Click on 'Save Block'.
  21. Navigate to the 'Band' node and now you will see the related 'Songs' displayed as a block!

Although it has been 21 steps this has been a very compact tutorial. The possibilities are endless as you can use views to alter the display of the fields and blocks. If you have any questions about this tutorial let me know in the comments so I can help you!

Want to become a Drupal Expert?

Subscribe to my newsletter and receive e-mails with the latest Drupal tutorials, tips and tricks.