2

I'm having some difficulty with "Restrict uploads to a single folder?" option on an Assets entry field. I find that when I upload an asset into the field, the asset folder gets created as expected, but the asset does not end up in the folder.

For example, if the author is SomeAuthor, a folder called 'SomeAuthor' gets created in the correct location in the Asset Source using the following dynamic path, set in the field setting as mentioned above:

{% if owner is defined %}{owner.author.username}{% elseif author is defined %}{author.username}{% endif %}

I'm using this to allow me to re-use the field on Matrix and non-Matrix sections, but the point is that the folder is created successfully. What isn't so successful is that the file does not end up inside that folder, but in the root of my Asset Source. I've tried combinations of logging in as the specified author, removing all the assets from the site. Re-indexing etc. etc. all to no avail.

Perhaps I'm looking in the wrong place.

Does anyone have any experience trying to achieve what I'm attempting?

The general context is I have separate users on the system, each responsible for a "project" page, along with permissions to create 'articles' and 'events' in the respective sections/channels. I want to have one asset folder per user, so that no matter where they upload assets, the assets end up in the same folder per user, so that user with username 'SomeAuthor's assets will end up in 'SomeAuthor' folder within my single site source. I am attempting to do this using this snippet above, which in theory should work considering the author.username is not changing...

EDIT

Since posting this, I have discovered that I had another assets field with a simpler "Restrict uploads to a single folder?" setting: {author.username|lower} that was creating the field, and not the field I was testing with the setting above! Doh...

I supposed the real question is, therefore, how can I get a dynamic path, like the one above, to work?

3

Whenever you're not using the shortcut syntax with single braces you need to acces the element via object.

So add that var before owner etc. and you should be good.

One thing I noticed is that you can't use ternary syntax for conditionals within short syntax either but with Twig code you can:

{{ object.owner.author ? object.owner.author.friendlyName }}
  • That's helpful, thanks. I've since realised that I don't need to re-use this field anyway. It was all a journey of discovery! – lowe_22 Mar 26 '15 at 17:43

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.