Commit d6ada284 authored by jhodgdon's avatar jhodgdon

Issue #1173322 by javier_: Add function bodies to file hook documentation

parent af230648
......@@ -97,7 +97,15 @@ function hook_file_insert(Drupal\file\File $file) {
* The file that has just been updated.
*/
function hook_file_update(Drupal\file\File $file) {
$file_user = user_load($file->uid);
// Make sure that the file name starts with the owner's user name.
if (strpos($file->filename, $file_user->name) !== 0) {
$old_filename = $file->filename;
$file->filename = $file_user->name . '_' . $file->filename;
$file->save();
watchdog('file', t('%source has been renamed to %destination', array('%source' => $old_filename, '%destination' => $file->filename)));
}
}
/**
......@@ -111,7 +119,14 @@ function hook_file_update(Drupal\file\File $file) {
* @see file_copy()
*/
function hook_file_copy(Drupal\file\File $file, Drupal\file\File $source) {
$file_user = user_load($file->uid);
// Make sure that the file name starts with the owner's user name.
if (strpos($file->filename, $file_user->name) !== 0) {
$file->filename = $file_user->name . '_' . $file->filename;
$file->save();
watchdog('file', t('Copied file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->filename)));
}
}
/**
......@@ -125,7 +140,14 @@ function hook_file_copy(Drupal\file\File $file, Drupal\file\File $source) {
* @see file_move()
*/
function hook_file_move(Drupal\file\File $file, Drupal\file\File $source) {
$file_user = user_load($file->uid);
// Make sure that the file name starts with the owner's user name.
if (strpos($file->filename, $file_user->name) !== 0) {
$file->filename = $file_user->name . '_' . $file->filename;
$file->save();
watchdog('file', t('Moved file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->filename)));
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment