Last updated August 26, 2011. Created by kingandy on September 7, 2008.
Edited by kostask, Stuart Greenfield, chris_car. Log in to edit this page.

The documentation below is based on this post which was put in the issue queue (thanks to kingandy for taking the time to write it!). It refers to the SWF Tools 5 version of the swf() function.

Under SWF Tools 6 the function is changed slightly to make it more akin to other Drupal 6 function that take optional parameters. So instead of passing a series of individual parameters you pass the filename, followed by an optional associative array, with optional keys params, flashvars, othervars and methods.

Definition

SWF Tools 5
swf($file, $params = FALSE, $flashvars = FALSE, $othervars = FALSE, $methods = FALSE);

SWF Tools 6
swf($file, $args = array());

Description

Return outputs to allow flash content to be generated. May be pure HTML, or may include JavaScript, depending on the embedding method.

Parameters

$file (required): The file to be played. If it is a SWF file it will usually be embedded directly. Use a full URL, a path relative to webroot, or a path relative to the configured files directory.

$args (Drupal 6, optional): An associative array of arguments. This is in line with the Drupal 6 methodology and is a replacement for the "$params, $flashvars, $othervars" method of declaring arguments. The available keys are 'params', 'flashvars', 'othervars' and 'methods'. See the appropriate Drupal 5 arguments below for more on these.

$params (Drupal 5, optional): An associative array of variables to set e.g. array('bgcolor' => 'FF00FF') To set height and width: array('width' => '200', 'height' => '120'). However, as a convenient alternative for the common requirement of just height and width you can also pass a text string like '200x100'. If you pass nothing, and the file to play is a .swf, swftools will try and establish a natural width and height from the actual .swf file that you've passed into $file. Note - this autodetection process only works for local files, not remote url's. Possible $params are width, height, version, wmode, bgcolor, scale, quality, name, base, align, salign, allowfullscreen.

Be careful of the change with SWF Tools 6! As noted above, the way to supply this argument is to use 'params' as a key in the $args array. For example, the proper way to set the width and height is:

<?php
print swf('myMovie.swf', array('params' => array('width' => '200', 'height' => '200')));
?>

$flashvars (Drupal 5, optional): An associative array of flashvar variables to set. eg. array('playlist' => 'files/my_playlist.xml')

$othervars (Drupal 5, optional): An associative array of variables that might be required by the $player or $embed technique. These values are not output as params or flashvars. Available $othervars include: html_alt, playlist_data, id, class

$method (Drupal 5, optional): Explicitly declare an action, player or action by passing an array of the form:
array('action' => 'dosomething', 'player' => 'withsomething', 'embed' => 'withthisjavascript').
If you don't specify an explicit value then SWF Tools will use its defaults and determine an appropriate action (e.g. play an mp3 with the configured player, using the selected embedding method). It is not necessary to supply every parameter - e.g. to over-ride only the embedding method set just that key and value. A list of available action, players and methods (and their names) are summarised in the SWF Tools filter documentation.

Return value

A string containing the HTML output to embed the flash file.

Examples

There is sometimes confusion about how to use the swf() syntax, so there are some examples below to try and clear things up.

SWF Tools 5
Render an flv file, passing height and width parameters, and a flash var to set the thumbnail image.

<?php
print swf('test1.flv',
          array(
'width' => '400', 'height' => '300'),
          array(
'image' => 'picture.jpg')
        );
?>

SWF Tools 6
Render an flv file, passing height and width parameters, and set the thumbnail image on othervars, so all players can find it.
<?php
print swf('test1.flv',
          array(
           
'params' => array('width' => '400', 'height' => '300'),
           
'othervars' => array('image' => 'picture.jpg'),
          )
        );
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Is there a method to use alternate content with this function for users who have javascript turned off?

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

I know you can configure what SWFTools displays to non-javascript browsers at admin/media/swf/embed - the "HTML Alternative" field under "Javascript Embedding". Unfortunately it doesn't look like it's possible to declare this on a case-by-case basis.

EDIT: Actually I've just noticed the 'html_alt' parameter in the $othervars variable ... it might be worth investigating that. I guess you'd call that as follows in D5:

<?php
swf
('myMovie.swf', FALSE, FALSE, array('html_alt' => $html));
?>

or in D6:
<?php
print swf('myMovie.swf', array('othervars' => array('html_alt' => $html)));
?>

No promises though, that's just an educated guess :)

--Andy
Developing Drupal websites for Livelink New Media since 2008

