1

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?

1 Answer 1

2

Finally found it. I knew it had to be something simple. I was using the wrong field type. Turns out it should have been list_boolean instead of simply boolean. I also simplified the code

    <?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(
                'checkboxes' => array(
                    'label' => t('Checkboxes'),
                    'field types' => array('list_boolean')
                ),
            );
        }

        /**
         * Implements hook_field_formatter_view()
         */
        function CheckBoxFormatter_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display) {
            $output = array();
            $img = array("uncheck.jpg","check.jpg");
            $disp = array("Unchecked","Checked");


            if($display['type'] == "checkboxes") {
                foreach($items as $delta => $item) {
                    $output[$delta] = array(
                        '#markup' => '<img src="' . $img[$item['value']] . '" alt="' . $disp[$item['value']] . '" />'
                    );
                }
            }
            return $output;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.