You are here

How to create a View with a featured item and a list that excludes the featured item, Drupal 8

A common pattern for websites is a list of content, for example news, with one (or more) featured items, followed by a list of a few more items, excluding the featured one(s). This seems like it should be simple to do, but it's not as straightforward as it seems.

Here's an example of how to achieve it with Views in Drupal 8. There are many ways of flagging featured content. In this case, we want to display the items on the front page, and we have a Homepage content type with an entity reference field called "Featured News Article". The field references the News content type, and is limited to one item.

Our goal is a block with a list of 4 news articles, one of which is the article referenced from the featured news field, and three which are simply most recent, excluding the featured one. I won't go through all the nuances of the View setup; assuming you know the basics of Views, we'll just focus on the featured part.

First, add the entity reference field to the content type where you want to display your list of items. Make sure you've created some content of the type you are displaying, and that you've added at least one in the entity reference field, so you have data to test with.

Next, we create a view of content, type News, sorted by newest first. Add a block display and configure it with the fields and settings you need. This will be the list of latest news, excluding the featured article. In our case, we want to show 3 articles, so in the "Pager" settings we choose "Display a specified number of items" and enter 3.

Next, add another display, an attachment—this will be the featured article. In "Attachment settings", attach it to the block display and select "Before" as "Attachment position". If you want, you can limit the number of items, but if you've set up your featured news field to limit it to the number you want to show, this shouldn't be necessary.

Next, we are going to add a relationship to the view. Relationships are a way of passing information to the view that normally it wouldn't know about, in this case, what is in the "Featured News Article" field on the homepage.

In the "Advanced" tab of the View settings, Relationships, click "Add" and select "Content using field_featured_news". Make sure this relationship is added to both the block and attachment displays.

Next, we'll add a contextual filter to both displays, but configure it slightly differently. To both displays, add the "ID" (content) contextual filter. In the filter configuration, from the "Relationship" dropdown, select the field_featured_news relationship you added earlier. Under "When the filter value is not available", select "Provide default value" and in the "Type" dropdown, select "Content ID from URL".

Now, for the attachment you're done. You've told it to only display content which is in the featured news article field of the node this view is displayed on. You can test it by entering the homepage node ID in the "Preview with contextual filters" field in the view preview while editing the attachment. When you click "Update preview", you should see the article you chose in the homepage featured news field. (If you have limited the number of items to display, you may want to remove the limit for testing just to make sure it's not coincidental that the same article is showing, and that the view's been set up correctly).

Next, edit the contextual filter settings for your block display (making sure you're editing the block only; select "This block (override)" in the "For" dropdown at the top if necessary). At the very bottom of the filter configuration popup, under "More", select the "exclude" checkbox. This tells it to not display the content which is in the featured news field of the node the block is displayed on. Again, you can enter the node ID in the preview pane to confirm. (Note, if you've limited the number of items in the block, you may want to unlimit them just for testing, to be sure the featured article isn't not showing just because it's the first article you created. I've been caught out that way before).

If everything has been set up correctly, in the preview you should see your featured article under "Attachment before" and the rest of the articles you've chosen to display under "Content", excluding the featured one. Save your view and you're good to go!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.