class FileExtensionConstraintValidator
Same name in other branches
- 10 core/modules/file/src/Plugin/Validation/Constraint/FileExtensionConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileExtensionConstraintValidator
Validates the file extension constraint.
Hierarchy
- class \Drupal\file\Plugin\Validation\Constraint\BaseFileConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator
- class \Drupal\file\Plugin\Validation\Constraint\FileExtensionConstraintValidator extends \Drupal\file\Plugin\Validation\Constraint\BaseFileConstraintValidator
Expanded class hierarchy of FileExtensionConstraintValidator
File
-
core/
modules/ file/ src/ Plugin/ Validation/ Constraint/ FileExtensionConstraintValidator.php, line 13
Namespace
Drupal\file\Plugin\Validation\ConstraintView source
class FileExtensionConstraintValidator extends BaseFileConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate(mixed $value, Constraint $constraint) : void {
$file = $this->assertValueIsFile($value);
if (!$constraint instanceof FileExtensionConstraint) {
throw new UnexpectedTypeException($constraint, FileExtensionConstraint::class);
}
$extensions = $constraint->extensions;
$regex = '/\\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i';
// Filename may differ from the basename, for instance in case files
// migrated from D7 file entities. Because of that new files are saved
// temporarily with a generated file name, without the original extension,
// we will use the generated filename property for extension validation only
// in case of temporary files; and use the file system file name in case of
// permanent files.
$subject = $file->isTemporary() ? $file->getFilename() : $file->getFileUri();
if (!preg_match($regex, $subject)) {
$this->context
->addViolation($constraint->message, [
'%files-allowed' => $extensions,
]);
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
BaseFileConstraintValidator::assertValueIsFile | protected | function | Checks the value is of type FileInterface. |
FileExtensionConstraintValidator::validate | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.