2

I have 3 entries. One of the entries got the entries field where Ive attached the two other entries. Im trying to display all the related entries whether entry 1, 2 or 3 is chosen.

For example:

  • Entry 1: Has the entries field. When this entry is selected, I need to show entry 2 and 3
  • Entry 2: When this entry is selected I need to show entry 1 and 3
  • Entry 3: When this entry is selected I need to show entry 1 and 2

Is this possible?

3

This is more tricky then I first thought. I hope this is what you need:

In case the current entry element is not the source of your relation, find that source entry first. This entry then goes into the relatedTo parameter of a new ElementCriteriaModel, where we exclude the current entry entry using its ID.

Code is probably more easy to understand than my wobbling english explanation:

<h1>{{ entry.title }}</h1>
{{ entry.body }}

{# Get `allRelatedEntries` if this `entry` is the "source element" of the relation #}
{% set allRelatedEntries = craft.entries.relatedTo({ sourceElement: entry }) %}

{# Get `allRelatedEntries` if this `entry` is the "target element" of the relation #}
{% if not allRelatedEntries|length %}
    {% set sourceElement = craft.entries.relatedTo({ targetElement: entry }).first() %}

    {# Any relations at all? #}
    {% if sourceElement %}
        {% set targetElements = craft.entries.relatedTo({ sourceElement: sourceElement }).id('not ' ~ entry.id).find() %}
        {% if targetElements|length %}
            {% set allRelatedEntries = targetElements|merge([sourceElement]) %}
        {% endif %}
    {% endif %}

{% endif %}

{% if allRelatedEntries|length %}

    <h2>Related Entries</h2>
    <ul>
        {% for relatedEntry in allRelatedEntries %}
            <li>{{ relatedEntry.title }}</li>
        {% endfor %}
    </ul>

{% endif %}
| improve this answer | |
  • thanks for your respond but ive tried that. entry 1 shows entry 2 + 3 (as I want). But when im at entry 2 or 3 page, it only shows entry 1. – imnotsure Nov 27 '14 at 18:29
  • @imnotsure sorry, it was still not working, I know. Updated again. – carlcs Nov 27 '14 at 18:56
  • Im getting error: The merge filter only works with arrays or hashes. – imnotsure Nov 27 '14 at 22:04
  • This is still untested, @imnotsure. So please comment back if it works for you now. – carlcs Nov 28 '14 at 8:19
  • thanks for your help so far. This seems to work perfectly for entries which are related to each other. But my template breaks on those entries which arent related to anything. I get a error: Undefined offset: 0 – imnotsure Nov 28 '14 at 15:51
1

Here is a bare bones example of how I would deal with this.

I would essentially build an array of the ids I need and then retrieve the entries based on those IDs. It's an example of a technique I recently wrote about.

<h1>{{ entry.title }}</h1>
{{ entry.body }}

{# create empty array #}
{% set relatedItemsIds = [] %}

{# if entry field is not empty, merge entry ids into array #}
{% if entry.newsRelatedItems|length %}
  {% set relatedItemsIds = relatedItemsIds|merge(entry.newsRelatedItems.ids()) %}
{% endif %}

{# find source item in the relationship using the current entry #}
{% set sourceItem = craft.entries.section('news').relatedTo({
  targetElement: entry
}).first() %}

{# 
 # if source item is found
 # - merge its id to the array
 # - merge the ids of its target entries to the array but remove the current entry id from that merged array
 #}
{% if sourceItem %}
  {% set relatedItemsIds = relatedItemsIds|merge(craft.entries.section('news').id(sourceItem.id).ids()) %}
  {% set relatedItemsIds = relatedItemsIds|merge(sourceItem.newsRelatedItems.ids())|without(entry.id) %}
{% endif %}

{# use created array to retrieve an ElementCriteriaModel for the entries you need #}
{% set relatedItems = craft.entries.section('news').id(relatedItemsIds).fixedOrder(true).find() %}

{# display entries #}
<h2>Related entries</h2>
{% for entry in relatedItems %}
  <ul>
    <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
  </ul>
{% else %}
  <p>No related entries found</p>
{% endfor %}

Tested this code on my local sandbox. Can you check if it works for your use case ?

| improve this answer | |
  • Hi Jerome, Ive also tested ur code and it works like a charm! Im going to see which one is performing better. Thanks for ur respond! – imnotsure Dec 1 '14 at 23:12
  • Glad it helps. Should pretty ok performance-wise. Fetching ids is usually quite lean in terms of queries: in my case I get 17 queries and 15Kb of memory. – Jérôme Coupé Dec 2 '14 at 5:39

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.