How do you get a view to display the contents of a certain node using its ID without passing it through the URL?

I'm trying to display "recommended articles" inside other nodes. I'm doing this by having the main, view which displays all the contents for that page, contain a field called "recommended" which the editor can add an id of another article by way of entity reference.

What I'd like to do is add the recommended article's title, image, and pub date. I can do this just fine using a view, but how do I get it to display the correct article? In other words, how can I tell my recommended article view to display the article which matches the node id in the main view?

To summarize, I have a view with a field with an id (like "527"). I want another view to see that ID and retrieve that article's information and display it on the page.

Thanks!
Brendan

Comments

vm’s picture

perhaps looking at other modules in contrib that were coded around the idea of 'relevant content'. There were a few available when I started with D7 years ago. No idea what is left out there.

onejam’s picture

You should create the 'Recommended article' as a Views block and assign this to the region you want to display it. follow this instruction below to make the block only appear on the article that has content reference to it:

Under fields:
add content id field (make this hidden)
add reference field

Under advanced for contextual filters:
Add Content: Nid
Select 'provide default filter', under type dropdown, choose: 'Content ID from url'
Scroll down and check 'Specify validation criteria'
For 'Validator' dropdown, select 'content'
Select the content type for your article.

Now when you navigate to node 572 (as an example), your 'Recommended article' block should appear with the content you have reference.

I hope that is what you mean?

-----------------------------------------------------------------
We build engaging websites and intuitive designs that will benefit your business.
Duvien

bdparker’s picture

Duvien, I think we're close! THANKS!

I'm working out the details and I think we're on the right track. I'll attach some images of the specifics.

First, I have an article (node 887) titled "Basketball Wins". In this view, I've added a field called "related_1".
https://www.dropbox.com/s/obruwwh2ufj10gl/RELATED1.PNG?dl=0

I've set an article here, and the node ID displays on my page.

Next, I have a block which I just created per your suggestions (though I tweaked a few things). It displays all the nodes just fine, so I know it works. Of course, I want to limit it to the ID of the related article, so I add the contextual filter. This is where it breaks. Nothing displays after I do this.

I've set up my block: https://www.dropbox.com/s/29tgqdws7pdqbtk/RELATED2.png?dl=0
And added the filters: https://www.dropbox.com/s/qgy3sx9kze6sdna/RELATED3.png?dl=0

One thing I tried after following your recommendation is to choose the "field_related_1" as opposed to the "nID" you suggested. The nID didn't work so I thought I'd try that.

I think the logic here is that I want this block to display the information of the view whose node ID matches the field_related_1 string from the current node in the URL.

Any suggestions on what to try next? I'll be playing around with this.

Thanks,
Brendan

onejam’s picture

Did you add a nodeID field under fields (the fields you want to display) in Views?

You need that, then in your contextual filters, use nodeID field, not field_related_1

I think the logic here is that I want this block to display the information of the view whose node ID matches the field_related_1 string from the current node in the URL.

This is why you need the nodeID field as explaind above.

-----------------------------------------------------------------
We build engaging websites and intuitive designs that will benefit your business.
Duvien

bdparker’s picture

Yes, I did do that. :/ It didn't work for me. And I do have nID under Fields.

After it didn't work, I tried playing around with the variables, such as adding field_related_1. I should have explained that better.

I'm guessing I might need a relationship? I'm going to try to write one, but it seems like that's the thing I need to use. I have to learn more about them, though. Is this is the right step? If so, any advice on how to write it?

It seems that my block is having to display content of another node based on the value in a completely different view, so perhaps this is the right step?

bdparker’s picture

I DID IT!

YEAH! EXCITED! Alright... here's what I did.

I followed your instructions, and then set up a relationship. I added the Entity Reference: Referencing entity of field_related_1 to Relationships, then changed my Contextual Filter (Content: Nid) to use the relationship in that dropdown at the top of the window to
"Content referencing Content from field_related_1".

Thank you for your assistance, and I hope this answer helps others!

onejam’s picture

Nice one!

-----------------------------------------------------------------
We build engaging websites and intuitive designs that will benefit your business.
Duvien

bdparker’s picture

Hey Duvien, thanks for your help on that! I actually have another problem I'm working on which is similar. Perhaps you'd like to take a look?
https://www.drupal.org/node/2876088

I'd appreciate any input you could offer.
Also, cool personal website!