diff --git a/core/modules/file/src/Validation/FileValidator.php b/core/modules/file/src/Validation/FileValidator.php index 79fb762cbee..44c4781ee74 100644 --- a/core/modules/file/src/Validation/FileValidator.php +++ b/core/modules/file/src/Validation/FileValidator.php @@ -2,6 +2,7 @@ namespace Drupal\file\Validation; +use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Validation\ConstraintManager; use Drupal\Core\Validation\DrupalTranslator; @@ -53,7 +54,12 @@ class FileValidator implements FileValidatorInterface { else { // Create the constraint. // Options are an associative array of constraint properties and values. - $constraints[] = $this->constraintManager->create($validator, $options); + try { + $constraints[] = $this->constraintManager->create($validator, $options); + } + catch (PluginNotFoundException) { + @trigger_error(sprintf('Passing invalid constraint plugin ID "%s" in the list of $validators to Drupal\file\Validation\FileValidator::validate() is deprecated in drupal:10.2.0 and will throw an exception in drupal:11.0.0. See https://www.drupal.org/node/3363700', $validator), E_USER_DEPRECATED); + } } } diff --git a/core/modules/file/tests/src/Kernel/Validation/FileValidatorTest.php b/core/modules/file/tests/src/Kernel/Validation/FileValidatorTest.php index 88ff1f74ada..a990cae8cd2 100644 --- a/core/modules/file/tests/src/Kernel/Validation/FileValidatorTest.php +++ b/core/modules/file/tests/src/Kernel/Validation/FileValidatorTest.php @@ -31,9 +31,11 @@ class FileValidatorTest extends FileValidatorTestBase { // Use a mix of legacy functions and plugin IDs to test both work. // Each Constraint has its own tests under // core/modules/file/tests/src/Kernel/Plugin/Validation/Constraint. + // Also check that arbitrary strings can be used. $validators = [ 'file_validate_name_length' => [], 'FileNameLength' => [], + 'foo' => [], ]; file_test_reset(); @@ -42,6 +44,7 @@ class FileValidatorTest extends FileValidatorTestBase { $this->assertCount(0, $violations); $this->assertCount(1, file_test_get_calls('validate')); + $this->expectDeprecation('Passing invalid constraint plugin ID "foo" in the list of $validators to Drupal\file\Validation\FileValidator::validate() is deprecated in drupal:10.2.0 and will throw an exception in drupal:11.0.0. See https://www.drupal.org/node/3363700'); file_test_reset(); $this->file->set('filename', ''); $violations = $this->validator->validate($this->file, $validators);