0

I am trying to make a template that outputs JSON, listing all events within a date range. I want to be able to display my custom channel fields in the Event channel, as well as within the Calendar channel that the event is associated with.

I have tried so many things, and cannot seem to get this to work. Not sure if I just can't find the right syntax, or if it's not possible with the Calendar tags.

Which {exp:calendar:???} tag will allow me to display custom fields from both my event and calendar channels? Thanks!

1
  • I know one (bad) solution would be to pass the calendar_id to an embedded template and do a loop there to get the data, but I didn't want the overhead that would take, with an embed for each event... So my current workaround is to use stash, and make an index of the calendar custom info using set_list and then grabbing that in my {exp:calendar:events} loop where I need it. Seems to work fine... but still curious if there's a 'right' way to do this... Seems like a feature that SHOULD exist... Commented Oct 9, 2015 at 21:01

2 Answers 2

1

Highly suggested reading to distinguish Calendar tags: https://solspace.com/docs/calendar/getting_started/

{exp:calendar:cal} is usually used when displaying a list of events in a chronological order. However, since it doesn't use the native EE Channel:Entries library when parsing data, it won't be able to parse some custom fields correctly, in particular those with complex data such as Playa/Relationships and Matrix/Grid.

If you do need to use the full power of Channel:Entries to parse your custom fields, you'll need to use {exp:calendar:events}. However, this tag is used to display information about event entries, and isn't really made to display events in a fully chronological order (again, reading the above reading material is highly suggested to distinguish both tags)

{exp:calendar:cal} and {exp:calendar:events} display data from the Calendar: Events channel. You can't mix both Calendar: Calendars and Calendar: Events channels with these tags.

You could use {exp:channel:entries} to display custom fields from both the Calendar: Calendars and Calendar: Events channels, but you will not be able to display any event data (eg. start/end dates/times, occurrences, etc), nor display data in a chronological way.

As for displaying data in JSON format, there's likely a number of ways to do this. You could use PHP to create the JSON output (using json_encode()). you could hand-craft the JSON output manually. There might even be a plugin that can create the JSON output for you.

1
  • Thank you for your thorough answer! I have read the docs on "Choosing the right calendar tag for your project". It just seemed to me that having access to event and calendar custom fields at once would be a useful feature. Now that I know it is not possible with Calendar tags, I feel better about my workaround. I will post my workaround as an alternate answer to this question. Commented Oct 13, 2015 at 15:50
0

Since @Solspace responded clarifying the uses and limitations of the various calendar tags, it is now clear to me that it is not possible to access both event AND calendar custom fields within one calendar tag pair. My workaround, now, is to utilize stash, and first create an index of my calendar custom fields, that I can then access from within my {exp:calendar:cal} tag loop, for each event I am displaying.

{exp:http_header content_type="application/json"}
[
{exp:stash:set_list name="calendar_data"  parse_tags="yes"}
    {exp:calendar:calendars}
        {stash:my_calendar_id}{calendar_id}{/stash:my_calendar_id}
        {stash:my_calendar_title}{title}{/stash:my_calendar_title}
        {stash:my_calendar_color}{calendar_color}{/stash:my_calendar_color}
        {stash:my_calendar_icon}{calendar_icon}{/stash:my_calendar_icon}
    {/exp:calendar:calendars}
{/exp:stash:set_list}
{exp:calendar:cal
    date_range_start="{get:start}"
    date_range_end="{get:end}"
    status="not closed"
    show_months="999"
    dynamic="no"
}
    {if event_count != 1},{/if}
    {exp:stash:get_list name="calendar_data" match="#^{event_calendar_id}$#" against="my_calendar_id"}
        {
            "id": "{event_id}-{event_count}",
            "calendar": "{event_calendar_url_title}",
            "title": {exp:json_encode}{event_title}{/exp:json_encode},
            "start": {exp:json_encode}{event_start_date format="%Y-%m-%dT%H:%i:%s"}{/exp:json_encode},
            "end": {exp:json_encode}{event_end_date format="%Y-%m-%dT%H:%i:%s"}{/exp:json_encode},
            "allDay": {if occurrence_all_day}true{if:else}false{/if},
            "url_title": {exp:json_encode}{event_url_title}{/exp:json_encode},
            "url": {exp:json_encode}/calendar/event-entry-form/{event_start_date format="%U"}/{event_start_date format="%U"}/{event_id}{/exp:json_encode},
            "icon": {exp:json_encode}{my_calendar_icon}{/exp:json_encode},
            {if event_multi_day}        
                "color": {exp:json_encode}#{my_calendar_color}{/exp:json_encode},
                "textColor": {exp:json_encode}{exp:projects:contrasting_text_color hexcolor="{my_calendar_color}"}{/exp:json_encode},
            {if:else}       
                "color": "#FFFFFF",
                "textColor": {exp:json_encode}#{my_calendar_color}{/exp:json_encode},
            {/if}       
            "className": "{exp:tagger:tags entry_id='{entry_id}'}tag-{tagger:urlsafe_tagname} {/exp:tagger:tags}"
        }
    {/exp:stash:get_list}
{/exp:calendar:cal}
]

Here, I'm using a combination of other addons, like 'HTTP Header' by Rob Sanchez, JSON Encode by NobleStudios, and most importantly, Stash by Mark Croxton.

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.