I actually managed to get it working, I just didn't post my findings here!

The main problem I had was that I forgot to set swftools to use swfobject in the settings. I had installed the swfobject scripts in the folder, but not enabled them. After enabling swfobject in the swftools menu, I used this code:

<?php
$vars
['left_flash'] = swf
(
   
'left_flash.swf',
    array
    (
       
'params' => array
        (
           
'width' => '228',
           
'height' => '618',
           
'wmode' => 'transparent',
        ),
       
'othervars' => array
        (
            
'html_alt' => '<img src="' base_path() . drupal_get_path('theme', 'theme_name') . '/images/left_no_flash.png" alt="Replacement image used when flash does not load" />',
        ),
    )
);
?>

Explanation (for those who want/need it): What this does is loads the left_flash.swf file, which has a width of 228, a height of 618, and a transparent background, and if it doesn't load for whatever reason (the user doesn't have the correct version of the flash player, or has javascript turned off), it loads the replacement image 'left_no_flash.png' which is in the 'images' directory of my theme. It is also set up to load alternate text (the 'alt' in the image field) if the user has images turned off.

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

And for Drupal 5

<?php
print swf('flash.swf', array('wmode' => 'transparent'), FALSE,
  array(
'html_alt' => '<img src="'base_path() . drupal_get_path('theme', 'theme_name') .'/images/noflash.png" alt="Replacement image used when flash does not load" />')
);
?>

- Vivian

