Drupal Upload Image File Folder Naming Conventions
When upgrading an one-time site or migrating to a new platform, file management is 1 of those tasks that either gets ignored or unearths old habits. Throughout the years, site managers have accommodated typical requests like "Tin can you lot make this PDF bachelor for download on the website?" This is a simple task, but it can cause issues years from at present when file versioning and links in a WYSIWYG editor are thrown into the mix.
How can site managers enforce strict organizational guidelines across dozens, hundreds, or thousands of users? The default solution is to push this file mess to the sites/default/files
directory and non go too concerned over how this directory grows over time.
By organizing your folder structure and defining a file naming convention, you can maintain the site for years to come. And so let's embrace our organizational side and put some effort into a better hereafter of managing user-uploaded files.
Modules and Organizational Structure
When Media was added to the Drupal 8.5.x release, it was a game changer for storing and managing user-facing files. It's highly recommended that y'all follow the "Creating and configuring Media Types" guide published on Drupal.org unless you lot have specific apply cases.
File (Field) Paths is my module of pick. It handles binder structure and file renaming via tokens. Every bit of September 2020, it is nevertheless in beta, but it is actively existence maintained with a strong attempt to get a stable release out the door.
How to Ascertain the Organization
When creating media, we'll have "Title" and "File Upload" fields, but we'll need to add two more than fields to achieve the ideal organizational structure. I suggest adding two term reference fields to assistance categorize media and another to tag items in a non-hierarchical manner.
"Category" vocabulary should exist hierarchical and is the base for the new folder structure. Past using taxonomy, we have an easy-to-use UI for defining a dynamic folder construction without the demand for an FTP client. Category terms may be user facing or have some additional meaning to the organization. Examples include departments for higher educational activity sites, sections for publishing sites, or production categories for e-commerce.
The "Tags" field is for internal employ only and should just be used to find media easier. More on this to come in Role 2 of this article.
File Naming Conventions
Next, nosotros need to address the file name itself. Consistency should exist the goal when working with lots of users uploading files. At a minimum, rename any uploaded file every bit an all-lowercase, no-spaces version of the value of the "Title" field that is transliterated into the United states-ASCII alphabet. Transliteration is the procedure of taking letters or characters that be in other languages and replacing them with their English equivalent. This process ensures that we practise not have to account for special characters, browser compatibility in those languages, and the complexity of sorting characters from different languages. Depending on the site's needs, it might brand sense to add a date or the username of the person who uploaded the file.
Setting Up the Organization
- Download and install the File (Field) Paths module. It does not accept whatever dependencies then this process is pretty straightforward. For reference, hither is Drupal's guide to installing modules.
- Create the ii taxonomy vocabularies outlined higher up.
- Change an existing media entity or create a new one and so it has 2 term reference fields pointed at the new vocabularies. The user should only be able to reference one term from the category vocabulary and unlimited terms from the tag vocabulary.
File Path Configuration
Once y'all accept File (Field) Paths enabled, at that place volition be a new configuration option for every instance of a File or Paradigm field. This configuration should exist consistent across all media types.
First, we'll define the File path by using tokens:
[media:field_media_category:entity:parent:parent:parent:parent]/
[media:field_media_category:entity:parent:parent:parent]/
[media:field_media_category:entity:parent:parent]/
[media:field_media_category:entity:parent]/
[media:field_media_category]
Permit me intermission down what we're doing here, starting from the last token. [media:field_media_category]
tells Drupal to create a new folder within sites/default/files
and use the term referenced.
The second part of this configuration is the parent construction. If the term referenced is at a second level, we want Drupal to create two folders: Commencement will be the term's parent and then the bodily referenced term. This is where the [media:field_media_category:entity:parent]/
values come into play. Chaining this token with an added :parent
argument allows the binder structure to include multiple parent levels. In my example above, I wasn't certain how deep my category tree would get, but I didn't anticipate information technology being deeper than four levels. Your situation may be different, so be mindful of how this structure could expand over time.
Below the path tokens, at that place are 3 additional options that help make clean up the tokenized output. Select all of these options and the folder construction volition output a browser-friendly path that matches the vocabulary construction. The binder names will be transliterated with the actress slashes between not-real parents removed. The result: Files stored at any level of the hierarchy.
Renaming the File
For the file name, nosotros'll use the title of the media and its original extension: [media:name:value].[file:ffp-extension-original]
. This cleans up file names in example users upload files to the "Biological science 101 Syllabus" media entity with names similar "Biology-101 Syllabus-Semester-ane 2020.pdf." This example transforms to "biology-101-syllabus.pdf."
Boosted Configuration
Lastly, as media files are revised and potentially recategorized, I want Drupal to reflect these changes here. By enabling "Replace Existing Files," I can be assured that the latest file is always existence used. If I accept a media entity with the name "Biology Course Syllabus" and upload a new file with the aforementioned name, or fifty-fifty an entirely different name, it will automatically replace the correct file. If a content admin changes the referenced category, it volition move the file to a different folder. I also recommend calculation the "Create Redirect" option then existing links that were manually created still bespeak to the right file.
Quick tip: There are a lot of taxonomy term field widgets available on Drupal.org. Here are two I recommend:
- The Client-side Hierarchical Select module works well with the category field. It allows the content editor to choose parents or hands find child terms without the ambiguous nature of an autocomplete widget.
- The Autocomplete Palatial module is hard to beat when it comes to tagging content. The content editor can reference existing tags or create new ones on the wing.
What We've Accomplished
As your site grows, your files folder volition remain organized intuitively and consistently across users. By integrating a taxonomy vocabulary as the folder structure, you can grow and change this organization every bit time goes on.
If your site's files directory is disorganized and y'all need some help, contact u.s.a. about our support offerings and how we can aid.
What's Next?
In Office 2 of this article, I'll explore improving the admin UI. This helps your users find files easier and encourages media reuse instead of repeatedly uploading files.
Source: https://www.fourkitchens.com/blog/development/better-file-management-drupal-8-drupal-9-part-1/
0 Response to "Drupal Upload Image File Folder Naming Conventions"
Post a Comment