How to dynamically change the path to store commerce files depending on some product variables? All my Drupal Commerce files are being saved in the same directory on the server.

I have a Commerce product variation of type of album. Each album contains multiple Commerce files (mp3’s, m4a’s). I want each album to have its files saved in a different directory depending on artist name and album name.

For example: commerce_mp3s/artist_name/album_name/album_files*

I tried looking in the DB where these Commerce files are referenced, table name being field_data_commerce_file, but file paths are not used there.

The worst case scenario would be to change the file path of the content type every time I add a new album, but that’s not a solution. This must be a common scenario. 

How can I accomplish this?
You need to use the Token with File (Field) Paths and Pathauto modules.


  Note: File (Field) Paths extends the token module by adding Node tokens, without this module, you will not be able to use the node fields as tokens.


Then edit your file field.



We then use Pathauto to fix names with spaces. Ex: If the album name is "Hello World" the path/folder name will change to /hello-world/