Magento – add a new action in grid

Here, how to add new action in grid like ‘Edit’ link at admin grid.
On this example, we will add a “schedule” action in product admin grid.

We will overrite this class /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php.

Declare the rewrite class on the /app/code/local/Adin/Catalog/etc/config.xml
and add

<global>
      <blocks>
            <adminhtml>
                <rewrite>
                    <catalog_product_grid>Adin_Catalog_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
            </adminhtml>
        </blocks>
</global>

Then create your rewritted class /app/code/local/Adin/Catalog/Block/Adminhtml/Catalog/Product/Grid.php.

<?php
class Adin_Catalog_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {
}
?>

Now, rewrite the _prepareColumns() function, find the ‘action’ column an add an entry in the actions array

  $this->addColumn('action',
            array(
                'header'    => Mage::helper('catalog')->__('Action'),
                'width'     => '50px',
                'type'      => 'action',
                'getter'     => 'getId',
                'actions'   => array(
                    array(
                        'caption' => Mage::helper('catalog')->__('Edit'),
                        'url'     => array(
                            'base'=>'*/*/edit',
                            'params'=>array('store'=>$this->getRequest()->getParam('store'))
                        ),
                        'field'   => 'id'
                    ),
                    array(
                        'caption' => Mage::helper('catalog')->__('Schedule'),
                        'url'     => array(
                            'base'=>'*/*/schedule',
                            'params'=>array('store'=>$this->getRequest()->getParam('store'))
                        ),
                        'field'   => 'id'
                    ),
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
            ));

In our example, we just add this part

array(
                        'caption' => Mage::helper('catalog')->__('Schedule'),
                        'url'     => array(
                            'base'=>'*/*/schedule',
                            'params'=>array('store'=>$this->getRequest()->getParam('store'))
                        ),
                        'field'   => 'id'
                    ),

One last thing, at the end of the _prepareColumns function, change

return parent::_prepareColumns();

by

return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();

Symfony2 sonata custom CRUD template

It is possible to change templates use by default.

First, override the controller action method, in your AdminBundle/Controller/SecteurAdminController.php file

 /**
     * return the Response object associated to the list action
     *
     * @return Response
     */
    public function listAction()
    {
        if (false === $this->admin->isGranted('LIST')) {
            throw new AccessDeniedException();
        }

        $datagrid = $this->admin->getDatagrid();
        $formView = $datagrid->getForm()->createView();

        // set the theme for the current Admin Form
        $this->get('twig')->getExtension('form')->setTheme($formView, $this->admin->getFilterTheme());

        //custom code from here
        $total = 0;
        $enable = 0;
        $new = 0;
        $site = $this->admin->site;

        $repository = $this->getDoctrine()->getRepository('TripixAdminBundle:Annonce');
        $total = $repository->getNombreAnnonceTotal($site);
        $enable = $repository->getNombreAnnonceEnabled($site);
        $new = $repository->getNombreAnnonceNew($site);



        return $this->render('TripixAdminBundle:CRUD:list_secteur.html.twig', array(
            'action'   => 'list',
            'form'     => $formView,
            'datagrid' => $datagrid,
            'total'     => $total,
            'enable'    => $enable,
            'new'       => $new,
        ));
    }

The import thing on previous code is the render() fonction, the first parameter is the template to use.
In our example, the template file will be /AdminBundle/Ressources/view/CRUD/list_secteur.html.twig

You can use the /AdminBundle/Ressources/view/CRUD/base_secteur.html.twig file as example for your custom template.

Parent function to override on the controller are here : /vendor/bundles/Sonata/AdminBundle/Controller/CRUDController.php