I'm trying to add a column on the catalog_product grid on magento I want my column to contain the number of images of each product.

I have created an extension for that.

In config.xml


In observer.php :

public function addNbImagesColonne($observer){
            $block = $observer->getEvent()->getBlock();

            if(get_class($block) == 'Mage_Adminhtml_Block_Catalog_Product_Grid'){
                $block->addColumnAfter('nbrImages', array(
                        'header'    => "Number of images",
                        'width'     => '100px',
                        'index'     => 'nbImage',
                        'type'        => 'text',
                        'filter_index' => 'cpemg.nbImage',
                ), 'status');
public function catalogProductGridCollectionLoadBefore($observer)

        $collection = $observer->getCollection();//get the collection

        $collection->calculateSizeWithoutGroupClause = true;//Correct the number of row return
        $select = $collection->getSelect();

                array('cpemg' => Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_media_gallery')),
                        'nbImage' => new Zend_Db_Expr(' COUNT(cpemg.value_id)'),


My column is working fine but when I try to filter products by the number of image, I have no error print and my grid does not show.

Could you tell me where my mistake is ?


filter_index is afaik the column you want to filter on. Did you add a column nbImage to your table catalog_product_entity_media_gallery? If not, try to remove the cpemg right before

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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