Hi,

I am looking for a way to extend the list of formatters of a given CCK-field without hacking the module itself. I want to create a custom module which extends the formatter-list of a CCk field.

To be more concrete:
I am using the filefield.module but I want a different display of the files within custom views. I can create a custom theme for the display but this will affect the display of all file-fields.

So my idea was to add a custom formatter to the filefield.module which I can intercept in my theming-function, but I don't want to hack the module itself to be as compatible as possible.

Or is there a simpler way to do what I want?

Thanks in advance
Stephan

Comments

maastrix’s picture

subscribing

ultimike’s picture

subscribing

willistg’s picture

I've been looking into this too. And I have yet to come up with anything. I'm no expert, but I'm starting to believe that it would have to be supported in the CCK framework in order for this to be possible.

Or as you said, hack the module. :(

Thomas G. Willis

dodorama’s picture

Hacking the mediafield_display.module I ended up with this custom module that extend the filefields formatters. After installing the module two extra formatters are available. Plain Text and File Type Icon.
Plain Text simply returns the link to the file as plain text so that you can use it inside <a> tags.
File type icon requires to drop file type icons inside your_theme/images directory. These images must be named using the file extension (i.e. mov.png - mp3.png etc.).
The file type icon formatter displays an icon that links to the file.

Create two files:

  • filefield_display.module
  • filefield_display.info

and copy and paste the code below.

filefield_display.module

<?php

/**
 * Implementation of hook_field_formatter_info().
 */
function filefield_display_field_formatter_info() {
  $formatters = array();
  if (module_exists('filefield')) {
      $formatters['plain'] = array(
        'label' => t('Plain Text'),
        'field types' => array('file'),
      );
      $formatters['filetype'] = array(
        'label' => t('File Type Icon'),
        'field types' => array('file'),
      );
  }
  return $formatters;
}

/**
 * Implementation of hook_field_formatter().
 */
function filefield_display_field_formatter($field, $item, $formatter) {
  if (!module_exists('filefield')) {
    return t('File Field module is required to display audio files.');
  }
  require_once(drupal_get_path('module', 'filefield') .'/filefield.module');
  
  if(!empty($item['fid']) && $item['list']) {
    $file = _filefield_file_load($item['fid']);
  }
  if (!empty($file['filepath'])) {
    return theme('filefield_display_'. $formatter, array_merge($file, $item));
  }
  
}

function theme_filefield_display_plain($file) {
  if (is_file($file['filepath']) && $file['list']) {
    $path = ($file['fid'] == 'upload')
            ? file_create_filename($file['filename'], file_create_path($field['widget']['file_path']))
            : $file['filepath'];

    $url = file_create_url($path);
    $name = $file['filename'];
    $desc = $file['description'];
    return check_url($url);
  }
  return '';
}

function theme_filefield_display_filetype($file) {
    
    if (is_file($file['filepath']) && $file['list']) {
      
      $ext = array_pop(explode('.',$file['filename']));
      $known_extensions = array('0','ace','aif','ai','ani','asf','asp','avi','bak','bat','bin','bmp','bz2','bz','cab','cdr','cfg','com','conf','cpt','css','cur','dat','db','dcr','dic','diff','dir','dll','dmg','doc','dwg','edir','eml','eps','exe','fla','flv','fon','gif','gz','hqx','html','htm','ico','inc','ini','iso','jpeg','jpg','js','lnk','log','m3u','mdb','midi','mid','mov','mp3','mpeg','mpg','nfo','odb','odc','odf','odg','odm','odp','ods','odt','ogg','otg','oth','otp','ots','ott','patch','pdf','php3','php','phtml','pl','png','pps','ppt','psd','pwl','qt','ram','ra','rar','reg','rpm','rtf','sh','shtml','sit','sql','svg','swf','sxc','sxi','sxw','sys','tar','tgz','tiff','tif','tmp','tpl','ttf','txt','wav','wma','wmv','wp','xls','xml','zip');
      if (!in_array($ext, $known_extensions))  {
        $ext = 0;
      }
      $imagepath = base_path() . path_to_theme() . '/images/'. $ext .'.png';
      
      $path = ($file['fid'] == 'upload')
              ? file_create_filename($file['filename'], file_create_path($field['widget']['file_path']))
              : $file['filepath'];

            $url = file_create_url($path);
            $name = $file['filename'];
            $desc = $file['description'];
      return '<a href="'. check_url($url) . '">' . '<img src="' . $imagepath . '" /></a>';
    }
    return '';
}

filefield_display.info

; $Id: mediafield_display.info,v 1.1 2007/07/26 19:06:52 dodo Exp $
name = File Field Display
description = Adds display options for file fields.
dependencies = content
package = CCK

; Information added by drupal.org packaging script on 2007-08-13
version = "5.x-1.x-dev"
project = "filefield_display"
datestamp = "1187006824"


becw’s picture

As seen in the example filefield_display.module above, CCK actually provides two hooks to help you build custom field formatters: hook_field_formatter_info() and hook_field_formatter(). These are documented in an example included with CCK--check out lines 311 through 456 of cck/examples/example_field.php.

While making a custom formatter in Drupal 6 for FileField fields, I found that I could use hook_field_formatter_info() and a theme function, theme_hook_formatter_formatterName() (which I also had to mention in my module's hook_theme()). It looks something like this stub:


/**
* Implementation of hook_field_formatter_info().
*/
function myModule_field_formatter_info() {
  $formatters = array();
  if (module_exists('filefield')) {
      $formatters['myFormatter'] = array(
        'label' => t('My Formatter'),
        'field types' => array('file'),
        'multiple values' => CONTENT_HANDLE_CORE,
      );
  }
  return $formatters;
}

/**
* Theme function for myFormatter from hook_field_formatter_info.
* @param $element
*   an array of formatter info and the item to theme. look in $element['#item'] for the field item to theme.
*/
function theme_myModule_formatter_myFormatter($element) {
  return "themed element";  
}

/**
 * Implementation of hook_theme().
 */
function myModule_theme($existing, $type, $theme, $path) {
  return array(
    'myModule_formatter_myFormatter' => array(
      'arguments' => array('element' => NULL),
    ),
  );
}

yang_yi_cn’s picture

a small correction,
now

        'field types' => array('file'),

to

        'field types' => array('filefield'),
deciphered’s picture

I know this is an old topic, but I just released a new module for exactly this purpose, adding custom formatters without the need of a custom module. It's aptly named Custom Formatters.