Displaying CCK filefields with a 1pixelout audio player

Last modified: October 24, 2008 - 14:20

This is a small module that adds an extra Display option (in technical terms, a formatter) to CCK filefields to show the 1pixelout player. It's based almost entirely on code from the cck_extras module.

Dependencies: Obviously, you need the filefield module. You also need the audio module, as this module goes to find the audio player in the folder for the audio module (but this can be changed: see comment in the code).

Notes: It's up to the site developer to make sure the filefield only allows MP3 files. Also, there's no checking of sample rates, so it might simply Not Work if the MP3 file isn't compatible with the player.

The module code is as follows. The info file is left as an exercise for the reader ;)

<?php
/**
* @file
* filefield_audio
* Based on cck_extras
* Defines an audio player widget for use on CCK filefields
*
* Depends on audio.module for the 1pixelout player file.
*
* This will provide an extra option in the field's Display options
* Only use for fields that hold MP3!
*/

/**
* Implementation of hook_field_formatter_info().
*/
function filefield_audio_field_formatter_info() {
  return array(
    'ffa_1pixelout' => array(
      'label' => t('1pixelout player'),
      'field types' => array('file'),
      'module' => 'filefield_audio',
    ),
  );
}

/**
* Implementation of hook_field_formatter().
* Show the 1pixelout player on filefields
*/
function filefield_audio_field_formatter($field, $item, $formatter) {
  if ($formatter == 'ffa_1pixelout') {   
    if(!empty($item['fid'])) {
      $item  =  array_merge($item, _filefield_file_load($item['fid']));
    }
    if (!empty($item['filepath'])) {
      return theme('filefield_audio', $item);
    }
  }
}

/**
* Theming function for the 1pixelout player on filefields
*/
function theme_filefield_audio($file) {
  if (user_access('view filefield uploads') && is_file($file['filepath']) && $file['list']) {
    $path = ($file['fid'] == 'upload')
            ? file_create_filename($file['filename'], file_create_path($field['widget']['file_path']))
            : $file['filepath'];
    global $base_url;
   
    $url = file_create_url($path);
   
    // URL for the player held in audio module
    // change this if the player is somewhere else
    $player_url = $base_url .'/'. drupal_get_path('module', 'audio') .'/players/1pixelout.swf';

    $options['soundFile'] = $url;
 
    $flashvars = array();
    foreach ($options as $key => $val) {
      $flashvars[] = rawurlencode($key) .'='. rawurlencode($val);
    }   
    $flashvars = implode('&amp;', $flashvars);  
   
$output = <<<EOT
<object type="application/x-shockwave-flash" data="$player_url" width="290" height="24" >
<param name="movie" value="$player_url" />
<param name="wmode" value="transparent" />
<param name="menu" value="false" />
<param name="quality" value="high" />
<param name="FlashVars" value="$flashvars" />
</object>
EOT;
    return $output;
  }
  return '';
}

 
 

Drupal is a registered trademark of Dries Buytaert.