<p>Looking at the <a href="https://solspace.com/craft/calendar/docs/calendar.events/#param-calendar" rel="nofollow noreferrer">docs</a> you can search for <code>events</code> for a certain <code>calander</code> by including the <code>id</code> or the <code>handle</code> in your query.</p>

<pre><code>{% set calendars = craft.calendar.calendars({
    limit: 100,
    order: "name",
    sort: "asc"
}) %}

{% for calendar in calendars %}
    {% set events = craft.calendar.events({
        calendarId: calendar.id
    }) %}

    {% if events|length %}
        // there are events
    {% else %}
        // calendar has no events
    {% endif %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2018-03-23T14:20:05.940Z
2018-03-23T14:43:39.840Z
2018-03-23T14:43:39.840Z
Robin Schambach
<p>I need to write a conditional that determines if events actually exist in a calendar. I only want to display an option in the the select dropdown IF the calendar has events:</p>

<pre><code>{% set calendars = craft.calendar.calendars({
    limit: 100,
    order: "name",
    sort: "asc"
}) %}

{% set events = craft.calendar.events({
    calendarId: calendar.id
}) %}

{% for calendar in calendars %}

    {# determine what the URL should look like based on the current URL structure #}

    {% if segment1 == '' %}
        {% set urlPart = "month/calendar/" ~ calendar.handle ~ "/" ~ month.date.format('Y/m') ~ "#1" %}
    {% elseif segment3 is not empty and segment4 is not empty %}
        {% set urlPart = "month/calendar/" ~ calendar.handle ~ "/" ~ segment4 ~ "/" ~ segment5 ~ "#2" %}
    {% else %}
        {% set urlPart = "month/calendar/" ~ calendar.handle ~ "/" ~ segment2 ~ "/" ~ segment3 ~ "#3" %}
    {% endif %}

    {# I need a conditional here that outputs an option only IF there are events in the calendar #}

    {% if events %}
        &lt;option value="{{ siteUrl }}cal/{{ urlPart }}"{% if segment3 == calendar.handle %} selected="selected"{% endif %}&gt;
            {{ calendar.name }} - {{ calendar.event.occurrenceCount }}
        &lt;/option&gt;
    {% endif %}

{% endfor %}
</code></pre>

<p>UPDATE: I expanded this out so you can see everything. Unfortunately, this is still outputting all calendars even if the calendar has no events.</p>

CC BY-SA 3.0
2018-03-23T14:34:05.990Z
Of course you have to search the events for each calendar.... so you have to search the events inside your loop. Your calendar variable is not even defined at this point
Robin Schambach
CC BY-SA 3.0
2018-03-23T14:38:51.410Z
I apologize, but I'm a newbie to Craft (migrating from EE). Perhaps you could help me with a code example?
Vaughn D. Taylor
CC BY-SA 3.0
2018-03-23T14:44:31.673Z
This has nothing to do with a certain CMS/Framework it's just about basic code logic, I updated my answer
Robin Schambach
<conditional><plugin-solspacecalendar>
How to determine if there are events in a calendar?
# How to determine if there are events in a calendar?

(Asked by: username_0 on 2018-03-23)

I need to write a conditional that determines if events actually exist in a calendar. I only want to display an option in the the select dropdown IF the calendar has events:
{% set calendars = craft.calendar.calendars({
    limit: 100,
    order: "name",
    sort: "asc"
}) %}

{% set events = craft.calendar.events({
    calendarId: calendar.id
}) %}

{% for calendar in calendars %}

    {# determine what the URL should look like based on the current URL structure #}

    {% if segment1 == '' %}
        {% set urlPart = "month/calendar/" ~ calendar.handle ~ "/" ~ month.date.format('Y/m') ~ "#1" %}
    {% elseif segment3 is not empty and segment4 is not empty %}
        {% set urlPart = "month/calendar/" ~ calendar.handle ~ "/" ~ segment4 ~ "/" ~ segment5 ~ "#2" %}
    {% else %}
        {% set urlPart = "month/calendar/" ~ calendar.handle ~ "/" ~ segment2 ~ "/" ~ segment3 ~ "#3" %}
    {% endif %}

    {# I need a conditional here that outputs an option only IF there are events in the calendar #}

    {% if events %}
        
            {{ calendar.name }} - {{ calendar.event.occurrenceCount }}
        
    {% endif %}

{% endfor %}

UPDATE: I expanded this out so you can see everything. Unfortunately, this is still outputting all calendars even if the calendar has no events.

**Tags:** conditional, plugin-solspacecalendar
> **Comments:**
> **username_1**: Of course you have to search the events for each calendar.... so you have to search the events inside your loop. Your calendar variable is not even defined at this point  
> **username_0**: I apologize, but I'm a newbie to Craft (migrating from EE). Perhaps you could help me with a code example?  
> **username_1**: This has nothing to do with a certain CMS/Framework it's just about basic code logic, I updated my answer  

---

## 1 answers

### Answer by username_1 on 2018-03-23. Score: 3 | Accepted answer

Looking at the docs you can search for events for a certain calander by including the id or the handle in your query.
{% set calendars = craft.calendar.calendars({
    limit: 100,
    order: "name",
    sort: "asc"
}) %}

{% for calendar in calendars %}
    {% set events = craft.calendar.events({
        calendarId: calendar.id
    }) %}

    {% if events|length %}
        // there are events
    {% else %}
        // calendar has no events
    {% endif %}
{% endif %}


---

