CCK Formatters: Creating an Audio Widget from a URL

CCK formatters are useful for theming content in a sustainable way. Instead of creating piles of template files to format content types, you can create re-usable CCK formatters and select how your fields are formatted using the "Display Fields" tab of a content type.

For demo code and useful slides about creating CCK formatters, take a look at the CCK formatters session that Jody Hamilton gave at Drupal Design Camp in June.

I recently created a CCK formatter module to display audio widgets from raw audio files. The MP3 Player module comes with a CCK formatter to display filefield audio uploads as audio widgets. However, sometimes audio files can be really large, particularly if they're really long and uploading them as a filefield is not ideal. Sometimes, you want to allow your users to upload audio files via FTP and then reference them using a URL.

So I created a formatter to display the URL to the audio file as an audio widget. The formatter is available to text fields rather than file fields. In this case, creating the CCK formatter was easy because the MP3 player module already provides a theme function for turning audio files into audio widgets. Below is the code from the module file


<?php 

/**
 * Implementation of CCK's hook_field_formatter_info().
 * 
 */
function mp3_field_field_formatter_info() {
  
$formatters = array(
    
'audio_widget' => array(
      
'label' => t('MP3 Audio Widget'),
      
'field types' => array('text'),
      
'description' => t('Displays an MP3 file upload as an audio file'),
    ),
  );
  return 
$formatters;
}

/**
 * Implementation of hook_theme().
 */
function mp3_field_theme() {
  return array(
    
'mp3_field_formatter_audio_widget' => array(
      
'arguments' => array('element' => NULL),
    ),
  ); 
}

/**
 * Theme function for a custom formatter.
 */
function theme_mp3_field_formatter_audio_widget($element) {
  
$output theme('mp3player''Default'$element['#item']['value']);
  return 
$output;
}
?>

After enabling the mp3_field module above, I choose the "MP3 Audio Widget" formatter on the "Display Fields" tab of the podcast content type. I can move around the field in the "Manage Fields" tab just like any other CCK field. You resulting page looks something like this:


St. Thomas's Anglican Church Audio Player

Comments

Alternatively, you could use filefield + imce + filefield_sources to add the file you uploaded with ftp as a filefield entry and use the MP3 Player module's formatter.

Wow, I never knew it was so easy. I'm going to be using this a lot more.

Can you export CCK display settings as features?

@Wim Mostrey, We recently came across filefield_sources and it certainly looks like a good alternative to this approach. It has the advantage of allowing a user to select a file from the files directory rather than having to know the URL to the file.

@dalin, Display settings are exported along with other CCK data when you export a content type or create a feature.

@Wim, not only can you use a feature to export te cck settings but you can use the .module function created by the feature to house your functions, so only one module need to be created. just dont touch the

<?php
include_once('iFEATURES.features.inc');

?>
at the top fo the generated file