2010-08-23 14:53:50 +00:00
< ? php
/**
* @ file
* Hooks for file module .
*/
2012-08-31 01:27:21 +00:00
2013-01-16 17:37:23 +00:00
/**
* Act on a newly created file .
*
* This hook runs after a new file object has just been instantiated . It can be
* used to set initial values , e . g . to provide defaults .
*
2013-08-18 21:16:19 +00:00
* @ param \Drupal\file\Entity\File $file
2013-01-16 17:37:23 +00:00
* The file object .
*/
2013-08-18 21:16:19 +00:00
function hook_file_create ( \Drupal\file\Entity\File $file ) {
2013-01-16 17:37:23 +00:00
if ( ! isset ( $file -> foo )) {
$file -> foo = 'some_initial_value' ;
}
}
2012-08-31 01:27:21 +00:00
/**
* Load additional information into file entities .
*
* file_load_multiple () calls this hook to allow modules to load
* additional information into each file .
*
* @ param $files
* An array of file entities , indexed by fid .
*
* @ see file_load_multiple ()
* @ see file_load ()
*/
function hook_file_load ( $files ) {
// Add the upload specific data into the file entity.
$result = db_query ( 'SELECT * FROM {upload} u WHERE u.fid IN (:fids)' , array ( ':fids' => array_keys ( $files ))) -> fetchAll ( PDO :: FETCH_ASSOC );
foreach ( $result as $record ) {
foreach ( $record as $key => $value ) {
2013-06-27 01:52:15 +00:00
$files [ $record [ 'target_id' ]] -> $key = $value ;
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 .
* @ return
* 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 ;
}
/**
* Act on a file being inserted or updated .
*
* This hook is called when a file has been added to the database . The hook
* doesn ' t distinguish between files created as a result of a copy or those
* created by an upload .
*
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $file
2012-08-31 01:27:21 +00:00
* The file entity that is about to be created or updated .
*/
2013-06-15 08:46:11 +00:00
function hook_file_presave ( Drupal\file\FileInterface $file ) {
2014-01-07 10:09:22 +00:00
// Change the owner of the file.
$file -> uid -> value = 1 ;
2012-08-31 01:27:21 +00:00
}
/**
* Respond to a file being added .
*
* This hook is called after a file has been added to the database . The hook
* doesn ' t distinguish between files created as a result of a copy or those
* created by an upload .
*
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $file
2012-08-31 01:27:21 +00:00
* The file that has been added .
*/
2013-06-15 08:46:11 +00:00
function hook_file_insert ( Drupal\file\FileInterface $file ) {
2012-08-31 01:27:21 +00:00
// Add a message to the log, if the file is a jpg
$validate = file_validate_extensions ( $file , 'jpg' );
if ( empty ( $validate )) {
2014-06-26 18:55:12 +00:00
\Drupal :: logger ( 'file' ) -> notice ( 'A jpg has been added.' );
2012-08-31 01:27:21 +00:00
}
}
/**
* Respond to a file being updated .
*
* This hook is called when an existing file is saved .
*
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $file
2012-08-31 01:27:21 +00:00
* The file that has just been updated .
*/
2013-06-15 08:46:11 +00:00
function hook_file_update ( Drupal\file\FileInterface $file ) {
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 ) {
$old_filename = $file -> getFilename ();
$file -> setFilename ( $file -> getOwner () -> name . '_' . $file -> getFilename ());
2012-11-27 15:35:31 +00:00
$file -> save ();
2014-06-26 18:55:12 +00:00
\Drupal :: logger ( 'file' ) -> notice ( t ( '%source has been renamed to %destination' , array ( '%source' => $old_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 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-26 18:55:12 +00:00
\Drupal :: logger ( 'file' ) -> notice ( t ( '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-26 18:55:12 +00:00
\Drupal :: logger ( 'file' ) -> notice ( t ( '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
}
/**
* Act prior to file deletion .
*
* This hook is invoked when deleting a file before the file is removed from the
* filesystem and before its records are removed from the database .
*
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $file
2012-08-31 01:27:21 +00:00
* The file that is about to be deleted .
*
* @ see hook_file_delete ()
2014-03-27 11:54:40 +00:00
* @ see \Drupal\file\FileStorage :: delete ()
2012-08-31 01:27:21 +00:00
* @ see upload_file_delete ()
*/
2013-06-15 08:46:11 +00:00
function hook_file_predelete ( Drupal\file\FileInterface $file ) {
2012-08-31 01:27:21 +00:00
// Delete all information associated with the file.
2013-06-15 08:46:11 +00:00
db_delete ( 'upload' ) -> condition ( 'fid' , $file -> id ()) -> execute ();
2012-08-31 01:27:21 +00:00
}
/**
* Respond to file deletion .
*
* This hook is invoked after the file has been removed from
* the filesystem and after its records have been removed from the database .
*
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $file
2012-08-31 01:27:21 +00:00
* The file that has just been deleted .
*
* @ see hook_file_predelete ()
2014-03-27 11:54:40 +00:00
* @ see \Drupal\file\FileStorage :: delete ()
2012-08-31 01:27:21 +00:00
*/
2013-06-15 08:46:11 +00:00
function hook_file_delete ( Drupal\file\FileInterface $file ) {
2012-08-31 01:27:21 +00:00
// Delete all information associated with the file.
2013-06-15 08:46:11 +00:00
db_delete ( 'upload' ) -> condition ( 'fid' , $file -> id ()) -> execute ();
2012-08-31 01:27:21 +00:00
}
2010-08-23 14:53:50 +00:00
/**
* Control download access to files .
*
2012-08-21 19:40:43 +00:00
* The hook is typically implemented to limit access based on the entity that
* references the file ; for example , only users with access to a node should be
* allowed to download files attached to that node .
2010-08-23 14:53:50 +00:00
*
2012-02-28 18:32:20 +00:00
* @ param $field
* The field to which the file belongs .
2013-06-15 08:46:11 +00:00
* @ param \Drupal\Core\Entity\EntityInterface $entity
2012-08-21 19:40:43 +00:00
* The entity which references the file .
2013-06-15 08:46:11 +00:00
* @ param \Drupal\file\FileInterface $file
2012-08-21 15:19:09 +00:00
* The file entity that is being requested .
2010-08-23 14:53:50 +00:00
*
* @ return
* TRUE is access should be allowed by this entity or FALSE if denied . Note
* that denial may be overridden by another entity controller , making this
* grant permissive rather than restrictive .
*
2013-09-30 13:14:06 +00:00
* @ see hook_entity_field_access () .
2010-08-23 14:53:50 +00:00
*/
2013-06-15 08:46:11 +00:00
function hook_file_download_access ( $field , Drupal\Core\Entity\EntityInterface $entity , Drupal\file\FileInterface $file ) {
2014-01-30 12:06:58 +00:00
if ( $entity -> getEntityTypeId () == 'node' ) {
2013-12-01 12:33:39 +00:00
return $entity -> access ( 'view' );
2010-08-23 14:53:50 +00:00
}
}
/**
* Alter the access rules applied to a file download .
*
* Entities that implement file management set the access rules for their
* individual files . Module may use this hook to create custom access rules
* for file downloads .
*
* @ see hook_file_download_access () .
*
2011-05-08 19:50:38 +00:00
* @ param $grants
2010-08-23 14:53:50 +00:00
* An array of grants gathered by hook_file_download_access () . The array is
* keyed by the module that defines the entity type ' s access control ; the
* values are Boolean grant responses for each module .
2012-08-21 15:19:09 +00:00
* @ param array $context
* An associative array containing the following key - value pairs :
2012-08-21 19:40:43 +00:00
* - field : The field to which the file belongs .
* - entity : The entity which references the file .
2012-08-21 15:19:09 +00:00
* - file : The file entity that is being requested .
2010-08-23 14:53:50 +00:00
*
2012-08-21 15:19:09 +00:00
* @ see hook_file_download_access () .
2010-08-23 14:53:50 +00:00
*/
2012-08-21 15:19:09 +00:00
function hook_file_download_access_alter ( & $grants , $context ) {
2010-08-23 14:53:50 +00:00
// For our example module, we always enforce the rules set by node module.
if ( isset ( $grants [ 'node' ])) {
$grants = array ( 'node' => $grants [ 'node' ]);
}
}