<p>It seems that the <code>??</code> operator or the <code>is defined</code> operator do not work at that level, for a reason unkown to me.</p>

<p>I found a workaround: testing against a property which I know will always be there. In our case, we know that the field will only be assigned to a category or an entry. Thus, we can check if the element type is a category and use the group handle as subfolder or the entry's section handle if it is an entry:</p>

<pre><code>{% if object.elementType == 'Category' %}{group.handle}{% else %}{section.handle}{% endif %}
</code></pre>

<p>I don't know if it is the best solution, but it works.</p>

CC BY-SA 3.0
2017-09-20T11:58:14.410Z
This sadly doesn't work any longer in the new Craft 3. It'd be nice to find a solution that did. For now I've created one field for my categories and one for my entries.
ianp
CC BY-SA 3.0
2017-06-12T13:51:07.280Z
2017-06-12T13:51:07.280Z
kant312
<p>I have a field named <code>image</code> that is assigned to an entry as well as a category. </p>

<p>When a user edits an entry, I would like the subfolder to be named after the section.handle property. </p>

<p>When a user edits a category, I would like the subfolder to be named after the group.handle property.</p>

<p>Is there a way to achieve this? I've tried something like this:</p>

<pre><code>{{ section.handle ?? group.handle }}
</code></pre>

<p>...but unfortunately it does not seem to work.</p>

<assets><subfolder>
In an Asset field, how to use a dynamic subfolder field value that is dependent on the element type?
# In an Asset field, how to use a dynamic subfolder field value that is dependent on the element type?

(Asked by: username_0 on 2017-05-30)

I have a field named image that is assigned to an entry as well as a category. 
When a user edits an entry, I would like the subfolder to be named after the section.handle property. 
When a user edits a category, I would like the subfolder to be named after the group.handle property.
Is there a way to achieve this? I've tried something like this:
{{ section.handle ?? group.handle }}

...but unfortunately it does not seem to work.

**Tags:** assets, subfolder


---

## 1 answers

### Answer by username_0 on 2017-06-12. Score: 1 | Accepted answer

It seems that the ?? operator or the is defined operator do not work at that level, for a reason unkown to me.
I found a workaround: testing against a property which I know will always be there. In our case, we know that the field will only be assigned to a category or an entry. Thus, we can check if the element type is a category and use the group handle as subfolder or the entry's section handle if it is an entry:
{% if object.elementType == 'Category' %}{group.handle}{% else %}{section.handle}{% endif %}

I don't know if it is the best solution, but it works.

> **Comments:**
> **username_1**: This sadly doesn't work any longer in the new Craft 3. It'd be nice to find a solution that did. For now I've created one field for my categories and one for my entries.  

---

