Theme multiple items in embedded video field
From Transmission
A problem:
Having patched feedapi_mapper to allow us to aggregate multiple video enclosures and Media RSS fields from a third party RSS2 feed, we now have multiple versions of the same video displaying on the node.
A solution:
Modify the theme to display the one embedded flash video, and links to the other video types (mov, mp4, avi etc).
Here's how I did it:
1) Add a preprocess function to the theme's template.php:
function phptemplate_preprocess_node(&$vars) {
// get the $node object
$node = $vars['node'];
// loop through all embedded video fields
foreach ($node->field_emvideo as $video) {
if ($video['provider']=='zzz_custom_url') {
// if the provider is zzz_custom_url, we want to know the file extension
$video_file_name_array = explode('.', $video['value']);
$video_file_name_ext = array_pop($video_file_name_array);
$video['ext'] = $video_file_name_ext;
if ($video_file_name_ext == "flv") {
// if the extension is flv, add the $video array to the $display_vid array
$display_vid[] = $video;
} else {
// otherwise add it to the $link_vid array to be displayed as a link
$link_vid[$video['value']] = $video;
}
} else {
// else if not zzz_custom_url, then we have a provider,
// so we'll just add the $video array to the $display_vid array
$display_vid[] = $video;
}
}
if ($display_vid[0]['provider'] == 'zzz_custom_url' && $display_vid[0]['ext'] == 'flv') {
// add the flash player using swftools swf() function
$vars['video_view'] = swf($display_vid[0]['value'] );
} else {
// we have a provider, so display using theme('emvideo_video_video') embed accordingly
$field_type = 'field_emvideo';
$system_types = _content_type_info();
$field = $system_types['fields'][$field_type];
$field['widget'] = $system_types['content types'][$node->type]['fields'][$field_type]['widget'];
$vars['video_view'] = theme('emvideo_video_video', $field, $node->{$field_type}[0], 'emvideo_embed', $node);
}
if (count($link_vid) > 0) {
foreach ($link_vid as $vid_link) {
// get the file name
$vid_file_name_array = explode('/', $vid_link['value']);
$vid_file_name = array_pop($vid_file_name_array);
$link['href'] = $vid_link['value'];
$link['title'] = $vid_file_name ;
if (isset($vid_link['data']['meta']['size'])) {
$link['title'] .= " (" . format_size($vid_link['data']['meta']['size']) . ")";
}
$vars['video_download_links_array'][] = $link;
}
$vars['video_download_links'] = theme('links', $vars['video_download_links_array']);
}
}
2) Modify the node.tpl.php
In node.tpl.php, I removed
<?php print $content ?>
And replaced it with
<?php print $video_view // the embedded video player?> <?php print $video_download_links // the themed links to the other file types to download?> <?php print $node->content['body']['#value'] // the body text ?>
This prints out the themed variables as defined in our phptemplate_preprocess_node() function.
I'm sure there's a more elegant way to do this, but this worked for me.