Scenario: Showing multiple authors for a blog post
A client requested that a blog could have multiple authors. In this case there was a Blog content type with an Authors field referencing multiple nodes of the Author content type. The author(s) would be displayed in the article overview.
Using a view, instead of the built-in Drupal functionality showing authors, is necessary when you want to show author nodes instead of the user who created the node. Using a view also gives more flexibility when it comes to what fields to show such as job title.
The problem arises when you want to create an article view and reference the authors in this view. You will likely end up with duplicates because the article is shown once for every author. To fix this problem I created a view which displayed the author(s) of a blog post. I then added this view as a field to the article view. Here is how I did it:
Creating an author content type
First, let’s create the author content type:
- Navigate to Structure > Content types
- Click + Add content type
- Give your content type a name, such as Author
- You can change the Publishing options, etc. as you please.
- Click Save and manage fields
- You can add, delete or edit fields as you please
Creating an article content type
- Navigate to Structure > Content types
- Click + Add content type
- Give your content type a name, such as Article
- You can change the Publishing options, etc. as you please. Just note that if you want to show authors with a view you have to disable Display author and date information under Display settings
- Click Save and manage fields
- You can add, delete or edit fields as you please
- In order to assign an article to one or more authors you need to create an Entity Reference field by clicking on + Add field
- Select Reference > Content as a field type
- Label it how you like, I’ll name it Authors
- Click Save and Continue
- Under Allowed number of values fill in the numbers of maximum authors possible or select Unlimited when you’re not sure
- Click Save field settings
- Under Reference type > Content type select Author
- Click Save settings
Setting up the articles view
Next we need to make a view that displays the articles:
- Navigate to Structure > Views
- Click + Add view
- Give your view a name, such as Articles
- Under view settings set Show to Content and type to Article
- You can create a block or page, and use the settings you prefer
- Click on Save and edit
- You can add fields to this view as you please. I will leave it as is, only showing the title field
- Click on Save
Setting up the authors view
For the authors I will create a separate view. It is possible to do it in the Articles view but it makes more sense to keep the views separated:
- Navigate to Structure > Views
- Click + Add view
- Give your view a name, such as Authors
- Under view settings set Show to Content and type to Author
- Check Create a block as this is needed to implement this view in another view
- I usually set the Display format to fields to give me more freedom on what to show
- Remove the number under Items per block as this technically can be limitless
- Click on Save and edit
- You can add fields to this view as you please. I will leave it as is, only showing the title field
- Open the Advanced options on the right hand side
- Next to Relationships click on Add
- In the Search field type the first few letters of the Entity Reference field you’ve created. I named it Authors so I will type aut
- Mark the checkbox before Content using field_authors
- Click on Add and configure relationships
- Leave settings in the next dialog as is and click Apply
- Open the Advanced options again
- Click on Add next to Contextual filters
- Search for id
- Select the ID field (located in the Content category)
- Click on Add and Configure contextual filters
- Under Relationship select field_authors
- Under When the filter value is not visible select Provide default value
- Select Content ID from URL under type
- Click on Apply. This contextual filter will make sure only authors referenced in the blog will be shown.
- Click on Save
Adding the Authors view to the Articles view
- Download and install the View Field View module (learn how to install a Drupal module)
- Navigate to Structure > Views
- Click on Edit next to the Articles view you’ve created
- Next to fields click on Add
- Search for id
- Select the ID field in the Content category
- Click Add and configure fields
- Check Exclude from display
- Uncheck Display prefix and suffix
- Click on Apply
- Click on the down arrow next to Edit next to Fields and select Rearrange
- Drag the Content: ID field to the top of the list
- Click on Apply
- Next to Fields click on Add
- Search for view and select View field in the Global category
- Click Add and configure fields
- Under View select Authors. Sometimes Drupal jumps to the wrong dialog here. Just click on Save and try again.
- Under display select the Authors view block you created
- Fill in {{ fields.nid }} under Contextual filters. This will ensure only the referenced authors will be shown
- Click on Apply
- Click on Save
If everything went correctly you would now already see the right results in the Preview of the view below. If not, try refreshing the view by clicking on Update preview.
Do you have any questions about this tutorial? Please leave a comment. Also don’t forget to subscribe to my Drupal newsletter for more Drupal tutorials.