file.api.php 8.02 KB
Newer Older
1
2
3
4
5
6
7
<?php

/**
 * @file
 * Hooks for file module.
 */

8

9
10
11
12
13
14
/**
 * Act on a newly created file.
 *
 * This hook runs after a new file object has just been instantiated. It can be
 * used to set initial values, e.g. to provide defaults.
 *
15
 * @param \Drupal\file\Entity\File $file
16
17
 *   The file object.
 */
18
function hook_file_create(\Drupal\file\Entity\File $file) {
19
20
21
22
23
  if (!isset($file->foo)) {
    $file->foo = 'some_initial_value';
  }
}

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * Load additional information into file entities.
 *
 * file_load_multiple() calls this hook to allow modules to load
 * additional information into each file.
 *
 * @param $files
 *   An array of file entities, indexed by fid.
 *
 * @see file_load_multiple()
 * @see file_load()
 */
function hook_file_load($files) {
  // Add the upload specific data into the file entity.
  $result = db_query('SELECT * FROM {upload} u WHERE u.fid IN (:fids)', array(':fids' => array_keys($files)))->fetchAll(PDO::FETCH_ASSOC);
  foreach ($result as $record) {
    foreach ($record as $key => $value) {
41
      $files[$record['target_id']]->$key = $value;
42
43
44
45
46
47
48
49
50
51
    }
  }
}

/**
 * Check that files meet a given criteria.
 *
 * This hook lets modules perform additional validation on files. They're able
 * to report a failure by returning one or more error messages.
 *
52
 * @param \Drupal\file\FileInterface $file
53
54
55
56
57
58
59
 *   The file entity being validated.
 * @return
 *   An array of error messages. If there are no problems with the file return
 *   an empty array.
 *
 * @see file_validate()
 */
60
function hook_file_validate(Drupal\file\FileInterface $file) {
61
62
  $errors = array();

63
  if (!$file->getFilename()) {
64
65
    $errors[] = t("The file's name is empty. Please give a name to the file.");
  }
66
  if (strlen($file->getFilename()) > 255) {
67
68
69
70
71
72
73
74
75
76
77
78
79
    $errors[] = t("The file's name exceeds the 255 characters limit. Please rename the file and try again.");
  }

  return $errors;
}

/**
 * Act on a file being inserted or updated.
 *
 * This hook is called when a file has been added to the database. The hook
 * doesn't distinguish between files created as a result of a copy or those
 * created by an upload.
 *
80
 * @param \Drupal\file\FileInterface $file
81
82
 *   The file entity that is about to be created or updated.
 */
83
function hook_file_presave(Drupal\file\FileInterface $file) {
84
85
  // Change the owner of the file.
  $file->uid->value = 1;
86
87
88
89
90
91
92
93
94
}

/**
 * Respond to a file being added.
 *
 * This hook is called after a file has been added to the database. The hook
 * doesn't distinguish between files created as a result of a copy or those
 * created by an upload.
 *
95
 * @param \Drupal\file\FileInterface $file
96
97
 *   The file that has been added.
 */
98
function hook_file_insert(Drupal\file\FileInterface $file) {
99
100
101
  // Add a message to the log, if the file is a jpg
  $validate = file_validate_extensions($file, 'jpg');
  if (empty($validate)) {
102
    \Drupal::logger('file')->notice('A jpg has been added.');
103
104
105
106
107
108
109
110
  }
}

/**
 * Respond to a file being updated.
 *
 * This hook is called when an existing file is saved.
 *
111
 * @param \Drupal\file\FileInterface $file
112
113
 *   The file that has just been updated.
 */
114
function hook_file_update(Drupal\file\FileInterface $file) {
115
  // Make sure that the file name starts with the owner's user name.
116
117
118
  if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) {
    $old_filename = $file->getFilename();
    $file->setFilename($file->getOwner()->name . '_' . $file->getFilename());
119
120
    $file->save();

121
    \Drupal::logger('file')->notice('%source has been renamed to %destination', array('%source' => $old_filename, '%destination' => $file->getFilename()));
122
  }
123
124
125
126
127
}

/**
 * Respond to a file that has been copied.
 *
128
 * @param \Drupal\file\FileInterface $file
129
 *   The newly copied file entity.
130
 * @param \Drupal\file\FileInterface $source
131
132
133
134
 *   The original file before the copy.
 *
 * @see file_copy()
 */
