Experimental project

This is a sandbox project, which contains experimental code for developer use only.

file_replace screenshot

Sometimes an editor just needs to replace a file, nothing more and nothing less.

This utility module offers the ability for managed files (file and image field files) to be replaced without changing the path to the file and without the need for additional admin and editorial interfaces. It adds a "Replace" option on each file field widget that triggers a simple modal file replace upload form via AJAX. The replacement file assumes the exact same path, physical file name and file ID as the original. Any validation rules enforced by the file field are also re-enforced for the replacement.

Why is this useful?

When uploading files via a file field widget Drupal enforces a unique file name for each new file, which makes it difficult to force an in-place update later. Drupal does this for a number of very good reasons (avoiding accidental overrides, compatibility with entity revisions, etc.). However, once a file is uploaded there are some cases where an in-place replacement is useful, notably if you have inline/incoming links pointing directly to the file's path. So if your use case requires that your physical file paths act as permalinks this module may help with your file management workflow.

Of course there are other ways to solve this problem, some of which may be much better options depending in your use case:

  • The File Entity module (often used in conjunction with the Media module) abstracts files as entities and also allows the physical file associated with that entity to be replaced on demand. Anyone with modest-to-serious file management needs is almost certainly best-served by this option. However, going this route often requires subscribing to a new (and powerful!) ecosystem of media management practices which may not be necessary for more simple file management use cases.
  • The Upload File Replace module does some clever file renaming after a file field is updated. When a file name collision is detected it ensures the newest file retains the original name and alters the older one instead. This may be a great option for many but it does not give the editor explicit control over the replacement action via the UI, which could prove clunky for some users or problematic for others that are expecting the default Drupal behavior.
  • The IMCE module (and others like it) allow direct GUI access to parts of Drupal's file directory tree, and includes options to overwrite a file on upload. This is great for administrators but it requires a separate set of interfaces and is not always ideal for any site editors who don't want to navigate a file browser.

Notable Limitations

The following limitations are important when considering if this approach makes sense for a use case:

  • File replacements will not be tracked via node/entity revisions. When a file is replaced, the physical file is replaced, nothing more. The module is "contextually" aware of what field the file is associated with (it re-enforces the field's upload validation rules, etc.) but it does not make any changes to the way the file is used within Drupal.
  • File replacements cannot be undone. Since the file path and file ID are maintained after the replacement, no content from the old version will remain. It's just as if you overwrote the physical file in the appropriate files directory on the server.

So if your file management strategy is tightly linked to Drupal's entity revisions system, this module may not make much sense for you. That said, you can limit access to this replace option on a role-by-role and field-by-field basis, so it could be used in a very targeted way within a site that also leverages other file management strategies.

Installation and Usage

  1. Install and enable the module as usual. Note that the ctools module is a dependency, though chances are reasonably high that you already have that installed.
  2. Visit your permissions settings at /admin/people/permissions and grant the "Replace Files" permission (under the "In-Place File Replace" heading) to the roles that should have the ability to replace files.
  3. Users with correct permissions will now see a "Replace" button for each file/image field entry that will trigger the file replace modal form.

By default the replace option will be active for all file and image fields that use generic file or image widgets. You can further disable the feature on a field-by-field basis by simply adjusting the relevant field instance settings (via "Structure" > "Content Types" > Your Type > "Manage Fields" tab > "Edit" link on the relevant file or image field > "Show File Replace Button" checkbox).

Supporting organizations: 
Devlopment support

Project information