2

Not sure that is the best title but:

I have a Section called Research Projects. An entry from this section contains a number of images, videos and audio entries in a grid chosen through an Entry Field. At the moment when one of the grid items is selected it displays a modal showing the details of that entry. There is no issue is showing a relationship as the info is displayed on the same page.

My question is if I chose to show the entry selected on a new page how would I show the relationship to the page it appears on / came from? Perhaps as a title from the previous page or a back button.

I am not using a structure as video, image items can appear in multiple projects.

Hope that makes sense and thank for any help anyone can offer.

Scott

1

This is commonly called 'reverse-related entries' and is achievable using relatedTo(). See here for some info...

My question is if I chose to show the entry selected on a new page how would I show the relationship to the page it appears on / came from? Perhaps as a title from the previous page or a back button.

For example, if a visitor were on an Audio Entry page, your template code would look something like:

{% set relatedResearchProject = craft.entries.section('researchProjects').relatedTo(entry) %}
{% for researchProject in relatedResearchProject %}
    <li><a href="{{ researchProject.url }}">{{ researchProject.title }}</a></li>
{% endfor %}

The above, would display any Research Projects which contain related "audio" Entries.


The logic is as follows:

{% set relatedResearchProject = craft.entries.section('researchProjects').relatedTo(entry) %}
  1. Create a new variable called "relatedResearchProject" (in fact the variable name can be any word which makes sense to you)
  2. The new variable should contain any entries from the Research Projects Section which are related to the the (Audio) entry being displayed on the current page.

Then...

{% for researchProject in relatedResearchProject %}<li><a href="{{ researchProject.url }}">{{ researchProject.title }}</a</li>{% endfor %}

Loop through and display any entries which are in the 'relatedResearchProject' variable.


In a different example below, you can see that "Third news entry..." contains the Entry field. then, on /news/we-just-installed-craft you can see the rendered link back to the "Third news entry..." page.

Sorry, Scott - I hope illustrating two examples isn't confusing! Let us know how you get on.

enter image description here enter image description here

{% for researchProject in relatedResearchProject %}<li><a href="{{ researchProject.url }}">{{ researchProject.title }}</a</li>{% endfor %}
|improve this answer|||||
  • Hi Matt, just responded below as I maxed out the character count. – Scott Robinson May 26 '16 at 17:16
  • Looking back out your response it seems the two solutions work as a way of navigating back to the related project as long as the item isn't related to multiple projects. This may actually be okay for my current situation. Am I to assume that there is no way in craft to refer to the page I have just come from? I guess that could be achieved by JS maybe. – Scott Robinson May 26 '16 at 17:26
  • Hey Scott. I agree, I would handle a 'back' link with JS. I've also updated my answer to explain the template code. If multiple Research Projects contain relationships to Audio entries, then the above template code would display all of the Research Projects. Hope that helps – Matt P May 27 '16 at 16:33
  • Hey Matt, thanks for your help. I've got my head around relatedTo and am actually using the many to many plugin with success. – Scott Robinson Jun 4 '16 at 15:25
  • Well done Scott! Glad we could help :) – Matt P Jun 5 '16 at 6:02
0

Thanks for the response. I had a feeling that RelatedTo might help but I'll admit I was having difficulty understanding the logic.

Just so I understand the first example:

{% set relatedResearchProject = craft.entries.section('researchProjects').relatedTo(entry) %}

The above sits on the audio file template page and basically defines relatedResearchProject as the entry in the section 'researchProjects' that the details propagating the template relate to. This is essentially creating a relationship from the page I have just come from?

{% for researchProject in relatedResearchProject %}

  • {{ researchProject.title }}
  • {% endfor %}

    The above just lists out the title and url from the related project page.

    I just wanted to check that you are providing 2 solutions to the problem.

    Your second example would allow me to show the relationship to a project or multiple projects. Which I did consider. But it wouldn't help in navigating back to the previous page or show my audio page as a sub-page of the project I'd come from. Is that about right?

    |improve this answer|||||
    • Hey Scott - No. The template code I mentioned all applies to 1 example. I'll amend my answer to make it more clear... – Matt P May 26 '16 at 17:17

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.