1

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.

1

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.

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.
    – ianp
    Sep 20 '17 at 11:58

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.