Drupal Upload File to Folder With Date Name

Don't forget to subscribe to our YouTube channel to stay upward-to-date.

File Management Series

Drupal doesn't back up the ability to replace existing files. You can create and delete files, just you can't supersede a file without using a module. If you try to upload a file with the same name Drupal volition suspend "_0", "_1", etc… to the filename and increase it.

Luckily two modules can help with replacing files, and they're called Media Entity File Replace and File Supervene upon.

What is the primary deviation between Media Entity File Supplant and File Supplant?

Both modules will supercede "files" whilst retaining the original file's filename by performing an "overwrite" function in the backend. In Drupal, an uploaded "file" can be a "media entity" or a "file entity".

As the module names advise, the primary departure is that Media Entity File Supplant works at the media entity level, whereas File Supercede works at the file entity level.

This module allows editors to supplant files at the media entity level by overwriting the existing media files. Because information technology overwrites the files, the filename and path are exactly retained.

You should use this module to allow content editors to replace media entity files whilst keeping the same filename and path every bit the original file.

Getting Started

1. Download and enable the module. This module requires no boosted libraries and can but be installed with composer which is the recommended way.

To install using Composer:

            composer require drupal/media_entity_file_replace          

If you lot get the post-obit error when using Composer:

                          [InvalidArgumentException]   Could not find a version of packet drupal/media_entity_file_replace matching your minimum-stability (stable). Crave it with an explicit version constraint allowing its desired stability.          

Then specify the version:

            composer crave drupal/media_entity_file_replace:^i.0-beta3          

To enable the module using Drush:

            drush en media_entity_file_replace          

ii. Clear the cache. Y'all can easily clear the cache via the Drupal admin UI at admin/config/development/operation or by the drush command:

            drush cr          

A quick overview of this footstep is adding a media field to an existing content type and uploading an actual media document. If y'all already take a media field in your content type and an uploaded media file and then you can skip to step 3.

A. For whatsoever content type (nosotros will utilize the default Drupal Article content blazon in this tutorial), add together a Media field and check "Certificate" equally the Reference type. See Image one below.
B. Become to Content >> Media and click on "Add together Media". At the next folio, select "Document" and go on to upload a file. Note the filename.

Paradigm i – Enable "Document" reference type for the newly created Media field in the Commodity content type

3. Now enable the "Replace file" widget that comes from the Media Entity File Replace module.

Become to Structure >> Media Types >> Document >> Manage Course Brandish and enable the "Replace file" widget. This widget is disabled past default. Come across Image 2 below. You will not exist able to supercede a file until you enable this widget.

It's a expert idea to position the "Replace file" widget right nether the file widget equally shown in Epitome 2 and then that when a user is editing a file, the "Supercede file" widget is right under the file that they desire to supercede.

Prototype 2 –  Enable the "Replace file" widget in the Document media type found under "Manage Form Brandish".

Without this module and widget enabled, the default Drupal interface will appear as in Prototype 3.

Prototype three – The default Drupal viii interface to replace a media file.

iv. Now if you get to Content >> Media and edit the document media that we uploaded in Step 2.B, you will see the "Supersede file" widget as shown in Image 4 beneath. Discover that the default Drupal widget is automatically removed (see the difference between Image 3 and Image 4).

Epitome four – The "Replace file" widget coming from the Media Entity File Replace module

5. Go ahead and supercede the original uploaded file with a new file with a different filename. Upon replacing the file, if you left the default option "Overwrite original file" checked, then Drupal will upload the new file and fix the quondam file for deletion. Even if your new file has a new file name, Drupal will shop the new file whilst retaining the filename of the original file. Note that the filename is the same as that noted from Step 2.B.

If you click on the new uploaded file even later on doing a refresh in the browser, you may nevertheless see the old content of the original file. You must do a hard refresh in your browser to run across the contents of the newly uploaded file.

More information: How to practice a hard refresh in Chrome, Firefox and IE.

File Supervene upon

This module allows editors to replace files at the file entity level by overwriting the existing files. Because it overwrites the files, the filename and path are exactly retained.

