file_entity.api.php in File Entity (fieldable files) 8.2
Same filename and directory in other branches
Hooks provided by the File Entity module.
File
file_entity.api.phpView source
<?php
/**
* @file
* Hooks provided by the File Entity module.
*/
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
/**
* Control access to listings of files.
*
* @param object $query
* A query object describing the composite parts of a SQL query related to
* listing files.
*
* @see hook_query_TAG_alter()
* @ingroup file_entity_access
*/
function hook_query_file_entity_access_alter(AlterableInterface $query) {
// Only show files that have been uploaded more than an hour ago.
$query
->condition('timestamp', \Drupal::time()
->getRequestTime() - 3600, '<=');
}
/**
* Alter file download headers.
*
* @param array $headers
* Array of download headers.
* @param object $file
* File object.
*/
function hook_file_download_headers_alter(array &$headers, $file) {
// Instead of being powered by PHP, tell the world this resource was powered
// by your custom module!
$headers['X-Powered-By'] = 'My Module';
}
/**
* React to a file being downloaded.
*/
function hook_file_transfer($uri, array $headers) {
// Redirect a download for an S3 file to the actual location.
if (StreamWrapperManager::getScheme($uri) == 's3') {
$url = file_create_url($uri);
drupal_goto($url);
}
}
/**
* Decides which file type (bundle) should be assigned to a file entity.
*
* @param object $file
* File object.
*
* @return array
* Array of file type machine names that can be assigned to a given file type.
* If there are more proposed file types the one, that was returned the first,
* wil be chosen. This can be, however, changed in alter hook.
*
* @see hook_file_type_alter()
*/
function hook_file_type($file) {
// Assign all files uploaded by anonymous users to a special file type.
if (\Drupal::currentUser()
->isAnonymous()) {
return array(
'untrusted_files',
);
}
}
/**
* Alters list of file types that can be assigned to a file.
*
* @param array $types
* List of proposed types.
* @param object $file
* File object.
*/
function hook_file_type_alter(&$types, $file) {
// Choose a specific, non-first, file type.
$types = array(
$types[4],
);
}
/**
* Provides metadata information.
*
* @todo Add documentation.
*
* @return array
* An array of metadata information.
*/
function hook_file_metadata_info() {
}
/**
* Alters metadata information.
*
* @todo Add documentation.
*
* @return array
* an array of metadata information.
*/
function hook_file_metadata_info_alter() {
}
Functions
Name | Description |
---|---|
hook_file_download_headers_alter | Alter file download headers. |
hook_file_metadata_info | Provides metadata information. |
hook_file_metadata_info_alter | Alters metadata information. |
hook_file_transfer | React to a file being downloaded. |
hook_file_type | Decides which file type (bundle) should be assigned to a file entity. |
hook_file_type_alter | Alters list of file types that can be assigned to a file. |
hook_query_file_entity_access_alter | Control access to listings of files. |