W3cubDocs

/Drupal 8

function hook_file_copy

hook_file_copy(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source)

Respond to a file that has been copied.

Parameters

\Drupal\file\FileInterface $file: The newly copied file entity.

\Drupal\file\FileInterface $source: The original file before the copy.

See also

file_copy()

Related topics

Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/file/file.api.php, line 50
Hooks for file module.

Code

function hook_file_copy(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source) {
  // Make sure that the file name starts with the owner's user name.
  if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) {
    $file->setFilename($file->getOwner()->name . '_' . $file->getFilename());
    $file->save();

    \Drupal::logger('file')->notice('Copied file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->getFilename()));
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!file!file.api.php/function/hook_file_copy/8.1.x