You should use this module if y'all desire to allow content editors to supersede file entities whilst keeping exactly the aforementioned filename and path as the original file. It is useful in cases where existing files in Drupal need to be updated occasionally.

Getting Started

i. Download and enable the module. This module requires no boosted libraries and can simply be installed with composer which is the recommended mode.

Using Composer:

            composer require drupal/file_replace          

To enable the module using Drush:

            drush en file_replace          

2. Articulate the enshroud. Y'all can easily clear the cache via the Drupal admin UI at admin/config/development/performance or by the drush control:

            drush cr          

3. In that location is ane extra manual step that you must do. Considering this module merely provides the "Replace folio" for each file upload, it does not provide a link to this folio from the Drupal UI. You tin can access the "Supervene upon page" directly past typing the URL into the browser. You can manually type the post-obit URL into the browser:

            admin/content/files/replace/{{ fid }}          

Where {{ fid }} is the file id of the file you desire to supersede. For instance, if the fid was 6, then you would go to:

            admin/content/files/supersede/half-dozen          

This is not ideal. Nosotros volition prove you how to link to the page from the default Drupal Files overview folio. (The consequence we are trying to achieve is shown in Paradigm 11.)

Go to Content >> Files and click on "Edit view" as shown in Image v.

Prototype 5 – On the Files overview page, click on the Edit icon to the peak right to edit the View.

Alternatively, you can become to Structure >> Views and edit the Files view as shown in Image six.

Prototype 6 – Editing the Files view establish on the View listing page.

iv. Add a Custom text field to the Files View every bit shown in Image seven.

Image 7 – Adding a Custom text field to the Files view.

five. Then add some custom text for the text of the link. See Image 8

Prototype 8 – Adding custom text for the link anchor text

Also, edit the "Rewrite Results" department and enter the post-obit in the "Link path":

            admin/content/files/supersede/{{ fid }}          

You lot can as well enter custom text in the "Championship text" field although this is just for aesthetic purposes. See Prototype 9 below.

Paradigm 9 – Rewriting the results of the link to include a relative path.

At present click on "Apply (all displays)" and save your View.

6. The last pace is to enable the "Supervene upon files" permission for the role that you desire. See Image x.

Epitome 10 – Enable the permission "Replace files" for your function.

7. At present go to the Files overview page at Content >> Files and you will see a "replace" link equally shown in Epitome 11.

Image xi – Showing the custom "supersede" link created from editing the Files view

Click on the "replace" link adjacent to the file you desire to replace and this will have you to some other page where you can upload a new file as shown in Prototype 12.

Image 12 – The "Supervene upon file" page provided by the File Supervene upon  module.

Common Pitfall – Caching

Static files served by Drupal are usually cached externally (outside of Drupal) by services such every bit Varnish, Content Delivery Network (CDN) and the user'southward browser. By default, a user's browser volition not option up the content of the newly uploaded file until subsequently the max-age header which is fix in .htaccess for 2 weeks for static files. In other words, if you lot supervene upon a file and do nothing, terminate users volition see the new file content after two weeks (unless they locally do a hard refresh).

Immigration Drupal's internal cache will non solve this caching caveat considering Drupal is not involved with serving static files. This is the job of your webserver.

In that location are a few options worth mentioning:

  • Read the section on Caching on Media Entity File Replace module'south homepage (two options are listed)
  • After y'all supersede a file, manually articulate your Varnish/CDN/CloudFlare enshroud (not ideal but it works).
  • Follow the give-and-take to integrate with the Purge module

Summary

Drupal does not retain filenames for uploaded files that are replaced or overwritten. Additionally, Drupal stores uploaded files as either media entities or file entities depending on your set upwards. This tutorial demonstrated how to retain existing file names when replacing media or file entities. There are two contributed modules yous tin apply to solve this problem and they are Media Entity File Replace and File Supercede. These modules will allow you to overwrite existing media and files respectively, whilst keeping their original filenames.

hannafordtiolsell.blogspot.com

Source: https://www.webwash.net/how-to-replace-files-in-drupal/

0 Response to "Drupal Upload File to Folder With Date Name"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel