1

I have another interesting challenge to solve with Solspace Calendar and I've not been able to find a solution so far, which is not to say there isn't an obvious solution I've missed.

I have a custom field on events (event_featured) which allows an admin to flag individual events as 'featured' and I want to show a list of these featured events on the homepage. However, if an event is recurring, I only want to show the next occurrence of that event and not all occurrences.

On the homepage, I'm pulling out a list of the featured events with this code:

{exp:calendar:cal date_range_start="today" date_range_end="2 years" enable="custom_fields"}
  {if event_featured == "y"}
    <li>
      <h3><a href="{path='events/event'}/{if edited_occurrence}{event_parent_id}{if:else}{event_id}{/if}/{event_start_date format="%Y/%m/%d"}">{event_title}</a></h3>
      {if event_all_day}<time class="eventdate" datetime="{event_start_date format="%Y-%m-%d"}">{event_start_date format="%l %j<sup>%S</sup> %F %Y"}</time>
      {if:else}<time class="eventdate" datetime="{event_start_date format="%Y-%m-%dT%H:%iZ"}">{event_start_date format="%l %j<sup>%S</sup> %F %Y <i>at</i> %g:%i%a"}</time>{/if}
      <p class="description">{event_summary}</p>
    </li>
  {/if}
{/exp:calendar:cal}

This correctly displays events that are flagged as featured but obviously if an event is recurring, it shows all future instances. Can anyone suggest a solution?

1

Another way of interpreting the question might be "how to display events in chronological order, but with each different event appearing only once, regardless of how many occurrences it has in the timeframe".

One approach is to use PHP to create an array of unique events, and display your event data only when the event is encountered for the first time in {exp:calendar:cal}. Here's an example (turn on PHP for your template and set it to Output):

{!-- Create the arrays --}
<?php 
    $event_ids = array();
    $event_data = array();
?>

{!-- 
    // Check if the event was already encountered (i.e. is in the array),
    // and store your data in a separate array if this is the first time
    // encountering this event 
--}
{exp:calendar:cal /* your other parameters here */}
<?php 
    if( ! in_array('{event_id}', $event_ids) )
    {
        $event_ids[] = '{event_id}';
        $event_data[] = '* The data you want displayed';
    }
?>
{/exp:calendar:cal}

{!-- Display event data, still in chronological order --}
<?php 
    foreach($event_data as $data)
    {
        echo $data;
    }
?>

Of course there are likely other strategies, using PHP, Stash, or other means, but the main idea is to create a list/group/array of unique events within your timeframe.

| improve this answer | |
  • Brilliant, thanks, I think that's got me 99% of the way there. The only issue now is that I'm receiving syntax error, unexpected 's' (T_STRING) when I try to display the event summary like this $event_data[] = '{event_summary}'. – Matt Obee Feb 23 '14 at 16:05
  • Should that be $event_data[] = '{event_summary}'; (semi-colon at the end)? – Solspace Feb 23 '14 at 23:27
  • There is actually. $event_data[] = '{event_title}'; is fine but $event_data[] = '{event_title} {event_summary}'; errors. – Matt Obee Feb 24 '14 at 9:03
  • OK, I got this to work using {exp:xml_encode}, like so: $event_data[] = '{exp:xml_encode}{event_summary}{/exp:xml_encode}'; – Matt Obee Feb 28 '14 at 22:07
1

I wanted something very similar - a list of related events (defined by an earlier created stash) that showed a chronological list of only the NEXT 'occurrence' of each event. I saw this thread, but being new to PHP couldn't follow the example because I didn't know how to extrapolate all the $data variable to where I could build up the actual HTML I wanted... so in case anyone else in my shoes finds this later on, I wanted to share my code that hopefully explains well enough for a noob:

<!-- Create array to hold data from each related event against the Event ID -->
<?php $events = array(); ?>
{exp:calendar:cal
    date_range_start="today"
    date_range_end="1 year"
    pad_short_weeks="n"
    enable="custom_fields"
    event_id="{exp:stash:get name="stash_related_events"}"
    dynamic="no"}

    {events}
        <!-- Populate array with individual event details - this will include all occurrences, but we'll remove duplicates in PHP -->
        <?php $events['{event_id}'] = '{event_id}~~{event_title}~~{event_summary}~~{event_image:Medium}~~{event_location}~~{event_start_date format="%F %j, %Y"}~~{event_start_date format="%g:%i%a"}~~{event_end_date format="%F %j, %Y"}~~{event_end_date format="%g:%i%a"}'; ?>
    {/events}

    <!-- Reverse order of array because they're backwards for some reason (??) -->
    <?php $events = array_reverse($events); ?>

    <!-- Loop through each unique event_id in the array to get desired list -->
    <?php foreach($events as $event_id => $event_data) :?>

        <?php
            // Extrapolate data in the array to have usable variables using ~~ as the delimiter
            list($event_id, $title, $event_summary, $event_image, $event_location, $event_startDate, $event_startTime, $event_endDate, $event_endTime) = explode('~~',$event_data);

            // Check for empty image
            if ($event_image == "") { $event_image = "../../images/uploads/eventImages/_Medium/event-default.jpg"; }

            // Format Date/Time entry
            $event_dateTime = "";
            if ($event_startDate == $event_endDate){
                if ($event_startTime == "12:00am" && $event_endTime=="12:00pm"){
                    $event_dateTime="All day ".$event_startDate;
                } else {
                    $event_dateTime=$event_startDate." from ".$event_startTime." to ".$event_endTime;
                }
            } else {
                $event_dateTime="From ".$event_startDate.", ".$event_startTime." to ".$event_endDate.", ".$event_endTime;
            }

            // We could build up HTML inside PHP using either syntax below...
            // echo "<h2>".$title."</h2>";
            // echo "<h2>$title</h2>";  Same as above
        ?>

        <!-- But instead we'll build up HTML here and insert variables where necessary -->
        <div class="event item clearfix">
             <h2><a href="../../events/event/<?php echo $event_id ?>"><?php echo $title ?></a></h2>
             <h4 class="eventDateTime"><?php echo $event_dateTime ?></h4>
             <div class="eventImage">
                <a href="../../events/event/<?php echo $event_id ?>"><img src="<?php echo $event_image ?>" alt="<?php echo $title ?>" /></a>
             </div>
             <p>Location: <?php echo $event_location ?></p> 
             <p>Summary: <?php echo $event_summary ?><p>
        </div>

    <?php endforeach; ?>

{/exp:calendar:cal}

Hope this helps someone save the amount of time I spent on this!

| improve this answer | |

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.