Is it possible for File Entity to automatically create a redirect from the old file path to the new file path when using the Replace file function. replace file

CommentFileSizeAuthor
Screen Shot 2015-04-29 at 1.39.21 PM.png40.83 KBrovo
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

BenStallings’s picture

Status: Active » Needs review

Here is the solution I came up with, for a custom module which requires Redirect module.

function custom_module_name_file_update($file) {
  if ($file->uri != $file->original->uri) {
    $source = file_create_url($file->original->uri);
    $dest = file_create_url($file->uri);
    $redirect = new stdClass();
    redirect_object_prepare($redirect);
    $redirect->source = substr($source, strpos($source, '/', 8)+1);
    $redirect->redirect = substr($dest, strpos($dest, '/', 8)+1);
    redirect_save($redirect);
  }
}

I hope you find this helpful.

Dave Reid’s picture

Status: Needs review » Closed (duplicate)

This is a duplicate of #1899174: Automatically create a redirect when a file is saved with a new URI which is a feature request for the Redirect module itself.