hook_file_action($op, $args)
ubercart/docs/hooks.php, line 396
Perform actions on file products.
The uc_file module comes with a file manager (found at Administer » Store administration » Products » View file downloads) that provides some basic functionality: deletion of multiple files and directories, and upload of single files (those looking to upload multiple files should just directly upload them to their file download directory then visit the file manager which automatically updates new files found in its directory). Developers that need to create more advanced actions with this file manager can do so by using this hook.
$op The operation being taken by the hook, possible ops defined below.
The return value of hook depends on the op being performed, possible return values defined below.
| Name | Description |
|---|---|
| Hooks | Allow modules to interact with the Drupal core. |
<?php
function hook_file_action($op, $args) {
switch ($op) {
case 'info':
return array('uc_image_watermark_add_mark' => 'Add Watermark');
case 'insert':
//automatically adds watermarks to any new files that are uploaded to the file download directory
_add_watermark($args['file_object']->filepath);
break;
case 'form':
if ($args['action'] == 'uc_image_watermark_add_mark') {
$form['watermark_text'] = array(
'#type' => 'textfield',
'#title' => t('Watermark Text'),
);
$form['submit_watermark'] = array(
'#type' => 'submit',
'#value' => t('Add Watermark'),
);
}
return $form;
case 'upload':
_add_watermark($args['file_object']->filepath);
break;
case 'upload_validate':
//Given a file path, function checks if file is valid JPEG
if(!_check_image($args['file_object']->filepath)) {
form_set_error('upload',t('Uploaded file is not a valid JPEG'));
}
break;
case 'validate':
if ($args['form_values']['action'] == 'uc_image_watermark_add_mark') {
if (empty($args['form_values']['watermark_text'])) {
form_set_error('watermar_text',t('Must fill in text'));
}
}
break;
case 'submit':
if ($args['form_values']['action'] == 'uc_image_watermark_add_mark') {
foreach ($args['form_values']['file_ids'] as $file_id) {
$filename = db_result(db_query("SELECT filename FROM {uc_files} WHERE fid = %d",$file_id));
//Function adds watermark to image
_add_watermark($filename);
}
}
break;
}
}
?>