Theming the Drupal flashvideo module with swfobject.

The FlashVideo module is great, but you may want to embed the video with swfobject, so here's one way to do it.

Of course for this to work you're going to need to include swfobject.js in your template. How you do that depends on your theme and is a little beyond the scope of this article.

Once you have that setup, you just need to open up template.php and add ths theme override function.

  1. <?php
  2. /**
  3.  * Override flashvideo embed code
  4.  */
  5. function phptemplate_flashvideo_play_flash($video) {
  6.  
  7. // Initialize index variable used for multiple players if it doesn't exist
  8. if (!$GLOBALS['flashvideo_embed_index']) { $GLOBALS['flashvideo_embed_index']=0; }
  9.  
  10. // increment index
  11. $GLOBALS['flashvideo_embed_index']++;
  12.  
  13. // Get the FlashVars for this video.
  14. $flashvars = flashvideo_get_flashvars($video);
  15. // Creates an absolute path for the player.
  16. $loader_path = check_url(file_create_url(flashvideo_variable_get($video['nodetype'], 'player', 'Player.swf')));
  17. $image = str_replace('.flv', '.jpg', $video['file']);
  18.  
  19. $output .=<<<EOF
  20.   <div id="video-player-{$GLOBALS['flashvideo_embed_index']}"></div>
  21.   <script type="text/javascript">
  22.   var so = new SWFObject('/misc/mediaplayer.swf','mpl','450','357','8');
  23.   so.addParam('allowscriptaccess','always');
  24.   so.addParam('allowfullscreen','true');
  25.   so.addVariable('displayheight','357');
  26.   so.addVariable('displaywidth','450');
  27.   so.addVariable('file','{$video['file']}');
  28.   so.addVariable('image', '{$image}');
  29.   so.addVariable('backcolor','0x000000');
  30.   so.addVariable('frontcolor','0xEEEEEE');
  31.   so.addVariable('lightcolor','0xFFFFCC');
  32.   so.addVariable('screencolor','0x000000');
  33.   so.addVariable('searchbar','false');
  34.   so.addVariable('showdigits', 'true');
  35.   so.write('video-player-{$GLOBALS['flashvideo_embed_index']}');
  36.   </script>
  37. EOF;
  38.  
  39. return $output;
  40. }
  41. ?>

This code is using the JW FLV Player, so your swf parameters may differ if you are using something else.

What if you have more than 1 video?

Great piece of code!

Very useful and works great. Is there a way to update the code so it works with multiple videos on the same page?

I'm using FlashVideo module as well and found if you have multiple videos, it won't display correctly because the code is outputting the same id for each Flash video.

Good Question!

I was only using 1 video per page.

Check out the updated code in the main article. I hate to use globals, but $video doesn't contain any unique data besides the file path.

I added an index variable that increments every time the theme function is called. The index is then added to the end of the div id.

i.e.

div id="video-player-1"
div id="video-player-2"

etc...

I haven't tested it with multiple players, but this should work. Let me know if you test it out.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <img> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h5> <h6> <h7>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.