当我想允许特定字段的 .js 文件按原样上传(没有 .txt 和 'application/javascript' mimetype)时,我遇到了这种情况。另外,我不想改变 Drupal 核心……当然。
所以我需要创建一个实现 hook_file_presave() 的模块。这也适用于 Multiupload File Widget,因为它的钩子是 on file_save()
。
请注意,您必须将MYMODULE_NAME
and替换MYFIELD_NAME
为您自己的值。
function MYMODULE_NAME_file_presave($file) {
// Bypass secure file extension for .js for field_additional_js field only
if((isset($file->source) && strpos($file->source, "MYFIELD_NAME") !== FALSE) && substr($file->filename, strlen($file->filename) - 7) == ".js.txt") {
// Define new uri and save previous
$original_uri = $file->uri;
$new_uri = substr($file->destination, null, -4);
// Alter file object
$file->filemime = 'application/javascript';
$file->filename = substr($file->filename, null, -4);
$file->destination = file_destination($new_uri, FILE_EXISTS_RENAME);
$file->uri = $file->destination;
// Move fil (to remove .txt)
file_unmanaged_move($original_uri, $file->destination);
// Display message that says that
drupal_set_message(t('Security bypassed for .js for this specific field (%f).', array('%f' => $file->filename)));
}
}