0

This is my setup:

Content type Media contains next fields:

  • "Audio" filed with upload directory to: media/books (Audio field is provided by Audiofiled module)
  • "Book" field term reference

The "Book" vocabulary contains plus one text field called "Folder" that contains folder name for each book. I want to use that field value to append it to the file path for uploading the audio file in that subfolder.

It uploads the file in the temporary folder but doesn't to the proper one, e.g. if for the "Book" term "Book 1", "Folder" field value is "book_1", I want the audio files to be uploaded in the "media/books/book_1" folder.

I am using File (Field) Path module and I am not sure how to configure it properly. In my "Audio" field I set the File path setting to

"media/books/[node:field-book:field-folder]"

But nothing is happening. Maybe I don't use the right token or something else is the problem?

apaderno
  • 28,547
  • 16
  • 75
  • 90
old-willow
  • 111
  • 1
  • 12

1 Answers1

1

You don't need "folder" field in your vocabulary. Problem here is that Drupal's core file module doesn't support entity based tokens.

For this you need to install few modules. You need entity tokens module (it comes with entity API module) and you already have file paths module.

  1. Install entity API module and enable Entity tokens
  2. Set your file directory path for audio field to: media/books
  3. After you set default path for file directory enable File (Field) Paths in audio field in your media content type
  4. Change your file path there in: media/books/[node:field-book]

Now you should have what you want.

When you select file for upload and click upload button Drupal can't take path from reference term because value you selected there isn't parsed yet via Drupal core. Even when you try to save content without previous clicking on upload button Drupal core will first try to upload selected file but it will not know destination.

File (Field) Paths module temporary upload file on default folder destination (because of that you need to set the path to that directory without tokens - in your case /media/books). Once you save the node and Drupal is provided with the Node tokens values the file is moved on desired destination (in your case /media/books/token_values).

Addition:

In line:

media/books/[node:field-book]

token value is taken from book field from your media type which is defined by your vocabulary. Rename token values means to rename your taxonomy terms into names related to your books. If you already have folders on server that you can't rename then it is good choice to have another field in your vocabulary.

File (Field) Path in audio field in your media content type should be:

 media/books/[node:field-book:field-folder]

So if you set your folder values book_1 for Book 1, book_2 for Book 2, etc...

When you select "Book 1" during upload you should be able to upload files in

media/books/book_1 

or

media/books/book_2

Note: Drupal will not delete created folders if/when you change values of tokens.

Hope this helps.

Darko
  • 396
  • 3
  • 9
  • thank you for your time and effort to answer the question. This is a progress! But still not getting exactly what I want. e.g. My book name is "Book 1" and this is a taxonomy term name, but the folder that I have already in /media/books is called book_1. I need to upload my mp3 file to book_1 folder. After I did what you suggested drupal creates new folder called Book 1 in my media/books folder. That was the reason why I created field Folder in may taxonomy vocabulary. Each book has a regular title and its folder which differs for the purpose of sorting visually. – old-willow Apr 16 '16 at 15:28
  • I don't get what you want...Please be more specific... You wanted to upload files in "media/books/book_1" folder. Instead you got here files uploaded in media/books/Book 1. So rename token values? Book 1 into book_1. You can have that filed "Folder" in vocabulary but i don't understand why you need it for uploading files... – Darko Apr 16 '16 at 16:17
  • My logic was that since the book titles are different from theirs subfolder names (folders are already on server), I wanted to store the appropriate folder name in a field for each book, so that I can use it to append to media/books path when I add a node, through tokens. That was the reason for my Folder field existence. But I understand that I don't need it beacuse of your explanation that drupal is not aware of that value when the node is saving/saved. I have about 100 books and names of books are not the same as subfolder names. How to say to drupal where is the folder for particular book? – old-willow Apr 16 '16 at 17:09
  • I am not allowed to rename subfolders. What do you mean by "So rename token values?" I don't understand that question. Hopefully I am a little bit clearer... – old-willow Apr 16 '16 at 17:12
  • I understand. I thought you want to build sorting on server where data will be added after developing... I post addition in my answer, hope that helps. – Darko Apr 16 '16 at 17:56