Drupal investigation

file.api.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @file
  4. * Hooks for file module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Check that files meet a given criteria.
  12. *
  13. * This hook lets modules perform additional validation on files. They're able
  14. * to report a failure by returning one or more error messages.
  15. *
  16. * @param \Drupal\file\FileInterface $file
  17. * The file entity being validated.
  18. * @return array
  19. * An array of error messages. If there are no problems with the file return
  20. * an empty array.
  21. *
  22. * @see file_validate()
  23. */
  24. function hook_file_validate(Drupal\file\FileInterface $file) {
  25. $errors = [];
  26. if (!$file->getFilename()) {
  27. $errors[] = t("The file's name is empty. Please give a name to the file.");
  28. }
  29. if (strlen($file->getFilename()) > 255) {
  30. $errors[] = t("The file's name exceeds the 255 characters limit. Please rename the file and try again.");
  31. }
  32. return $errors;
  33. }
  34. /**
  35. * Respond to a file that has been copied.
  36. *
  37. * @param \Drupal\file\FileInterface $file
  38. * The newly copied file entity.
  39. * @param \Drupal\file\FileInterface $source
  40. * The original file before the copy.
  41. *
  42. * @see file_copy()
  43. */
  44. function hook_file_copy(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source) {
  45. // Make sure that the file name starts with the owner's user name.
  46. if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) {
  47. $file->setFilename($file->getOwner()->name . '_' . $file->getFilename());
  48. $file->save();
  49. \Drupal::logger('file')->notice('Copied file %source has been renamed to %destination', ['%source' => $source->filename, '%destination' => $file->getFilename()]);
  50. }
  51. }
  52. /**
  53. * Respond to a file that has been moved.
  54. *
  55. * @param \Drupal\file\FileInterface $file
  56. * The updated file entity after the move.
  57. * @param \Drupal\file\FileInterface $source
  58. * The original file entity before the move.
  59. *
  60. * @see file_move()
  61. */
  62. function hook_file_move(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source) {
  63. // Make sure that the file name starts with the owner's user name.
  64. if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) {
  65. $file->setFilename($file->getOwner()->name . '_' . $file->getFilename());
  66. $file->save();
  67. \Drupal::logger('file')->notice('Moved file %source has been renamed to %destination', ['%source' => $source->filename, '%destination' => $file->getFilename()]);
  68. }
  69. }
  70. /**
  71. * @} End of "addtogroup hooks".
  72. */