0

I want to link cms/blocks to categories. Then I created a Grid inspirited by the Product grid, this show blocks and this grid allow 2 things (select blocks with a checkbox and modify it's position with a textfield).

enter image description here

But when the user click on save I don't know how to get this grid values. Can you help me please ?

Here is the content of my grid class :

class Namespace_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('order');
        $this->setUseAjax(true);
    }

    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;
    }

    protected function _prepareCollection()
    {
        if (!$this->_aboutCollection) {
            $categoryId = (int)$this->getRequest()->getParam('id', 4);
            $aboutTableName = Mage::getSingleton('core/resource')->getTableName('namespace_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();
    }

    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'
        ));
        $this->addColumn('block_identifier', array(
            'header' => Mage::helper('catalog')->__('Block id'),
            'sortable' => true,
            'width' => '60',
            'index' => 'identifier'
        ));
        $this->addColumn('block_title', array(
            'header' => Mage::helper('catalog')->__('Block title'),
            'sortable' => true,
            'width' => '60',
            'index' => 'title'
        ));
        $this->addColumn('block_order', array(
            'header' => Mage::helper('catalog')->__('Block position'),
            'sortable' => true,
            'width' => '1',
            'index' => 'order',
            'editable' => true,
            'renderer'  => 'adminhtml/widget_grid_column_renderer_input'
        ));
        return parent::_prepareColumns();
    }

    protected function _getUsedBlocks()
    {
        $array = [];
        if (!$this->getCollection()) {
            $this->_prepareCollection();
        }

        foreach ($this->getCollection() as $item) {
            if ($item->getOrder() !== null) {
                $array[] = $item->getId();
            }
        }

        return $array;
    }
}

This will pick all CMS block with an identifier starting by "apropos_" and join the results with a new table I created which contain (block_id,category_id,order).

Now I want, when the user click on save, saving the grid informations (the order, and if the checkbox is checked). So to save this I create a new observer about the event "catalog_category_save_after" but in my function when I write a Mage::app()->getRequest()->getPost() I don't get the informations.

I tried to follow this post but I don't understand how it results to get it's data by using the same method as mine.

0

I found out that the only clean way to do this is by passing by the template app/design/adminhtml/default/default/template/catalog/catego‌​ry/edit/form.phtml and modify the JS to create a front input like the category product tab does.

I create generic functions to did this instead of copy/past default functions. This may allow me to add multiple new tabs easier.

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.