1.20.x file.module | file_view_file($file, $display = 'full', $langcode = NULL) |
Generate an array for rendering just the file portion of a file entity.
Parameters
object $file: A file object.
string|array $displays: Can be either:
- the name of a view mode;
- or custom display settings, as returned by file_display().
string $langcode: (optional) A language code to use for rendering. Defaults to the global content language of the current request.
Return value
array: An array as expected by backdrop_render().
File
- modules/
file/ file.module, line 3406 - Defines a "managed_file" Form API field and a "file" field for Field module.
Code
function file_view_file($file, $display = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->langcode;
}
// Prepare incoming display specifications.
if (is_string($display)) {
// Allow modules to change the view mode.
$entity = array($file->fid => $file);
$view_mode = key(entity_view_mode_prepare('file', $entity, $display, $langcode));
$display = file_display($file->type, $view_mode);
}
else {
$view_mode = '_custom_display';
}
$formatter = $display['formatter'];
// Attempt to display the file with the specified display.
// See file_display() for details.
$element = NULL;
// Load the file that contains file_info_formatter_types().
module_load_include('inc', 'file', 'file.admin');
$formatter_info = file_info_formatter_types($formatter);
if (isset($formatter_info['view callback']) && ($function = $formatter_info['view callback']) && function_exists($function)) {
if (!empty($formatter_info['default settings'])) {
if (empty($display['settings'][$formatter])) {
$display['settings'] = array();
}
$display['settings'][$formatter] += $formatter_info['default settings'];
}
$display_info = array(
'type' => $formatter,
'settings' => $display['settings'][$formatter],
);
$element = $function($file, $display_info, $langcode);
}
// As a last resort, fall back to showing a link to the file.
if (!isset($element)) {
$element = array(
'#theme' => 'file_link',
'#file' => $file,
);
}
// Add defaults and return the element.
$element += array(
'#file' => $file,
'#view_mode' => $view_mode,
'#language' => $langcode,
);
return $element;
}