annotate core/modules/file/file.api.php @ 19:fa3358dc1485 tip

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