0

I am building a site for my school. Teachers will be able to upload their CVs. Their files will be PDFs and I need to have them in two languages (English and Greek). So I have a File field type with limited (2) allowed number of values.

It works alright, however I would like to control the filenames. Ideally I would like to use their email (username) value and add a suffix. Say if someone has the email 'myname@somewhere.com', it would be ideal to rename their 2 pdf into: 'myname_en.pdf' and 'myname_gr.pdf'.

So, no matter what filename they upload, I would like to rename it and save using the above mentioned filenames. I understand that this might be tricky, since the email might also being given at the same time (when creating new content of type 'staff'). However, email value is required (CV is not) so can I safely assume that I have the username portion of the email value? How can I get that substring and use it to 'create' my filenames?

I am not sure such a thing is possible, I guess it might be.

I tried to search around, but found no definite solution.

This thread seems relevant, however the chosen answer points to two other, also relevant threads.

The first one is quite old (May 2012), and has no chosen answer.

The second one is from 2014, and also has no chosen answer.

I couldn't find anything else ..

I 'm new to Drupal, being using it for a couple months. I 'm still fighting my way out of the Desert of Despair, so please show some understanding.

I feel fairly confident with twig and templates, but haven't really grasped the idea of pre-hook functions (?). I have a feeling this is a case where I need to use them ...

There is also mention of a relevant contrib module, specifically this: https://www.drupal.org/project/filefield_paths

However, it's still in beta version for D8, released in December 2015, and its development version hasn't been updated since March 2016.

I couldn't find any other info ..

Finally, perhaps I need to mention that I have changed the default directory for all uploaded CVs, they 're all kept under a non-time specific dir with a simple title, like 'Teachers-CVs'.

Thanks for your time - any help would be greatly appreciated...

  • The problem with your approach is if someone uploads files later with the same name, the names will be deduped (made unique) in Drupal, ex. myname_gr-1.pdf. Otheriwse files would be unintentionally replaced. I ran into a similar problem where a client never wanted the URL to change even when changing the file over time, and I wound up implementing a permanent URI slug via drupal.org/project/media_entity_download – Kevin Mar 29 '18 at 19:14
  • I have already seen this in my testing, cause I 've created a few teachers and I always upload the same files for testing (cv_en.pdf and cv_gr.pdf), so they end up as cv_en_0.pdf, _1.pdf etc. I dont really care if files get overwritten, I actually want them to be overwritten, as they might update something in their CV and re-upload, so I guess its fine to over-write old file with newer.. So every teacher will always have 2 PDFs, with the right filenames and _en, _gr suffixes. Would that make it easier/possible to implement? – thomas Mar 29 '18 at 19:19
  • Thanks for the info. Could you please give me a little bit of help; where do I use the code in your link? I guess its a pre_process function. Should I give the exact name (so as to override an already defined function?). Any direction/help in order understand how to use that code and where, would be very helpful .. – thomas Mar 30 '18 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.