Same name and namespace in other branches
  1. 6.x includes/file.inc \file_validate_name_length()
  2. 7.x includes/file.inc \file_validate_name_length()
  3. 8.9.x core/modules/file/file.module \file_validate_name_length()
  4. 9 core/modules/file/file.module \file_validate_name_length()

Checks for files with names longer than can be stored in the database.

Parameters

\Drupal\file\FileInterface $file: A file entity.

Return value

array An empty array if the file name length is smaller than the limit or an array containing an error message if it's not or is empty.

Deprecated

in drupal:10.2.0 and is removed from drupal:11.0.0. Use the 'file.validator' service instead.

See also

https://www.drupal.org/node/3363700

1 call to file_validate_name_length()
LegacyValidatorTest::testFileValidateNameLength in core/modules/file/tests/src/Kernel/LegacyValidatorTest.php
This will ensure the filename length is valid.
1 string reference to 'file_validate_name_length'
FileValidatorTest::testValidate in core/modules/file/tests/src/Kernel/Validation/FileValidatorTest.php
Tests the validator.

File

core/modules/file/file.module, line 121
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function file_validate_name_length(FileInterface $file) {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'file.validator\' service instead. See https://www.drupal.org/node/3363700', E_USER_DEPRECATED);
  $errors = [];
  if (!$file
    ->getFilename()) {
    $errors[] = t("The file's name is empty. Enter a name for the file.");
  }
  if (strlen($file
    ->getFilename()) > 240) {
    $errors[] = t("The file's name exceeds the 240 characters limit. Rename the file and try again.");
  }
  return $errors;
}