spicywebau / craft-neo

A Matrix-like field type for Craft CMS that uses existing fields
Other
403 stars 63 forks source link

Entries from another site can be selected in the back end, but they are not output in Twig templates. #885

Closed mike-moreau closed 1 month ago

mike-moreau commented 2 months ago

Bug Description

I am working on a multi-site installation with two sites.

I have a section called "Articles" that is enabled for the Primary site only, and I have a section called "Pages" that is enabled for the Secondary site only.

There is a Neo field called "Blocks" that is available on both Articles and Pages.

On the Secondary Site in the Pages section, I can add selected articles to an entries field. It works as expected in the back end. When I try to output the selection on the front end, the entries field is empty.

Screenshots of the Primary Site:

Screenshot 2024-05-01 at 10 02 16 AM Screenshot 2024-05-01 at 10 02 25 AM Screenshot 2024-05-01 at 10 03 08 AM

Screenshots of the Secondary site:

Screenshot 2024-05-01 at 10 03 23 AM

Articles from primary site can be selected.

Screenshot 2024-05-01 at 10 03 43 AM Screenshot 2024-05-01 at 10 03 50 AM

Article relations are empty in primary site

Screenshot 2024-05-01 at 10 04 01 AM

The related articles field allows for the site select menu:

Screenshot 2024-05-01 at 10 02 44 AM

The code to render the block field looks like this:

{% for block in entry.blocks.all() %}
    {% if block['relatedArticles'] is defined %}
        {% for article in block.relatedArticles.all() %}
            {{ article.title }}
        {% else %}
            No related articles.
        {% endfor %}
    {% endif %}
{% endfor %}

Steps to reproduce

  1. Configure the test content and relations
  2. Attempt to output relationship in twig from Primary site only on Secondary Site

I'm happy to provide a DB dump or other config files if it would be helpful.

Expected behaviour

The relationships to Primary site content would be output in twig.

Neo version

4.1.2

Craft CMS version

4.9.0

What is the affected Neo field's propagation method?

Save blocks to all sites the owner element is saved in.

Does this issue involve templating, and if so, is eager-loading used?

This is not a templating issue

ttempleton commented 2 months ago

This seems to be the case for me even when putting the entries field directly on the entry's field layout - could you please confirm whether that's what happens for you?

mike-moreau commented 1 month ago

@ttempleton yes! I see the same behavior with the entries field on the entry itself.

Checking further, if I add a site('*') parameter to the query, then the articles from the other site are retrieved.

{% for r in entry.relatedArticles.site('*').all() %}
    {{ r.title }}
{% else %}
    No related articles on the entry.
{% endfor %}

{% for block in entry.blocks.all() %}
    {% if block['relatedArticles'] is defined %}
        {% for article in block.relatedArticles.site('*').all() %}
            {{ article.title }}
        {% else %}
            No related articles.
        {% endfor %}
    {% endif %}
{% endfor %}

So what must be happening is the that the current site is used by default when no site param is specified, and we need to expressly set it to get the entries from other sites.

Looks like this is the expected behavior.