0

I created to the category edit page a custom tab which allow the user to select one block and set for it a position. In my database I added a link table with the block_id, category_id and position.

So, I inspirited my work with the default category_products tab and then everything works except the sort/order of my blocks.

I tried :

  • die('hello') in the _addColumnFilterToCollection function : it resulted that the function is never called

Here is my tab code :

class Vendor_Catalog_Block_Adminhtml_Catalog_Category_Tab_About extends Mage_Adminhtml_Block_Widget_Grid
{
    protected $_aboutCollection = null;

    /** Mage_Cms_Model_Resource_Block_Collection $_aboutCollection */

    public function __construct()
    {
        parent::__construct();
        $this->setId('catalog_category_about');
        $this->setDefaultSort('block_order');
        $this->setDefaultDir('ASC');
        $this->setUseAjax(true);
    }

    /**
     * Get current category
     * @return Mage_Catalog_Model_Category
     */
    public function getCategory()
    {
        return Mage::registry('current_category');
    }

    /** {@inheritdoc} */
    protected function _addColumnFilterToCollection($column)
    {
        // Set custom filter for in category flag
        if ($column->getId() == 'block_used_in_category') {
            $blockIds = $this->_getUsedBlocks();
            if (empty($blockIds)) {
                $blockIds = 0;
            }
            if ($column->getFilter()->getValue()) {
                $this->getCollection()->addFieldToFilter('block_id', array('in' => $blockIds));
            } elseif (!empty($blockIds)) {
                $this->getCollection()->addFieldToFilter('block_id', array('nin' => $blockIds));
            }
        } else {
            parent::_addColumnFilterToCollection($column);
        }

        return $this;
    }

    /** {@inheritdoc} */
    protected function _prepareCollection()
    {
        if (!$this->_aboutCollection) {
            $categoryId = (int)$this->getRequest()->getParam('id', 0);
            $aboutTableName = Mage::getSingleton('core/resource')->getTableName('vendor_catalog/about');

            /** @var Mage_Cms_Model_Resource_Block_Collection $collection */
            $collection = Mage::getModel('cms/block')->getCollection();
            $collection->getSelect()
                ->joinLeft(
                    array('catalog_block_about' => $aboutTableName),
                    'main_table.block_id = catalog_block_about.block_id AND catalog_block_about.category_id = ' . $categoryId,
                    array('order')
                );
            $collection->addFieldToFilter('identifier', array('like' => 'apropos_%'));
            $this->_aboutCollection = $collection;
        }

        $this->setCollection($this->_aboutCollection);

        return parent::_prepareCollection();
    }

    /** {@inheritdoc} */
    protected function _prepareColumns()
    {
        $this->addColumn('block_used_in_category', array(
            'header_css_class' => 'a-center',
            'type' => 'checkbox',
            'name' => 'block_used_in_category',
            'values' => $this->_getUsedBlocks(),
            'align' => 'center',
            'index' => 'block_id',
            'sortable' => true
        ));
        $this->addColumn('block_identifier', array(
            'header' => Mage::helper('catalog')->__('Identifier'),
            'sortable' => true,
            'width' => '60',
            'index' => 'identifier'
        ));
        $this->addColumn('block_title', array(
            'header' => Mage::helper('catalog')->__('Title'),
            'sortable' => true,
            'width' => '60',
            'index' => 'title'
        ));
        $this->addColumn('block_order', array(
            'header' => Mage::helper('catalog')->__('Position'),
            'sortable' => true,
            'width' => '1',
            'index' => 'order',
            'editable' => true,
            'renderer' => 'adminhtml/widget_grid_column_renderer_input',
        ));
        return parent::_prepareColumns();
    }

    /**
     * Get ids of used blocks in category
     *
     * @return array
     */
    protected function _getUsedBlocks()
    {
        $array = [];
        if (!$this->getCollection()) {
            $this->_prepareCollection();
        }

        foreach ($this->getCollection() as $item) {
            // If the item in the collection have an order, it's because it's activated
            if ($item->getOrder() !== null) {
                $array[] = $item->getId();
            }
        }

        return $array;
    }

    /** {@inheritdoc} */
    public function getGridUrl()
    {
        return $this->getUrl('*/*/gridAbout', array('_current' => true));
    }

    /**
     * Get list of selected Blocks with it's position
     *
     * @return array
     */
    public function getUsedBlocks()
    {
        $categoryId = (int)$this->getRequest()->getParam('id', 0);

        $collection = Mage::getModel('vendor_catalog/about')
            ->getCollection()
            ->addFieldToFilter('category_id', $categoryId)
            ->load();

        $array = [];
        foreach ($collection as $item) {
            $array[$item->getId()] = $item->getData();
        }

        return $array;
    }
}

Update :

I modified my columns like :

$this->addColumn('block_title', array(
            'header' => Mage::helper('catalog')->__('Title'),
            'sortable' => true,
            'width' => '60',
            'index' => 'title',
            'filter_condition_callback' => array($this, '_titleFilter'),
        ));

and add this function :

protected function _titleFilter($collection, $column)
    {
        $filterValue = $column->getFilter()->getValue();

        if (!is_null($filterValue)) {
            $collection->addFieldToFilter('title', $filterValue);
        }
    }

The query is ok, the $collection is correct but it reload the collection with the function _prepareCollection().

  • It seems the function like the "following this post" works but the collection doesn't reload. The query was OK – Kpone Sep 11 '17 at 13:21
  • have you tested your die statement before the if condition? – Priyank Sep 11 '17 at 13:42
  • Yeah sure. _addColumnFilterToCollection is never executed but _titleFilter is executed, generate a good sql when I do a dump of the collection sql but it doesn't reload the collection showed to the front. I found out it reload everything, then the function _prepareCollection is reused to refresh the collection and don't take care of the modify from the _titleFilter function – Kpone Sep 11 '17 at 14:11

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.