1

I'm working on a site with a structure containing products that are nested within "category"(not actually using craft's categories, just an entry type) pages. On each category or sub-category page I display each product within as a card with an image and title. The image is selected/uploaded from an asset field on the entry.

In the interest of making the image folders as clean as possible I'm trying to set the asset upload locations based on the path of the entry it is uploaded from.

For example: I have a product at the following location: mysite.dev/products/doors/wood. When I upload the image to be used in the product list view I'd like the path to be mysite.dev/assets/img/products/doors/wood/imagefilename.jpg.

My default image upload location is set to mysite.dev/assets/img/.

Is there a simple method I have missed in the documentation to pull the path of the entry as the asset field upload location?

Or is it necessary to make a loop to go through the parent entries' slugs one by one, and if so what is my point of reference when creating the loop? Am I acting on the asset or the entry when I use a variable like {slug}?

I'm tinkering with it and testing, but it is tedious since I must upload the image each time I try something new, and I feel like I might have missed something in the documentation.

Edit:

After a bit more tinkering I was able to get something that almost works. By setting upload location with the Images location in the dropdown and then {parent.uri}/{slug} I get something like mysite.dev/assets/img/doors/wood/imagefilename.jpg. This is missing the structure slug products at the beginning.

2

To include the handle of the slug, and the path within the structure based on the entry uploaded from in the upload location field I've found the following to work:

{section.handle}/{parent.uri}/{slug}

If there are any gotchas on this let me know and I'll edit this answer to include them.

  • 1
    Looks like we both came to the same conclusion at the same time. Looks like that should work! – Cavell Blood May 1 '17 at 15:08
2

Assuming your Structure section is named Products with a handle of products you could try this:

{section.handle}/{parent.uri}/{slug}

Or you would have to restructure your entries to have a parent entry of Products.

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.