Problem/Motivation

I would like to use Taxonomy terms as folders (using Media directories.
If these terms have hierarchy, this hierarchy should be applied accordingly to the folder structure.
Example:
Term A
--Term B
----Term C

Folder structure should be -> a/b/c/file.txt

My plan is to use tokens for the parents like that:

[media:directory:entity:parents]/[media:directory:entity]

The problem is that the token replacement returns parents comma,separated - "a, b, c"

Proposed resolution

I've created a small patch that just replaces these commas with slashes, so that the terms can be used as part of the path.

CommentFileSizeAuthor
#2 taxonomy-hierarchy-folders-3182718-1.patch538 bytesyivanov
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

yivanov created an issue. See original summary.

yivanov’s picture

StatusFileSize
new538 bytes
megachriz’s picture

I wonder if this is the prefered way to fix this issue. What happens for example if you base the file path on the node title, and then use a comma in that title? I think that in such case you don't want the file to go into a subdirectory.

Example:

  1. Set "File path" to [node:title].
  2. Enable "Cleanup using Pathauto" option.
  3. Create a node titled "The Good, the Bad and the Ugly" and upload a file.

The file would then go into good/bad-and-ugly, which doesn't make sense in this context.

I think a better solution would be to use a token that returns the terms slash separated instead of comma separated.

yivanov’s picture

Thanks for your feedback, @MegaChriz.
Indeed you are right that this solution is not the best one and doesn't cover all the cases.
The main reason I added this patch is to cover my specific case and start a discussion on how this can be improved on a module level.
Do you think creating this custom token should be handled in this module or it's out of scope and each project should prepare such if they want to achieve the above case?

Another thing that comes to my mind is to use the options array and pass the join parameter there like this:

$value = \Drupal::service('token')
      ->replace($value, $data, ['join' => '/]);

The token replacement logic accepts it:

  $join = isset($options['join']) ? $options['join'] : ', ';

Then we can have a settings checkbox that says "Replace commas with slashes", similar to the "Remove slashes (/) from tokens" setting.

vivify’s picture

Could you use join paths? Similar to [term:vocabulary]/[term:parents:join-path]/[term:name]

marcoka’s picture

This looks very interestig.

@vicify
Are taxonomy terms avaliable? I can only see user tokens.

voleger’s picture

Component: Code » Compatibility

igoragatti made their first commit to this issue’s fork.

igoragatti’s picture

I've re-applied the changes so the patch continues to work.