In an Asset field, how to use a dynamic subfolder field value that is dependent on the element type? 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.
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.