0

I have a grid field that contains a relationship field. I want to alter the output according to whether or not a particular field in the related entry contains data or not. For instance:

{if grid_field:relationship_field:related_entry_content_field != ""}Do this{if:else}Do something else{/if}

Instead of the expected results, it prints "{if" then seems to evaluate both conditions as true.

Any ideas on how to make this work?

Edit: Here are some examples of what works and what doesn't work:

{exp:channel:entries channel="channel_name" search:search_field="={segment_x}" dynamic="no"}
    {grid_field}
            {!-- Grid field, then relationship field, then field from the related entry - no tag pair. This works: --}
            <h1>{grid_field:relationship_field:title}</h1>
            {!-- Same syntax as above but used in a conditional. Does not work: --}
            <h2>{if grid_field:relationship_field:title == "foo"}Foo{if:else}Not foo{/if}</h2>
            {!-- Using tag pair for the relationship field as suggested below. This works: --}
            {grid_field:relationship_field}
                <h3>{if grid_field:relationship_field:title == "foo"}Foo{if:else}Not foo{/if}</h3>
            {/grid_field:relationship_field}
    {/grid_field}
{/exp:channel:entries}

Here is the output from the above code:

Title of Related Entry (Desired result from Line 4)
{ifFooNot foo (Weird result from Line 6)
Not foo (Desired result from Lines 8 - 10)

Hopefully this helps someone else!

1
  • Can you give more of your surrounding template code - especially everything regarding the grid field. Or are you diving straight in and that is all you've got? Commented Oct 20, 2015 at 8:29

1 Answer 1

0

Both Grid and Relationships are tag pairs (as both can have many), so you can't just request the end point without telling EE which end point you want (which row and which relation).

This should work:

{grid_field}
    {grid_field:relationship_field}
        {if grid_field:relationship_field:related_entry_content_field != ""}
            Do this
        {if:else}
            Do something else
        {/if}
    {/grid_field:relationship_field}
{/grid_field}

You might have to replace in the above the if conditional with this:

{if "{grid_field:relationship_field:related_entry_content_field}" != ""}

Edit based on Question Update:

The cause for the broken conditional is the the same - you have to have the relationship pair. You can implement in a single line conditional:

{grid_field}
    {if "{grid_field:relationship_field}{grid_field:relationship_field:related_entry_content_field}{/grid_field:relationship_field}" != ""}
        Do this
    {if:else}
        Do something else
    {/if}
{grid_field}

Obviously this will fail if there are multiple relationships for the same grid row.

3
  • This works. The problem is the syntax I mentioned in my original post (no tag pair for the relationship field) does work to output the data - it just doesn't work in a conditional. I'll amend my original post to add some examples.
    – Matthew
    Commented Oct 20, 2015 at 15:08
  • Did you try: {if "{grid_field:relationship_field:related_entry_content_field}" != ""}? With qoutes and curlies in the conditional? Commented Oct 20, 2015 at 15:36
  • I tried it with curlies, which (predictably) threw an error. I didn't think to try it with quotes too (I haven't worked with conditionals much since EE changed how they're handled). I'll give that a shot as it's certainly more graceful to eliminate the tag pair if possible.
    – Matthew
    Commented Oct 20, 2015 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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