W3cubDocs

/Drupal 8

function hook_file_validate

hook_file_validate(Drupal\file\FileInterface $file)

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.

See also

file_validate()

Related topics

Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/file/file.api.php, line 27
Hooks for file module.

Code

function hook_file_validate(Drupal\file\FileInterface $file) {
  $errors = array();

  if (!$file->getFilename()) {
    $errors[] = t("The file's name is empty. Please give a name to the file.");
  }
  if (strlen($file->getFilename()) > 255) {
    $errors[] = t("The file's name exceeds the 255 characters limit. Please rename the file and try again.");
  }

  return $errors;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!file!file.api.php/function/hook_file_validate/8.1.x