如何在 Drupal 7 的模板中渲染 Youtube 缩略图

2021-12-29 00:00:00 php drupal drupal-7

我正在使用以下模块:

  • 媒体
  • media_youtube
  • 样式

并且想要在模板 (.tpl) 中呈现 Youtube 视频的缩略图.我应该使用哪个主题功能以及什么参数?

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_media['style_name'] = 'unlinked_thumbnail';
print theme('file_styles',$my_media);

其中 $my_media 是一个包含 fid、uri、filename、filemime 等的数组

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

由于我对 Drupal 非常陌生,所以我试图理解模块源代码的所有尝试都失败了.我觉得我已经尝试了 youtube 和样式模块中定义的所有可能的样式名称组合,但没有得到任何输出.

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.

使用

print theme('media_youtube_video',$my_media);

你们是怎么做到的?

推荐答案

media_youtube 模块代码中挖掘,有一个函数可以在 includes/media_youtube.formatters 中为您构建此代码.inc 称为 media_youtube_file_formatter_image_view().您可以使用如下代码来渲染缩略图:

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);

相关文章