2011-11-19 69 views
10

我使用以下模塊AM:如何使一個YouTube縮略圖在模板中的Drupal 7

  • 媒體
  • media_youtube
  • 樣式

,並想呈現一個縮略圖在模板(.tpl)中的Youtube視頻。我應該使用哪個主題函數以及哪些參數?

我最好quess會是這樣的:

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

其中$ my_media是包含FID,URI,文件名的數組,filemime等

因爲我很新的Drupal的,我所有的嘗試理解模塊源代碼失敗。我覺得我已經嘗試了在youtube和styles模塊中定義的樣式名稱的所有可能組合,但沒有得到任何輸出。

性。但是隻是正常使用

print theme('media_youtube_video',$my_media); 

視頻本身你們怎麼辦呢?

回答

12

挖掘media_youtube模塊代碼中有一個函數可以在includes/media_youtube.formatters.inc中爲您構建,名爲media_youtube_file_formatter_image_view()。您可以使用如下代碼來呈現縮略圖:

// 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); 
+0

Thx!奇蹟般有效。 – trioni

+0

你能告訴在哪裏添加這段代碼或者在vide tpl或其他的東西? – user3386779

0

如果你要同時打印拇指+視頻,你可以這樣做:如果你在一個TPL並希望

<?php print render($content['field_your_field']); ?> 

使用$變量 - 這將是這樣的:

<?php print render($variables['content']['field_url']['#object']->content['field_video']);?> 

厚朴ü可以使用它...

4

再舉一個例子,這使您可以使用自定義圖像樣式呈現視頻縮略圖(vimeo,youtube,qbrick等)。

$file - 您可以從媒體字段輕鬆獲取的文件對象。

$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri); 
$image_uri = $wrapper->getLocalThumbnailPath(); 

$image_rendered = theme('image_style', array(
    'style_name' => 'YOUR_IMAGE_STYLE_HERE', 
    'path' => $image_uri 
)); 

請注意,如果你想也呈現圖像,比你需要檢查$file->type如果是videoimage,因爲它會用不同的方法不同的包裝對象。