2

While I wait for Playa to be approved for use in my project, I've put together this structure to enable cross-referencing:

3 Channels:

  1. products
  2. all_colors
  3. product_colors

The all_colors channel just has a file fieldtype set for images.

The product_colors channel just has 2 relationship fields: related_products and related_colors. So, for each product (products), we want to be able to select a number of colors (all_colors), and we do that through the cross-reference (product_colors).

Here's my template:

{exp:channel:entries channel="products" limit="1" }
<div id="hero"><img src="{hero_image}" /></div>
{embed='includes/topnav'}
{if no_results}
<p>Unknown product.</p>
{/if}
<div id="prodinfo">
    <h1>{title}</h1>

    <div id="description" class="desc">{product_description}</div>
    <div id="productcolors">
    <ul id="prodcolors">
        {reverse_related_entries channel="product_colors"}
        <li>
            Related Product: {related_product}<br>
            Related Color: {related_color}
        </li>
        {related_entries id="related_color"}
        <li><img src="{color_swatch}" /></li>
        {/related_entries}
        {/reverse_related_entries}
    </ul>
    </div>

</div> <!-- END #prodinfo -->
{/exp:channel:entries}

And here's the output:

Related Product: 19
Related Color:
{REL[20][related_color]jPUgzXDsREL}
Related Product: 17
Related Color:
{REL[18][related_color]jPUgzXDsREL} 

The first LI is just for debugging purposes. The funky looking token in the curly braces changes on every request. I've no idea where it comes from. The template doesn't even produce my img tag, or the second LI, for that matter.

[EDIT] Why is the Related Color coming out empty? [/EDIT]

What am I doing wrong? Can this even be done with EE out-of-the-box?

Thank you.

1

Why is the Related Color coming out empty?

Because when a Relationship field is used as a single tag ({related_color}), it doesn't know what to output (as its just a pointer to another entry). Relationships fields can only be output using the {related_entries id="field_name"} tag pair.

What you're getting in your output is the cached relationship data stored in the field, which is only properly decoded when you use the {related_entries} tag pair.

Also, related_entries tags cannot be used inside reverse_related_entries tags.

For now, James' answer of using an embed is the way to go if you have to stay native to EE.

| improve this answer | |
  • Yep, I figured there'd be some limitation like you can't use one within the other. Thanks for your help folks. I just got Playa and the colors are nailed!! Talk about the right tool for the job. Couldn't have taken me more than 10 minutes. – NotoriousWebmaster Mar 20 '13 at 21:24
1

Doing this without Playa will be a world of pain. Without it I think there are two ways:

First, (and this is really bad for performance, but here it is anyway)... using an embedded template in place of the related_entries tag:

    ...
    <ul id="prodcolors">
      {reverse_related_entries channel="product_colors"}
        <li>
            Related Product: {related_product}<br>
            Related Color: {related_color}
        </li>            
        {embed="related-color" entry_id="{entry_id}"}
      {/reverse_related_entries}
    </ul>

Embedded 'related-color' template:

{exp:channel:entries entry_id="{embed:entry_id}" dynamic="no"}
   {related_entries id="related_color"}
     <li><img src="{color_swatch}" /></li>
   {/related_entries}
{/exp:channel:entries}

The second way would be to do some gymnastics with Stash to avoid calling the embed per-entry. I tried to come up with a working example of that, but it's really complicated - you're much better off just getting Playa! (Writing a custom plugin would be simpler than what you'd need to do with Stash here).

| improve this answer | |
  • Thanks for the reply James. I'm curious as to why the related_color comes out empty in my example. I expect this has a lot to do with why I'm then not getting to the color_swatch in the all_colors channel. – NotoriousWebmaster Mar 20 '13 at 19:35

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.