1

I'm adding a custom column (from a custom table) to an admin grid with an observer, and the filtering/sorting doesn't work. Adding the 'filter_condition_callback' to my column doesn't work, the function is never called.

I've seen other similar questions but none of them had this particular problem, or they were never answered.


I've made a module that modifies the admin_user grid.

It contains an observer linked to these events :

    <core_collection_abstract_load_before>
        <observers>
            <beforeCollectionLoad>
                <class>Namespace_Adminextra_Model_Observers_Users</class>
                <method>beforeCollectionLoad</method>
            </beforeCollectionLoad>
        </observers>
    </core_collection_abstract_load_before>

    <adminhtml_block_html_before>
        <observers>
            <add_an_field>
                <type>singleton</type>
                <class>Namespace_Adminextra_Model_Observers_Users</class>
                <method>appendContent</method>
            </add_an_field>
        </observers>
    </adminhtml_block_html_before>

So the beforeCollectionLoad method is called so I can join my custom column (from a custom table, not an attribute) :

public function beforeCollectionLoad(Varien_Event_Observer $observer)
{
    $collection = $observer->getCollection();
    if (!isset($collection)) {
        return;
    }
    if ($collection instanceof Mage_Admin_Model_Resource_User_Collection) {
        $collection->getSelect()->joinLeft(
            array('adminextra' => 'digitagri_adminextra'),
            'adminextra.user_id = main_table.user_id',
            array(
                'is_advisor' => 'adminextra.is_advisor',
                'phone' => 'adminextra.phone',
            )
        );
    }
}

This successfully seems to add the column to the collection. The observer first calls the function appendContent, which checks the current grid :

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

    if (!isset($block)) {
        return $this;
    }
    if ($block instanceof Mage_Adminhtml_Block_Permissions_User_Grid) {
        $this->appendCustomColumns($block);
    }
    return $this;
}

Then the column is added :

public function appendCustomColumns($grid){
    $grid->addColumnAfter('is_advisor', array(
        'header' => 'Conseiller',
        'align' => 'left',
        'width' => '50',
        'index' => 'is_advisor',
        'type' => 'options',
        'options'   => array('1' => Mage::helper('adminhtml')->__('Oui'), '0' => Mage::helper('adminhtml')->__('Non')),
        'filter_condition_callback' => array($this,'_filterIsAdvisor'),
    ), 'is_active');
    $grid->sortColumnsByOrder();
}

The column is added and the values are displayed, the 'options' acts as a renderer and correctly displays '1' as Oui and '0' as Non. But the filtering and sorting don't work. No error, no exceptions, it does nothing.

The function called in 'filter_condition_callback' never seems to be called either. I've put a Mage::log to check :

protected function _filterIsAdvisor($collection, $column){
    Mage::log('works?',null,'test.log',true);
}

So what am I doing wrong ?

Filtering and sorting seem incredibly finicky, especially since i'm joining a column from a custom table and not an attribute.

  • There is another way to add/ remove columns from grid via XML. please check github.com/magento-hackathon/GridControl . – Attila Naghi Dec 15 '17 at 14:34
  • I've seen this multiple times. I'll check it out if there is absolutely no other way, I would rather find a "normal" way to do this first. – Grymas Dec 15 '17 at 14:36

Your Answer

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

Browse other questions tagged or ask your own question.