2

I'm trying to plot entries from two different Solspace Calendars and an Entries section on one map, and use different markers for all types. Alll of the events are related to the current Entry through a field set on the Event, and a field set on the Entry. I have:

  • E Waste - Solspace Calendar
  • Community Events - Solspace Calendar
  • Disposal Sites - Craft Entry

All three of those above have address fields, and I'll be plotting them on a Smart Map. Currently, I'm doing this:

{# Get all eWaste events related to material #}
{% set eWaste = craft.calendar.events({
calendar: 'eWaste',
    relatedTo: [ 'or', { targetElement: entry.materialCategory }, { targetElement: entry } ],
    rangeStart: now|date('Y-m-d'),
    rangeEnd: now|date_modify('+3 months')
}) %}

{# Get related sites #}
{% set sites = craft.entries.section('sites').relatedTo([ 'or', { targetElement: entry.materialCategory }, { targetElement: entry }]) %}

{# Merge events and entries #}
{% set locations = eWaste|merge( sites ) %}

{{ craft.smartMap.map(locations, options) }}

That gets events from one calendar, and the entries in to a single array so I can plot them on one map, but I can't work out how to differentiate between the types in order to give them different markers. As an example, entry.type doesn't work, and neither does {% if entry.eventField is defined %}

Am I going about this the wrong way? How do I achieve what I'm after? Will the above approach also work for two different calendars + entries?

4

Not sure if I totally got it but you can check the class of an object via

{% if element is instance of ('craft\\elements\\Entry') %}

or via switch statement

{% switch className(element) %}
    {% case 'craft\\elements\\Entry' %}
  • Thanks Robin - that pretty much works. Follow up question to improve my own knowledge.... how would I have been able to work that out from the docs? Also, how would I query "if element is calendar entry"? – darylknight Aug 13 '18 at 12:44
  • About the docs thing: I don't know - since it is more or less basic/common PHP knowledge I'm not sure if it has so much to do with the docs. Craft docs are not supposed to cover those things. Not sure what the other thing should mean. You can always only query for one element type, there is no way to inlcude such a condition since it's always the case – Robin Schambach Aug 13 '18 at 13:54
  • Docs are only meant to cover common problems and be a reference (e.g. the className() function is documented here: docs.craftcms.com/v3/dev/functions.html#classname-object). That said we are working on a new Guides section for craftcms.com for less common things, and this could very well be something we put in there. – Brandon Kelly Aug 13 '18 at 15:42
  • Thanks you two - yeah, the functions reference is handy. Robin - worked it out. What I was testing for other than Entry was {% case "Solspace\\Calendar\\Elements\\Event" %}, which I found using entry.className – darylknight Aug 14 '18 at 6:31

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.