135
function hook_file_copy(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source) {
136
  // Make sure that the file name starts with the owner's user name.
137
138
  if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) {
    $file->setFilename($file->getOwner()->name . '_' . $file->getFilename());
139
    $file->save();
140

141
    \Drupal::logger('file')->notice('Copied file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->getFilename()));
142
  }
143
144
145
146
147
}

/**
 * Respond to a file that has been moved.
 *
148
 * @param \Drupal\file\FileInterface $file
149
 *   The updated file entity after the move.
150
 * @param \Drupal\file\FileInterface $source
151
152
153
154
 *   The original file entity before the move.
 *
 * @see file_move()
 */
155
function hook_file_move(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source) {
156
  // Make sure that the file name starts with the owner's user name.
157
158
  if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) {
    $file->setFilename($file->getOwner()->name . '_' . $file->getFilename());
159
    $file->save();
160

161
    \Drupal::logger('file')->notice('Moved file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->getFilename()));
162
  }
163
164
165
166
167
168
169
170
}

/**
 * Act prior to file deletion.
 *
 * This hook is invoked when deleting a file before the file is removed from the
 * filesystem and before its records are removed from the database.
 *
171
 * @param \Drupal\file\FileInterface $file
172
173
174
 *   The file that is about to be deleted.
 *
 * @see hook_file_delete()
175
 * @see \Drupal\file\FileStorage::delete()
176
177
 * @see upload_file_delete()
 */
178
function hook_file_predelete(Drupal\file\FileInterface $file) {
179
  // Delete all information associated with the file.
180
  db_delete('upload')->condition('fid', $file->id())->execute();
181
182
183
184
185
186
187
188
}

/**
 * Respond to file deletion.
 *
 * This hook is invoked after the file has been removed from
 * the filesystem and after its records have been removed from the database.
 *
189
 * @param \Drupal\file\FileInterface $file
190
191
192
 *   The file that has just been deleted.
 *
 * @see hook_file_predelete()
193
 * @see \Drupal\file\FileStorage::delete()
194
 */
195
function hook_file_delete(Drupal\file\FileInterface $file) {
196
  // Delete all information associated with the file.
197
  db_delete('upload')->condition('fid', $file->id())->execute();
198
199
}

200
201
202
/**
 * Control download access to files.
 *
203
204
205
 * The hook is typically implemented to limit access based on the entity that
 * references the file; for example, only users with access to a node should be
 * allowed to download files attached to that node.
206
 *
207
208
 * @param $field
 *   The field to which the file belongs.
209
 * @param \Drupal\Core\Entity\EntityInterface $entity
210
 *   The entity which references the file.
211
 * @param \Drupal\file\FileInterface $file
212
 *   The file entity that is being requested.
213
214
215
216
217
218
 *
 * @return
 *   TRUE is access should be allowed by this entity or FALSE if denied. Note
 *   that denial may be overridden by another entity controller, making this
 *   grant permissive rather than restrictive.
 *
219
 * @see hook_entity_field_access().
220
 */
221
function hook_file_download_access($field, Drupal\Core\Entity\EntityInterface $entity, Drupal\file\FileInterface $file) {
222
  if ($entity->getEntityTypeId() == 'node') {
223
    return $entity->access('view');
224
225
226
227
228
229
230
231
232
233
234
235
  }
}

/**
 * Alter the access rules applied to a file download.
 *
 * Entities that implement file management set the access rules for their
 * individual files. Module may use this hook to create custom access rules
 * for file downloads.
 *
 * @see hook_file_download_access().
 *
236
 * @param $grants
237
238
239
 *   An array of grants gathered by hook_file_download_access(). The array is
 *   keyed by the module that defines the entity type's access control; the
 *   values are Boolean grant responses for each module.
240
241
 * @param array $context
 *   An associative array containing the following key-value pairs:
242
243
 *   - field: The field to which the file belongs.
 *   - entity: The entity which references the file.
244
 *   - file: The file entity that is being requested.
245
 *
246
 * @see hook_file_download_access().
247
 */
248
function hook_file_download_access_alter(&$grants, $context) {
249
250
251
252
253
  // For our example module, we always enforce the rules set by node module.
  if (isset($grants['node'])) {
    $grants = array('node' => $grants['node']);
  }
}