diff --git a/core/modules/file/file.api.php b/core/modules/file/file.api.php index c0b0d52..8414e4a 100644 --- a/core/modules/file/file.api.php +++ b/core/modules/file/file.api.php @@ -97,7 +97,17 @@ function hook_file_insert(Drupal\file\File $file) { * The file that has just been updated. */ function hook_file_update(Drupal\file\File $file) { - + // Loads the file owner user object. + $file_user = user_load($file->uid); + // Checks if the user name is contained at the beginning of the uploaded file. + if (strpos($file->filename, $file_user->name) !== 0) { + $old_filename = $file->filename; + // Adds the user name at the beginning of the file name and saves a copy. + $file->filename = $file_user->name . '_' . $file->filename; + $file->save(); + // Logs the event. + watchdog('file', t('%source has been renamed to %destination', array('%source' => $old_filename, '%destination' => $file->filename))); + } } /** @@ -111,7 +121,16 @@ function hook_file_update(Drupal\file\File $file) { * @see file_copy() */ function hook_file_copy(Drupal\file\File $file, Drupal\file\File $source) { - + // Loads the file owner user object. + $file_user = user_load($file->uid); + // Checks if the user name is contained at the beginning of the uploaded file. + if (strpos($file->filename, $file_user->name) !== 0) { + // Adds the user name at the beginning of the file name and saves a copy. + $file->filename = $file_user->name . '_' . $file->filename; + $file->save(); + // Logs the event. + watchdog('file', t('Copied file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->filename))); + } } /** @@ -125,7 +144,16 @@ 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) { - + // Loads the file owner user object. + $file_user = user_load($file->uid); + // Checks if the user name is contained at the beginning of the uploaded file. + if (strpos($file->filename, $file_user->name) !== 0) { + // Adds the user name at the beginning of the file name and saves a copy. + $file->filename = $file_user->name . '_' . $file->filename; + $file->save(); + // Logs the event. + watchdog('file', t('Moved file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->filename))); + } } /**