function hook_file_validate

Same name in other branches
  1. 7.x modules/system/system.api.php \hook_file_validate()
  2. 9 core/modules/file/file.api.php \hook_file_validate()
  3. 8.9.x core/modules/file/file.api.php \hook_file_validate()

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.

Parameters

\Drupal\file\FileInterface $file: The file entity being validated.

Return value

array An array of error messages. If there are no problems with the file return an empty array.

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

\Drupal\file\Validation\FileValidatorInterface

Related topics

File

core/modules/file/file.api.php, line 81

Code

function hook_file_validate(\Drupal\file\FileInterface $file) {
    $errors = [];
    if (!$file->getFilename()) {
        $errors[] = t("The file's name is empty. Give a name to the file.");
    }
    if (strlen($file->getFilename()) > 255) {
        $errors[] = t("The file's name exceeds the 255 characters limit. Rename the file and try again.");
    }
    return $errors;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.