Flipkart

Friday, March 18, 2011

extend CCK-fields with custom formatters

We can create new custom formatter for CCK fields using hook_field_formatter_info(), below is the example


/**
* 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('filefield'),
       
'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),
    ),
  );
}
?>

Wednesday, March 16, 2011