2010-08-23 14:53:50 +00:00
< ? php
/**
* @ file
* Hooks for file module .
*/
2013-01-16 17:37:23 +00:00
/**
2014-07-11 12:04:53 +00:00
* @ addtogroup hooks
* @ {
2013-01-16 17:37:23 +00:00
*/
2012-08-31 01:27:21 +00:00
/**
* 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 .
*
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $file
2012-08-31 01:27:21 +00:00
* The file entity being validated .
2015-06-12 14:46:25 +00:00
* @ return array
2012-08-31 01:27:21 +00:00
* An array of error messages . If there are no problems with the file return
* an empty array .
*
* @ see file_validate ()
*/
2013-06-15 08:46:11 +00:00
function hook_file_validate ( Drupal\file\FileInterface $file ) {
2012-08-31 01:27:21 +00:00
$errors = array ();
2013-06-15 08:46:11 +00:00
if ( ! $file -> getFilename ()) {
2012-08-31 01:27:21 +00:00
$errors [] = t ( " The file's name is empty. Please give a name to the file. " );
}
2013-06-15 08:46:11 +00:00
if ( strlen ( $file -> getFilename ()) > 255 ) {
2012-08-31 01:27:21 +00:00
$errors [] = t ( " The file's name exceeds the 255 characters limit. Please rename the file and try again. " );
}
return $errors ;
}
/**
* Respond to a file that has been copied .
*
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $file
2012-08-31 01:27:21 +00:00
* The newly copied file entity .
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $source
2012-08-31 01:27:21 +00:00
* The original file before the copy .
*
* @ see file_copy ()
*/
2013-06-15 08:46:11 +00:00
function hook_file_copy ( Drupal\file\FileInterface $file , Drupal\file\FileInterface $source ) {
2012-11-27 15:35:31 +00:00
// Make sure that the file name starts with the owner's user name.
2013-06-15 08:46:11 +00:00
if ( strpos ( $file -> getFilename (), $file -> getOwner () -> name ) !== 0 ) {
$file -> setFilename ( $file -> getOwner () -> name . '_' . $file -> getFilename ());
2012-11-27 15:35:31 +00:00
$file -> save ();
2012-08-31 01:27:21 +00:00
2014-06-27 06:47:17 +00:00
\Drupal :: logger ( 'file' ) -> notice ( 'Copied file %source has been renamed to %destination' , array ( '%source' => $source -> filename , '%destination' => $file -> getFilename ()));
2012-11-27 15:35:31 +00:00
}
2012-08-31 01:27:21 +00:00
}
/**
* Respond to a file that has been moved .
*
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $file
2012-08-31 01:27:21 +00:00
* The updated file entity after the move .
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $source
2012-08-31 01:27:21 +00:00
* The original file entity before the move .
*
* @ see file_move ()
*/
2013-06-15 08:46:11 +00:00
function hook_file_move ( Drupal\file\FileInterface $file , Drupal\file\FileInterface $source ) {
2012-11-27 15:35:31 +00:00
// Make sure that the file name starts with the owner's user name.
2013-06-15 08:46:11 +00:00
if ( strpos ( $file -> getFilename (), $file -> getOwner () -> name ) !== 0 ) {
$file -> setFilename ( $file -> getOwner () -> name . '_' . $file -> getFilename ());
2012-11-27 15:35:31 +00:00
$file -> save ();
2012-08-31 01:27:21 +00:00
2014-06-27 06:47:17 +00:00
\Drupal :: logger ( 'file' ) -> notice ( 'Moved file %source has been renamed to %destination' , array ( '%source' => $source -> filename , '%destination' => $file -> getFilename ()));
2012-11-27 15:35:31 +00:00
}
2012-08-31 01:27:21 +00:00
}
2014-07-11 12:04:53 +00:00
/**
* @ } End of " addtogroup hooks " .
*/