comparison core/modules/file/file.api.php @ 0:4c8ae668cc8c

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