1

I'm trying to find the next event after today (now) from a series of entries. This code however outputs all upcoming events after today but I'm just looking for the first (next) event.

Any help is appreciated

{% set nextEvent =  craft.events.events.find() %}
{% for entry in nextEvent %}
    {% if entry.startDate > now %}
    <span class="text-primary">{{ entry.title }}</span><br>
    {{ entry.startDate|date('d F Y ') }}
    {% endif %}
{% endfor %}
2
  • 1
    Is this a plugin you're using or is craft.events.events.find() a typo in your question?
    – carlcs
    Commented May 6, 2015 at 12:17
  • The find() is a typo. I'm using the Craft events plugin for this. Commented May 6, 2015 at 12:30

3 Answers 3

3

When you are setting nextEvent, you can limit only events that are upcoming and only output the first result you get back like so:

.startDate('>= ' ~ now).first()

So you can take that and update your code like so:

{% set nextEvent = craft.entries.section('events').order('startDate').startDate('>= ' ~ now).first() %}

{% if nextEvent %}
    foo
{% endif %}

I have assumed you have a section called 'Event's until we get some clarification as to what craft.events.events is.

3
  • 1
    That was fast enough, Jamie! Congrats. ;) I'd suggest to use first method instead of setting a limit, though.
    – carlcs
    Commented May 6, 2015 at 12:27
  • The code won't work as I'm using the Craft events plugin. {% set nextEvent = craft.entries.events.order('startDate').startDate('>= ' ~ now).limit(1) %} is trowing in an SQL state error (column not found) Commented May 6, 2015 at 12:35
  • 1
    @RobbiePost Hi Robbie, please have a look at the answer posted by carlcs. The code I supplied will work, but you will need to make some adjustments to how you handle events, as that plugin shouldn't be used in production sites.
    – Jamie Wade
    Commented May 6, 2015 at 13:42
2

P&T's events plugin available on GitHub http://github.com/pixelandtonic/Events is actually not meant to be used at all. It was just a demo to show the potential of custom element types!

I'd suggest to use a channel section for your events, and with the code Jamie provided you're already done figuring out how to find your "next event"! You could also collect events with a matrix field, if you have a need for an extra level of hierarchy and if that's still not enough you could have your events in a table in that matrix (using the "Super Table" plugin).

There's also two other plugins: "Events" and "Calendar", but I'm not sure if they are still under development and I'd probably stick to core Craft features (channels / matrix) for now.

4
  • Thanks for clarifying carlcs. I agree, the P&T plugin is a no-go and a channel would be the best option for this.
    – Jamie Wade
    Commented May 6, 2015 at 13:41
  • For me events plugin was a quick and simple solution for a simple project. I solved the issue by hiding the generated list in HTML and only showing the first-child. Thanks for the suggestions Commented May 6, 2015 at 14:13
  • There's a better way to do this, @Robbie. I just posted another answer, but please do me a favor and don't accept it. Using P&T plugin is indeed a no-go ...
    – carlcs
    Commented May 6, 2015 at 14:28
  • The breakloop suggestion did the job and solved the issue. It's clear now that the P&T events plugin is not the way to go Commented May 7, 2015 at 6:20
2

To break a for loop you could have a variable and check against its value in your loop:

{% set breakLoop = false %}

{% for entry in nextEvent %}

    {% if (entry.startDate > now) and (breakLoop == false) %}
        {{ entry.startDate }}
        {% set breakLoop = true %}
    {% endif %}

{% endfor %}

Or – if you have a need to break loops more often – use the plugin "Break and Continue", that gives you a Twig tag for this:

{% for entry in nextEvent %}

    {% if entry.startDate > now %}
        {{ entry.startDate }}
        {% break %}
    {% endif %}

{% endfor %}

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.