0

So I have three channels. One is called Event Categories, one is called Events, and one is called Standard Pages.

For other reasons I don't want to get into here, I have decided not to use the native EE "Category" functionality to accomplish this.

What I need to have happen is this. Events are related to Event Categories and then Event Categories are related to Standard Pages. So if I associate an Event Category with a Standard Pages entry, it needs to list all of the specific Events associated with the Event Categories associated with that Standard Page entry. These Events should be listed under a title h3 element for each Event Category listed. If an Event Category is related to the Standard Page, but has no active Events associated with it, it should not be shown.

I am sure this is a very easy thing to accomplish, but relationships always mess with my head and I cannot, for the life of me, figure out the template code to accomplish this.

Thanks for your help!

4
  • 1
    It’s not crystal clear which channels those Playa fields are on. For example, “Events are related to Event Categories” just tells me there’s either a Playa field on Events pointing to Event Categories or there’s a Playa field on Event Categories pointing to Events. Can you please update your post making it clear where the actual Playa fields are? My hunch is that Jason got it backwards. Apr 29, 2013 at 14:09
  • There is a Playa field on the Events channel where you can pick an entry from the Event Categories channel. There is also a Playa field on the Standard Pages channel to select an entry from the Events Categories channel. Hope that helps! Man, relationships mess with your head! ;-)
    – Jacob Graf
    Apr 29, 2013 at 14:21
  • So: Standard Pages have Events, and Events have Event Caterogies. And for a given Standard Page, you want to list all of its Events, organized by Event Category. Got it. Apr 29, 2013 at 14:29
  • Does that mean I was right? Apr 29, 2013 at 17:24

3 Answers 3

1

Assuming both the Standard Pages and Events channels have a Playa field with entries from the Event Categories channel, the following should work:

{exp:channel:entries channel="pages"}
  {exp:playa:children var_prefix="cat"}
    {cat:exp:playa:parents field="event_category" var_prefix="event"}
      {if '{event:count}' == 1}
        <h3>{cat:title}</h3>
        <ul>
      {/if}
          <li>{event:title}</li>
      {if '{event:count}' == '{event:total_results}'}
        </ul>
      {/if}
    {/cat:exp:playa:parents}
  {/exp:playa:children}
{/exp:channel:entries}    

It's confusing for me too, but the Playa terminology page helps me understand what's what.

Standard Page = Parent (The entry that has the Playa field.)
Event Category = Child (The entry that you selected in the Playa field.)

Event = Parent (The entry that has the Playa field.)
Event Category = Child (The entry that you selected in the Playa field.)

3
  • Thanks. I've updated the field names and it doesn't seem to do anything except cause the page to time out. I think you're on to something with the var_prefixes, but I don't know what's causing it to hang?
    – Jacob Graf
    Apr 29, 2013 at 13:57
  • I was using 4.3.3. I doubt that the later versions would break it... but... maybe? I had almost that exact code, but on my index page. I had dynamic="no" added to my outer channel entries loop to display all the pages. Don't see why it would be any different though. Apr 29, 2013 at 17:33
  • What version of EE are you using? Apr 30, 2013 at 13:46
1

By golly, I've got it!

Thanks to everyone for your help! Like I said, relationships are crazy-hard to wrap your head around and I couldn't have figured it out without your help. The answer turned out to be Co-Parents.

Here is the final answer to my question...

{standard_page_events var_prefix="cat"}
    <h4>{cat:title}</h4>
    {exp:playa:coparents var_prefix="event" channel="events" child_id="{cat:entry_id}"}
        {event:title}<br>
    {/exp:playa:coparents}
{/standard_page_events}

This article really pushed me in the right direction.

http://pixelandtonic.com/blog/playa-coparents

0

The tricky part here is organizing Events by Event Categories. I can think of two ways to do it:

  • Loop through all possible Event Categories, and for each one, check to see if there are any events for the current category that were selected by the current page.
  • Just loop through all the events, and use a little PHP on the template to index them by their Event Categories, and then output them in a separate loop.

Neither option is great, but I’m leaning toward the second one because it should be a bit more efficient.

{exp:channel:entries channel="standard_pages" ... }

    {!-- Get all of the events grouped by their categories --}
    <?php
        $events_by_cat = array();
    ?>
    {exp:playa:children channel="events" var_prefix="event"}
        <?php
            $cat_ids = array({event:exp:playa:child_ids channel="event_categories" delimiter=", "});
            $event = <<<EOD
                <h4>{event:title}</h4>
                <p>{event:event_description}</p>
                ...
EOD;
            foreach ($cat_ids as $cat_id)
            {
                $events_by_cat[$cat_id][] = $event;
            }
        ?>
    {/exp:playa:children}

    {!-- Loop through all categories, fetch their titles and list their events --}
    <?php foreach ($events_by_cat as $cat_id => $events): ?>

        <?php $cat = get_instance()->db->select('title')->where('entry_id', $cat_id)->get('channel_titles')->result(); ?>
        <h3><?php echo $cat[0]->title ?></h3>

        <?php foreach ($events as $event): ?>
            <?php echo $event ?>
        <?php endforeach ?>

    <?php endforeach ?>

{/exp:channel:entries}
9
  • Ewwww. I just got back to check this out and I am kind of floored that I would have to use PHP to accomplish this? Is this really a case that Playa cannot handle natively? Seems like a simple thing in theory, doesn't it? It's just two levels of relation. Any thoughts Brandon? I am really hesitant to implement a solution that involves PHP.
    – Jacob Graf
    Apr 29, 2013 at 18:45
  • Playa does not natively allow for this to happen easily. There are a couple ways to do it without enabling PHP in your templates (NEVER DO THAT); i'm trying to do something else right now but will write up an answer shortly.
    – adrienne
    Apr 30, 2013 at 2:29
  • I appreciate your help. In my head it doesn't seem like it should be this hard? Also, Adrienne, I am open to completely changing how this is set up if I need to.
    – Jacob Graf
    Apr 30, 2013 at 2:48
  • As I said, the only other thing I could think of would be to loop through all categories, and for each one check to see if there are any events for the current category and page. That route would require an {embed} and a ton more {exp:channel:entries} calls (one per category), and won’t be pretty either. At least my proposed solution is efficient. And regarding PHP in templates – never say never. Apr 30, 2013 at 15:08
  • So why would you need to loop through ALL categories? Here is the logic I was following. Load a Page entry, see what Event Categories are related to that Page, then look up those Event Categories and pull in all Events related to the Event Categories assigned to that Page. That's the logic you were following as well, right? Seems simple in theory! :-) Also, I figured I would cache the rendered page for 24 hours so that would help on queries, etc.
    – Jacob Graf
    Apr 30, 2013 at 16:21

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.