Score:0

How can I use contextual filters to display a block with field values from a single related entity?

pe flag

I've found various recipes/threads with the scenario where have a content type A node and want to display in a sidebar block, all the related content type B nodes, with the relationship based on an entity reference field on content type B. Example and another

I've attempted to adapt these instructions to my situation, without success. My case is sort of flipped.

Content type: Resource
Content type: Source
Entity reference field: On the Resource content type, named source_select

So I have a bunch of Resource nodes that have a single source selected for each.

What I want to do:

On each Resource node, I want to show info from fields of its one related Source in a sidebar block.

What happens:

For every combination of configuration options I've been able to think of or draw from other threads, etc., I get an empty block.

One example:

View display: block
View filter: published = yes
Contextual filter: Content: Source (field_select_source)
Provide default: enabled
Default type: Content ID from URL

I feel like I'm missing something obvious--and I've successfully built working contextual filter blocks before in D7, but I don't have any of those sites operating now and can't remember what the secret was and can't seem to crack it again now.

Score:0
de flag

You want to create a view of Source nodes, then set a contextual filter for the Resource ID. This way, when you are on a Resource page, the Resource ID will be in the URL. The contextual filter will use this to limit the Source nodes (which your View is showing) to any sources referenced by that Resource, which in your case will be a single Source node. Then you can display the Source node either as a teaser (or create a custom view mode and use that), or you can display fields.

Score:0
pe flag

Pieces fell into place after a short nap.

Here's how you do it...

Concepts:

The contextual filter will be to display in the sidebar bock the same Resource node (matches the NID) loaded as a full node.

A relationship is needed to display field data for the related Source. In this situation, you don't contextual filter on the Source at all.

For me, it worked best to set them up in that order.

  1. Settings to get the current node filtered into a sidebar Views block:

Provide default value: enabled
Type: "Content ID from URL"

Save the View.

At this point, I did a bit of testing by putting a nid in the "Preview with contextual filters" field below the View. Worked as expected.

  1. Settings for Relationship to Source.

Relationship: Content referenced from field_select_source (important: This is not the same as the "Content using..." option)
Require this relationship: enabled (this may not be necessary, but made sense to me and works)

Apply and save the view.

  1. Add fields from Source and choose the "field_select_source: Content" in the Relationship dropdown for each field.

Add the block to sidebar region for Resource nodes. That's it.

Result: Each Resource has a sidebar block showing Source information.

Edit: Jaypan's answer posted while I was writing this. It sounds more like how to display Resources while viewing the Source, which is the flipside of the relationship. But maybe that can work also.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.