Support for SWFObject

elliotttt - December 20, 2007 - 19:10
Project:Mediafield Display
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

I was curious and modified the module with something like this:

<?php

/**
* Implementation of hook_field_formatter_info().
*/
function mediafield_display_field_formatter_info() {
  $formatters = array();
  if (module_exists('audiofield')) {
    $path = drupal_get_path('module', 'mediafield_display') .'/players/';
    if (file_exists($path .'1pixelout.swf')) {
      $formatters['1pixelout'] = array(
        'label' => t('1 Pixel Out player'),
        'field types' => array('file_audio'),
      );
      $formatters['1pixelout_plus'] = array(
        'label' => t('1 Pixel Out player plus download link'),
        'field types' => array('file_audio'),
      );
    }
    if (file_exists($path .'button.swf')) {
      $formatters['button'] = array(
        'label' => t('Button player'),
        'field types' => array('file_audio'),
      );
      $formatters['button_plus'] = array(
        'label' => t('Button player plus download link'),
        'field types' => array('file_audio'),
      );

    }
  }
  return $formatters;
}

/**
* Implementation of hook_field_formatter().
*/
function mediafield_display_field_formatter($field, $item, $formatter) {

  if (!module_exists('audiofield')) {
    return t('Audio Field module is required to display audio files.');
  }
  require_once(drupal_get_path('module', 'audiofield') .'/multimediafile.inc');

  if (!isset($item['fid']) || empty($item['fid'])) {
    return '';
  }
  $file = _field_file_load($item['fid']);

  switch ($formatter) {
    case '1pixelout':
    case 'button':
      $output = theme('mediafield_display_'. $formatter, $file, $item, $field);
      break;
    case '1pixelout_plus':
    case 'button_plus':

      $output = theme('mediafield_display_'. substr($formatter, 0, strlen($formatter) - 5), $file, $item, $field);
      $output .= theme('audiofield', $file, $item, $field);
      $js_url = $base_url . drupal_get_path('module', 'mediafield_display').'/swfobject.js';
      drupal_add_js($js_url);
      break;
  }

  return $output;
}

/**
* Theme a 1pixelout audio file.
*/
function theme_mediafield_display_1pixelout($file, $item, $field) {
  global $base_url;

  $options = array();
  $options['soundFile'] = check_url($base_url .'/'. $file['filepath']);
  $url = $base_url .'/'. drupal_get_path('module', 'mediafield_display') .'/players/1pixelout.swf';

  $flashvars = array();
  foreach ($options as $key => $val) {
    $flashvars[] = rawurlencode($key) .'='. rawurlencode($val);
  }
  $flashvars = implode('&amp;', $flashvars);
  $flashvars = str_replace("soundFile=", "", $flashvars);

$output = <<<EOT
<div id="$flashvars">
<strong>You need to upgrade your Flash Player</strong>
</div>

<script type="text/javascript">
// <![CDATA[

var so = new SWFObject("$url", "sotester", "290", "24", "9", "#EEEEEE");
so.addVariable("soundFile", "$flashvars"); // this line is optional, but this example uses the variable and displays this text inside the flash movie
so.write("$flashvars");

// ]]>
</script>
EOT;

  return $output;
}

/**
* Theme a button audio file.
*/
function theme_mediafield_display_button($file, $item, $field) {
  global $base_url;

  $options = array();
  $options['song_url'] = check_url($base_url .'/'. $file['filepath']);
  $options['song_title'] = check_plain($file['description']);
  // str_replace() is to fix issue in Drupal 5.2.
  $url = $base_url .'/'. drupal_get_path('module', 'mediafield_display') .'/players/button.swf?'. str_replace('http%3A/%252F', 'http://', drupal_query_string_encode($options));

$output = <<<EOT
<object type="application/x-shockwave-flash" data="$url" width="17" height="17">';
  <param name="movie" value="$url" />
  <param name="wmode" value="transparent" />
  <param name="quality" value="high" />
</object>
EOT;

  return $output;

And added the swfobject js to the module folder, and it works, is there a better way to do this? Or would this be a worthwhile feature to add to this module?

 
 

Drupal is a registered trademark of Dries Buytaert.