Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a Youtube thumbnail in a template in Drupal 7

I am using the following modules:

  • media
  • media_youtube
  • Styles

and would like to render a thumbnail of a Youtube video in a template (.tpl). Which theme function should I use and with what params?

My best quess would be something like:

$my_media['style_name'] = 'unlinked_thumbnail';
print theme('file_styles',$my_media);

where $my_media is an array containing fid,uri,filename,filemime etc.

Since i'm very new to Drupal, all my attempts to make sense of the modules source code have failed. I feel like I have tried all possible combinations of style names defined in the youtube and styles module without getting any output.

Rendering the video itself however works just fine using

print theme('media_youtube_video',$my_media);

How do you guys do it?

like image 614
trioni Avatar asked Nov 19 '11 20:11

trioni


1 Answers

Digging around in the media_youtube module code there's a function that will build this up for you in includes/media_youtube.formatters.inc called media_youtube_file_formatter_image_view(). You can use code like the following to render the thumbnail image:

// Make sure the correct include file is loaded
module_load_include('inc', 'media_youtube', '/includes/media_youtube.formatters.inc');

// Load the file
$file = file_load($my_media['fid']);

// Set up the settings array with your image style
$display['settings'] = array('image_style' => 'unlinked_thumbnail');

// Get the render array for the thumbnail image
$image_render_array = media_youtube_file_formatter_image_view($file, $display, LANGUAGE_NONE);

// Render it
print render($image_render_array);
like image 170
Clive Avatar answered Oct 19 '22 10:10

Clive