I have 2 sections, events and activities, that share many fields. But activities doesn't have an eventLevel field as there isn't a level. eventLevel is a radio fieldtype. So I want to insert the eventLevel when it exists and if it doesn't just insert the text "Activity". I've tried various tests on if eventLevel is defined, is null, and length. If there is an eventLevel it displays, but if it's an activity I end up with an empty cell.
{% set events = craft.entries.find({section:'events,activities', eventDate : ">= " ~ now.w3cDate() , order:'eventDate'}) %}
{% if events %}
{% for entry in events %}
<tr>
<td class="col-date">{{ entry.eventDate.format('D j M') }}</td>
<td><a href="{{ entry.url }}" title="More info about {{ entry.eventName }}">{{ entry.eventName }}</a></td>
<td>{{ entry.nearestTown }}</td>
<td>{% if entry.eventLevel is defined %}{{ entry.eventLevel }}{% else %}Activity{% endif %}</td>
</tr>
{% endfor %}
{% endif %}
I've also tried the twig default filter with the same result:
{{ entry.eventLevel|default('Activity') }}
entry[eventLevel] is defined and entry.eventLevel != ''– Mats Mikkel Rummelhoff Feb 19 '16 at 12:27if entry.eventLevel is definedtest won't work in this case, because it is truthy even if the field isn't attached to this entry type's field layout but to any other entry type. A simpleif entry.eventLeveltest should work though. – carlcs Feb 19 '16 at 13:03if entry.eventLevelorif entry.eventLevel is nullshould work and theis definedtest isn't necessary. There's one exception though, which is if you had this field attached to section B and later removed it, because removing a field doesn't delete the values from the db. Maybe this is the case here? – carlcs Feb 19 '16 at 16:47eventLevelattribute won't exist, and attempting to access a non-existing attribute isn't allowed (even as a part of a conditional). See this page for reference. I'd recommend turning ondevModefor your dev environment if you haven't; it'll make debugging your code a lot easier. – Mats Mikkel Rummelhoff Feb 19 '16 at 16:52{% if entry.attributeThatDoesntExist is defined %}will always evaluate totrue(you can test this with{{ dump(entry.attributeThatDoesntExist is defined) }}). I don't know exactly why it this is, and I admit it seems less than logical/intuitive, but the gist is that whenever an attribute may or may not exist, you have to use brackets notation to test for it before attempting to access the attribute using dot notation. – Mats Mikkel Rummelhoff Feb 19 '16 at 16:59