swf() syntax

Last modified: March 29, 2009 - 00:24

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 height and width is:

<?php
print swf('myMovie.swf', array('params' => array('height' => '200', 'width' => '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 a flash var to set the thumbnail image.
<?php
print swf('test1.flv',
          array(
           
'params' => array('width' => '400', 'height' => '300'),
           
'flashvars' => array('image' => 'picture.jpg'),
          )
        );
?>

Is there a method to use

Jay Matwichuk - January 29, 2009 - 09:05

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

Kinda sorta

kingandy - February 4, 2009 - 09:43

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

I actually managed to get it

Jay Matwichuk - February 24, 2009 - 01:24

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.

And for Drupal 5

vivianspencer - March 23, 2009 - 17:06

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

samwich - June 23, 2009 - 16:43

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" />',
        ),
    )
);
?>

Preventing cache

ncatdesigner - February 22, 2009 - 16:50

How Can I prevent a swf to being in cache?

Workaround

kingandy - February 23, 2009 - 09:56

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

Not working...

ncatdesigner - February 24, 2009 - 21:56

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...

The ? is getting encoded

rcharamella - June 24, 2009 - 15:59

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?

 
 

Drupal is a registered trademark of Dries Buytaert.