blob: 26e36498b37cab36d428d2530cb8f5f346152861 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?php
namespace Drupal\file\Validation;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Environment;
/**
* Provides a trait to create validators from settings.
*/
trait FileValidatorSettingsTrait {
/**
* Gets the upload validators for the specified settings.
*
* @param array $settings
* An associative array of settings. The following keys are supported:
* - max_filesize: The maximum file size in bytes. Defaults to the PHP max
* upload size.
* - file_extensions: A space-separated list of allowed file extensions.
*
* @return array
* An array suitable for passing to file_save_upload() or the file field
* element's '#upload_validators' property.
*/
public function getFileUploadValidators(array $settings): array {
$validators = [
// Add in our check of the file name length.
'FileNameLength' => [],
];
// Cap the upload size according to the PHP limit.
$maxFilesize = Bytes::toNumber(Environment::getUploadMaxSize());
if (!empty($settings['max_filesize'])) {
$maxFilesize = min($maxFilesize, Bytes::toNumber($settings['max_filesize']));
}
// There is always a file size limit due to the PHP server limit.
$validators['FileSizeLimit'] = ['fileLimit' => $maxFilesize];
// Add the extension check if necessary.
if (!empty($settings['file_extensions'])) {
$validators['FileExtension'] = [
'extensions' => $settings['file_extensions'],
];
}
return $validators;
}
}
|