I'm learning to develop for Drupal 7 and figured I'd start with something simple, like a custom formatter. I have a content type that contains several boolean fields indicating if the content type belongs to certain class of objects. When I create a view of the content types, all the boolean fields show a 1 or a 0, when I would like it to display an image instead. I created the following CheckBoxFormatter.module file
<?PHP
/**
* @file
* Creates a custom format for boolean fields, a checked checkbox for true
* and unchecked for false
*/
/**
* Implements hook_field_formtatter_info()
*/
function CheckBoxFormatter_field_formatter_info() {
return array(
'CheckBoxFormatter' => array(
'label' => t('Checkboxes'),
'field types' => array('boolean')
),
);
}
/**
* Implements hook_field_formatter_view()
*/
function CheckBoxFormatter_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display) {
$elements = array();
foreach($items as $delta => $item) {
//Build variables array for formatter
$element = array(
'#obj_type' => $obj_type,
'#object' => $object,
'#field' => $field,
'#instance' => $instance,
'#langcode' => $langcode,
'#items' => $items,
'#display' => $display
);
if (function_exists($function = "{$display['module']}_field_formatter_{$display['type']}")) {
$elements[$delta] = array(
'#markup' => $function($element)
);
}
}
return $elements;
}
/**
* creates the actual checkboxes
*/
function CheckBoxFormatter_field_formatter_CheckBoxFormatter($variables) {
$img = array("<URL TO UNCECKED>","<URL TO CHECKED>");
$disp = array("Unchecked","Checked");
$element = array();
foreach($variables['#items'] as $delta => $item) {
$element[$delta] = array(
'#markup' => '<img src="' . $img[$item['value']] . '" alt="' . $disp[$item['value']] . '" />'
);
}
return $element;
}
I uploaded this to all/sites/modules/CheckBoxFormatter. I was able to activate the module in the modules section of my drupal site. Now, when I go back to the view I created, this new formatter doesn't show up. I also do not see it as an option under Manage Display for the content type. What am I doing wrong?