swf Files and Drupal 7

.swf files contain the information necessary to display Flash animation. In Drupal 6, there was a module that provided an api function swf() to generate the embed code needed. That module isn't available in Drupal 7 ... so what to do?

There are a number of diverse options for how to handle swf files. In fact, there is even a plugin for Flash available from Google, Swiffy, that enables the creation of HTML5-format video, for use in the canvas tag, rather than Flash, which still isn't viable for IOS devices, like iPhone and iPad. In my case, the swf files exist already, and I don't have Flash development tools available. Google does provide an online conversion tool, but it is currently in beta and only supports a file up to 1Mb. 

Another complication is that I need the videos to play in blocks. On the D6 version of the site, this was done using the swf() function and having the text format of the blocks be php. I'd rather not use php in content these days, so I needed a different solution.

I decided to go the route of a pseudo-token, pseudo because I'm not going to use the token functionality in core. I could, but this approach seemed like it would make for a more interesting article.

Step 1

Define a string that will be the token. Wherever I want a swf player embedded within content, I'm going to put

#SWF#<swf_name>#

where <swf_name> is the name of the swf file, without the .swf extension.

Step 2

I'll need some code to check for the token and, finding it, replace it with the markup I need. I'm going to do this on-demand, when the content is loaded. Since I'll be using this token in blocks, I'm going to start with the block preprocessor in template.php in my theme directory.

$pos = strpos($vars['content'], '#SWF#');

This looks for #SWF# in the content, returning FALSE if it isn't found, in which case we're done. If a match is found, the next step is to find the closing # after the filename. 

$pos2 = strpos($vars['content'], '#', $pos + 5);

There is the possibility that the token will be malformed due to a typo, and not have the closing hash. We'll only process well-formed tokens. 

if ($pos2 !== FALSE) {

It's best to use !== FALSE to test for failure in any function that can return 0 as meaningful, so as not to confuse the two. The next step is to extract the filename.

$movie = substr($vars['content'], $pos + 5, $pos2 - $pos - 5);

The string selected is what falls between the token #SWF# and the final hash. I'll then call a function that inserts the filename in markup and returns that markup.

$movie_markup = insertHTML($movie);

The code for insertHTML appears below in the full listing. Finally, I'll replace the token in the template content.

$vars['content'] = str_replace('#SWF#' . $movie . '#', $movie_markup, $vars['content']);

And that's it! Here is the complete code.

function kamaron_7x_preprocess_block(&$vars) {
// if the block has a placeholder #SWF#swfname# replace it here
  $pos = strpos($vars['content'], '#SWF#');
  if ($pos !== FALSE) {
    // find closing #
    $pos2 = strpos($vars['content'], '#', $pos + 5);
    if ($pos2 !== FALSE) {
      $movie = substr($vars['content'], $pos + 5, $pos2 - $pos - 5);
      $movie_markup = insertHTML($movie);
      $vars['content'] = str_replace('#SWF#' . $movie . '#', $movie_markup, $vars['content']);
    }
  }
}

function insertHTML($movie,$width=320,$height=256,$text='') {
  $markup = '<embed width="190" height="160" src="/video/' . $movie . '.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" align="middle" play="false" loop="false" scale="showall" wmode="window" devicefont="false" bgcolor="#66a2df" name="' . $movie . '" menu="true" allowfullscreen="false" allowscriptaccess="sameDomain" base="/video" salign="" type="application/x-shockwave-flash">';
  return $markup;
}

©2005-2017 Ayen Designs. Underlying theme by Kiwi Themes.