1 - I used theme_file_link function to get html link to a file and render that link on to my page.
filename link is displayed but fileicon is not displayed.
code :

$fid = 7;
$file = file_load($fid);
$variables = array();
$variables['file'] = $file;
$icon_path = file_icon_path($file);
$variables['icon_directory'] = $icon_path;
$out .= theme_file_link($variables);

Wrong result : (html code) &lt img class="file-icon" alt="" title="application/pdf" src="" / &gt

2 - Result is correct in form used to browse file and download it : fileicon is correctly displayed.
Correct result : (html code) &lt img class="file-icon" alt="" title="application/pdf" src="/drupal/modules/file/icons/application-pdf.png" / &gt

Is theme_file_link the right function to render html link with fileicon and filename ?
Which function is used in form API to render managed_file link (see 2) ?

Comments

Jaypan’s picture

Theme functions are never called directly.

You should be using something like:

$fid = 7;
$file = file_load($fid);
$variables = array();
$variables['file'] = $file;
$icon_path = file_icon_path($file);
$variables['icon_directory'] = $icon_path;
$output = theme('file_link', $variables);

But I think that your code has a problem in that icon_directory should be a directory, and you are passing it a file path. You'll need to fix this.

luc.beaupere’s picture

Thank you for your help, following code works correctly.

Rendering of fileicon / filename link on module page :

$fid = 7;
$file = file_load($fid]);
$variables = array();
$variables['file'] = $file;
$icon_directory = variable_get('file_icon_directory', drupal_get_path('module', 'file') . '/icons');
$variables['icon_directory'] = $icon_directory;
$output .= '<span>' . theme('file_link', $variables) . '</span>';

hook_form to insert/modify and upload file :

$form['attached_files'] = array(
	'#title' => t('Attached file'),
	'#type' => 'managed_file', 
	'#description' => t('Click on "Browse..." to select file to upload.'),
	'#upload_location' => 'public://attached_files/',
);

hook_form_submit to save file attached to module 'module_name' and object 'object_type' with ID 'object_id' :

$file = file_load($form_state['values']['attached_files']);
$file->status = FILE_STATUS_PERMANENT;
file_usage_add($file, 'module_name', 'object_type', 'object_id');
file_save($file);