I'm looking to do something like this
$fid = 15;
$file = (array)file_load($fid);
$content = drupal_render($file);
echo $content;
And specifically this fid is from the 7.x-2.x version of the media module.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI'm looking to do something like this
$fid = 15;
$file = (array)file_load($fid);
$content = drupal_render($file);
echo $content;
And specifically this fid is from the 7.x-2.x version of the media module.
This works for me. For videos via the media module:
<?php
$file = file_load($fid);
if (strpos($file->filemime, 'video/') !== 0) {
return;
}
$key = 'media_' . substr($file->filemime, 6) . '_video';
$formatter_info = file_info_formatter_types($key);
$content = array();
$content['#theme'] = $key;
$content['#uri'] = $file->uri;
if (isset($formatter_info['default settings'])) {
$content['#options'] = $formatter_info['default settings'];
}
$rendered = drupal_render($content);
return $rendered;
?>
And for images; this shows you what presets are available (#style_name
)
<?php
$styles = image_styles();
echo '<pre>' . print_r($styles, TRUE) . '</pre>';
?>
And this will render the file
<?php
$file = file_load($fid);
$image = image_load($file->uri);
$content = array(
'file' => array(
'#theme' => 'image_style',
'#style_name' => 'large',
'#path' => $image->source,
'#width' => $image->info['width'],
'#height' => $image->info['height'],
),
);
echo drupal_render($content);
?>
Note that image_load performs I/O.
And for the reverse; given a filename get a fid.
<?php
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'file')
->propertyCondition('filename', basename($filename))
->execute();
foreach ($result['file'] as $values) {
$fid = $values->fid;
break;
}
echo $fid
?>
Given media embed code, get a FID.
<?php
$file = media_parse_to_file($embed_code);
if (empty($file->fid)) {
return FALSE;
}
return $file->fid;
?>
$file->filemime = video/youtube
then the key will end up being 'media_youtube_video' which will use the theme function inside of drupal.org/project/media_youtube (see media_youtube_theme())
Aug 3, 2016 at 18:20
You are misapplying drupal_render()
in your example. drupal_render()
holds the content to be displayed and the instructions for how to render it in an array so that all content can be modified by other modules, until the last moment before displaying. Everything is loaded in the drupal_render()
argument &$elements
, which is the argument for the function. drupal_render()
does not return rendered elements from variables passed in arguments.
Look at the theme API for a series of functions that will provide HTML for various content elements including files.
The function you are probably looking for is file_get_content_headers(), which sets the headers for allowing a file to be downloaded, or (if the file is a text file, or an image) to be viewed inline.
The function requires a file object as returned by file_load_multiple(), file_load(), or entity_load('file').