I got it to work with a small change, put print swf( in instead.

<?php
print swf('left_flash.swf',
    array
    (
       
'params' => array
        (
           
'width' => '228',
           
'height' => '618',
           
'wmode' => 'transparent',
        ),
       
'othervars' => array
        (
            
'html_alt' => '<img src="' base_path() . drupal_get_path('theme', 'theme_name') . '/images/left_no_flash.png" alt="Replacement image used when flash does not load" />',
        ),
    )
);
?>

How Can I prevent a swf to being in cache?

That's more of a browser issue than a problem for the injection code - in my experience IE in particular is very enthusiastic about caching Flash movies. Adobe suggests setting no-cache headers in the HTML page, but the problem is that the SWF file is loaded via a separate HTTP request that doesn't include the page's headers. As far as I'm aware there's no way of setting a caching preference for the SWF specifically, short of creating an interstitial page to serve the SWF up with headers.

However - there is a relatively simple way to trick the browser into thinking it's a different SWF file and thus downloading anew (because it doesn't think it has a version cached). Flash files can be loaded with a query string after the file URL; this is generally used as a way of passing variables into the movie (for example "example.swf?foo=x&bar=y"), but most browsers include the query string when determining whether or not they have a cached version of the file. (After all, if the variables are different you're probably going to want to see some difference in the movie.)

So the easy workaround is simply to include a random variable in your SRC. You can generate this using php - for example:

<?php
$rand
= rand();
print
swf("example.swf?rand=$rand");
?>

A word of warning - by sidestepping the cache, you are sidestepping the cache. You lose all benefits from caching - every user has to download the SWF file every time they load a page displaying the movie. Without knowing any of the details of your case (how big the file is, how often it appears on your site) I can't say how much of a server hit that will cause.

--Andy
Developing Drupal websites for Livelink New Media since 2008

Not working but thanks anyway...

When I use your code the movie even appear, I should mistake something...

This is the code I'm using now and works like a charm:

<object type="application/x-shockwave-flash" height="500" data="/sites/all/files/SWF/login.swf?v=<?php echo time() ?>" style="visibility: visible;">
<param name="allowscriptaccess" value="sameDomain"/>
<param name="allowFullScreen" value="false"/>
<param name="quality" value="high"/>
<param name="swliveconnect" value="true"/>
</object>

I want to prevent the cache for development purposes, I'm testing sef movies, changing constanly and I needed this way, once a release I may will disable cache preventing...

I tried your solution here, but the ? is being encoded or changed by the function to %253F so the player throws an error because it can't find the file. Any ideas how to stop the encoding?

I got the youtube video working, but want it to auto play as soon as the page loads, what is the param I need to set?

Hi there,

I'm trying to get image thumbnails working from video feeds coming into the site. I'm using a static image here for testing purposes.

print swf($node->field_emvideo[0]['value'],array(
'params' => array( 'height' => '150', 'width' => '240'),
'othervars' => array('image' => 'http://mindymedia.clearerchannel.org/sites/mindymedia.clearerchannel.org/files/250_mule.jpg'),
)
);

I get the following output on my page. So it seems that the params is working but not the othervars. Can anyone shed any light as to why?

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swftools-1269974474-6" height="150" width="240">
<param name="movie" value="http://mindymedia.clearerchannel.org/sites/all/modules/swftools-DRUPAL-6--3/shared/flash_media_player/player-viral.swf">
<param name="allowScriptAccess" value="sameDomain">
<param name="wmode" value="opaque">
<param name="bgcolor" value="#FFFFFF">
<param name="scale" value="showall">
<param name="quality" value="autohigh">
<param name="align" value="l">
<param name="allowfullscreen" value="true">
<param name="base" value="http://mindymedia.clearerchannel.org/sites/mindymedia.clearerchannel.org/files/">
<param name="play" value="true">
<param name="menu" value="false">
<param name="loop" value="true">
<param name="flashvars" value="width=240&amp;height=150&amp;file=https:%2F%2Fwww.northern-indymedia.org%2Fsystem%2Fvideo%2F2010%2F03%2F11%2F564%2Flilac_film__final_edit_.wmv.flv">
<!--[if gte IE 7]>-->
<object type="application/x-shockwave-flash" data="http://mindymedia.clearerchannel.org/sites/all/modules/swftools-DRUPAL-6--3/shared/flash_media_player/player-viral.swf" id="swftools-1269974474-6" height="150" width="240">
<param name="allowScriptAccess" value="sameDomain">
<param name="wmode" value="opaque">
<param name="bgcolor" value="#FFFFFF">
<param name="scale" value="showall">
<param name="quality" value="autohigh">
<param name="align" value="l">
<param name="allowfullscreen" value="true">
<param name="base" value="http://mindymedia.clearerchannel.org/sites/mindymedia.clearerchannel.org/files/">
<param name="play" value="true">
<param name="menu" value="false">
<param name="loop" value="true">
<param name="flashvars" value="width=240&amp;height=150&amp;file=https:%2F%2Fwww.northern-indymedia.org%2Fsystem%2Fvideo%2F2010%2F03%2F11%2F564%2Flilac_film__final_edit_.wmv.flv">
<!--<![endif]-->
<p>You are missing some Flash content that should appear here! Perhaps your browser cannot display it, or maybe it did not initialize correctly.</p>
<!--[if gte IE 7]>-->
</object>
<!--<![endif]-->
</object>

I am using SWF tools 6.x-2.5

According to the manual I should be able to set the object id by using 'othervars'. Here is my code:

print swf('checklogin.swf',
          array(
            'params' => array('width' => '200', 'height' => '200'),
            'othervars' => array('id' => 'xxx'),
          )
        );

However the browser outputs this:
<div id="swf-xxx" class="swftools-wrapper swftools-swf"><div class="swftools">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="222" height="200" id="swf13061372181">
<param name="movie" value="checklogin.swf" />
id="xxx"<param name="allowScriptAccess" value="sameDomain" />

The id info is shown in the tag bit not in the 'object'. Here it shows an arbitrary id: id="swf13061372181"

Any suggestions what I do wrong?

You are aren't doing anything 'wrong' as it were. It's behaving as it was designed to.

What are you trying to do?

Edit: or rather, why? As in, what is your goal in setting an ID on the object? If the id is set on the parent, you can still target the object with both css and js.

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

Hi Jay!

What I am trying to achieve: When my flash embedded object can be referenced by ID I am able to pass a variable from html/java script to my flash object via java script: getElementById('flash') ... and the flash object code ExternalInterface.addCallback(methodName, instance, method);
If you are familiar with that. So I really need the my ID to be IN the object.
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="222" height="200" id="myID">

You can do something like this:

document.getElementbyId("YOURID").getElementsByTagName("object")[0]

...or something along those lines. Sorry, I've been using jquery for so long, I'm having troubles remembering how exactly to do it in pure javascript. I think I'm wrong with the index ([0]), there is a different way to do it, but basically I'm saying it can be done.

Or if you want to do it in jquery (which you can, since you are using Drupal), you can use:

$("#YOUR_ID object").eq(0);

That will give you the element you are looking for.

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

Thanks for the input. Haven't looked into jqueury yet. Will so now :)

However my previous problem remains. The SWF tool object still gets a random id instead of "YOURID". I guess the module doesn't provide for that so I guess I just change the code in "swftools.module" even though that is not really a good way to do it...

Best regards!

Hello,

My english is not good.

I am configure the Flow Player 3 in my site.

How can preview on the player first frame on my video?

Please help

Bye

eliasC