1

I am creating a custom backend module that has a grid. All the functionality is working fine except that the content-header portion is not showing. this means that the "add entry" button also does not appear.

As you can see in the code below, i have added debugging log statements to check if the code is running. for all of them, the statements gets logged as expected... except for the __construct() method in rules.php (the grid container file).

Thank you in advance for the help!

Config.xml:

<config>
    <modules>
        <Mycompany_Mymodule>
            <version>1.0.0</version>
        </Mycompany_Mymodule>
    </modules>

    <global>

        <models>
            <mycompany_mymodule>
                <class>Mycompany_Mymodule_Model</class>
                <resourceModel>mycompany_mymodule_resource</resourceModel>
            </mycompany_mymodule>
            <mycompany_mymodule_resource>
                <class>Mycompany_Mymodule_Model_Resource</class>
                <entities>
                    <rules>
                        <table>mycompany_mymodule_rules</table>
                    </rules>                    
                </entities>
            </mycompany_mymodule_resource>
        </models>

        <resources>
            <mycompany_mymodule_setup>
                <setup>
                    <module>Mycompany_Mymodule</module>
                </setup>
            </mycompany_mymodule_setup>
        </resources>   

        <blocks>
            <mycompany_mymodule>
                <class>Mycompany_Mymodule_Block</class>
            </mycompany_mymodule>
        </blocks>

        <helpers>
            <mycompany_mymodule>
                <class>Mycompany_Mymodule_Helper</class>
            </mycompany_mymodule>
        </helpers>

    </global>

    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Mycompany_Mymodule before="Mage_Adminhtml">Mycompany_Mymodule_Adminhtml</Mycompany_Mymodule>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

    <adminhtml>
        <layout>
            <updates>
                <mycompany_mymodule>
                    <file>mycompany/mymodule.xml</file>
                </mycompany_mymodule>
            </updates>
        </layout>

    </adminhtml>

</config>

Adminhtml.xml:

<config>
    <menu>
        <mycompany_mymodule translate="title" module="mycompany_mymodule">
            <title>Customer Sort</title>
            <sort_order>100</sort_order>
            <children>
                <rules translate="title" module="mycompany_mymodule">
                    <title>Rules</title>
                    <sort_order>10</sort_order>
                    <action>adminhtml/mycompany_mymodule_rules</action>
                </rules>
            </children>

        </mycompany_mymodule>

    </menu>

</config>

RulesController.php:

<?php

class Mycompany_Mymodule_Adminhtml_Mycompany_Mymodule_RulesController extends Mage_Adminhtml_Controller_Action
{

    public function indexAction()
    {

        Mage::log( "Mycompany_Mymodule_Adminhtml_Mycompany_Mymodule_RulesController indexAction" );

        $this->loadLayout();

        $this->_setActiveMenu('mycompany_mymodule');
        $this->_addContent($this->getLayout()->createBlock('mycompany_mymodule/adminhtml_rules_grid'));
        $this->renderLayout();
    }


    public function gridAction(){

        Mage::log( "Mycompany_Mymodule_Adminhtml_Mycompany_Mymodule_RulesController gridAction" );


        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('mycompany_mymodule/adminhtml_rules_grid')->toHtml()
        );
    }

    public function editAction()
    {

        Mage::log( "Mycompany_Mymodule_Adminhtml_Mycompany_Mymodule_RulesController editAction" );


        $this->loadLayout();
        $this->_setActiveMenu('mycompany_mymodule');
        $this->_addContent($this->getLayout()->createBlock('mycompany_mymodule/adminhtml_rules_edit'));

        $this->renderLayout();
    }

    public function newAction()
    {  
        // We just forward the new action to a blank edit form
            Mage::log( "Mycompany_Mymodule_Adminhtml_Mycompany_Mymodule_RulesController newAction " );
           $this->_redirectReferer();

    }  

     public function saveAction()
    {
           Mage::log( "Mycompany_Mymodule_Adminhtml_Mycompany_Mymodule_RulesController saveAction " );


            $this->_redirect('*/*/');
    }   

     public function deleteAction()
    {
            Mage::log( "Mycompany_Mymodule_Adminhtml_Mycompany_Mymodule_RulesController deleteAction " );

            $this->_redirect('*/*/');
    }   

    public function massDeleteAction()
    {
           Mage::log( "Mycompany_Mymodule_Adminhtml_Mycompany_Mymodule_RulesController massDeleteAction" );

           $this->_redirectReferer();
    }   

}

Rules.php:

<?php

class Mycompany_Mymodule_Block_Adminhtml_Rules extends Mage_Adminhtml_Block_Widget_Grid_Container {

    public function __construct(){

        Mage::log( "Mycompany_Mymodule_Block_Adminhtml_Rules" );

        $this->_blockGroup = 'mycompany_mymodule'; # the first part of the grouped class name, i.e. (some_module)/whatever
        $this->_controller = 'adminhtml_rules';
        $this->_headerText = Mage::helper('mycompany_mymodule')->__('Manage Rules');
        $this->_addButtonLabel = Mage::helper('mycompany_mymodule')->__('Add New Rule');

        parent::__construct();

    }

}

Grid.php

<?php

class Mycompany_Mymodule_Block_Adminhtml_Rules_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

    public function __construct()
    {

        Mage::log( "Mycompany_Mymodule_Block_Adminhtml_Rules_Grid" );


        parent::__construct(); 

        $this->setId('rulesGrid'); 
        $this->setDefaultSort('rules_id'); 
        $this->setDefaultDir('asc'); 
        $this->setSaveParametersInSession(true); 
        $this->setUseAjax(true);

    }

    protected function _prepareCollection(){

        Mage::log( "Mycompany_Mymodule_Block_Adminhtml_Rules_Grid _prepareCollection" );

        $collection = Mage::getModel('mycompany_mymodule/rules')->getCollection();

        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {

        Mage::log( "Mycompany_Mymodule_Block_Adminhtml_Rules_Grid _prepareColumns" );



        $this->addColumn(
            'rules_id', 
            array(
                'type'     => 'number', 
                'header'   => Mage::helper('mycompany_mymodule')->__('ID'),
                'width'    => '30px',
                'index'    => 'rules_id', 
                'sortable' => true, 
                 )
        );

        $this->addColumn(
            'searchstring',
            array(
                'header' => Mage::helper('mycompany_mymodule')->__('Search String'),
                'width'  => '400px',
                'index'  => 'searchstring',
            )
        );

        $this->addColumn(
            'destination',
            array(
                'type'   => 'number',
                'header' => Mage::helper('mycompany_mymodule')->__('Destination'),
                'width'  => '50px',
                'index'  => 'destination', 
                 )
        );   

         $this->addColumn(
            'order',
            array(
                'type'   => 'number',
                'header' => Mage::helper('mycompany_mymodule')->__('Order'),
                'width'  => '50px',
                'index'  => 'order', 
           )       
        );

         $this->addColumn(
            'enabled',
            array(
                'type'   => 'number',
                'header' => Mage::helper('mycompany_mymodule')->__('Enabled'),
                'width'  => '50px',
                'index'  => 'enabled', 

            )       
        );        

        return parent::_prepareColumns();
    }


    public function getRowUrl($row)
    {    
        return $this->getUrl('*/*/edit', array('rules_id' => $row->getId()));
    }

    protected function _prepareMassaction()
    {

        Mage::log( "Mycompany_Mymodule_Block_Adminhtml_Rules_Grid _prepareMassaction" );


        $this->setMassactionIdField('mass_id');
        $this->getMassactionBlock()->setFormFieldName('mass');
        $this->getMassactionBlock()->addItem(
            'delete',
            array(
                'label'   => Mage::helper('mycompany_mymodule')->__('Delete'),
                'url'     => $this->getUrl('*/*/massDelete'),
                'confirm' => Mage::helper('mycompany_mymodule')->__('Are you sure?')
            )
        );
        return $this;
    }
}

--UPDATE--

mymodule.xml

<layout>
    <adminhtml_rules_index>
        <reference name="content">
            <block type="mycompany_mymodule/adminhtml_rules" name="mycompany_mymodule_rules" />
        </reference>
    </adminhtml_rules_index>
</layout>
0

3 Answers 3

2

You don't need to specify some layout XML here.

The problem here is that your controller code:

    $this->loadLayout();
    $this->_setActiveMenu('mycompany_mymodule');
    $this->_addContent($this->getLayout()->createBlock('mycompany_mymodule/adminhtml_rules_grid'));
    $this->renderLayout();

Will do the following:

  • load your layout XML file
  • add mycompany_mymodule/adminhtml_rules to the content using your layout XML
  • this block will automatically add the grid block
  • add mycompany_mymodule/adminhtml_rules_grid to the content using your controller code
  • render the layout

First you need to replace:

$this->_addContent($this->getLayout()->createBlock('mycompany_mymodule/adminhtml_rules_grid'));

With:

$this->_addContent($this->getLayout()->createBlock('mycompany_mymodule/adminhtml_rules'));

Then you can get rid of your layout XML file and that should fix your problem. Don't forget to get rid of the declaration in your config.xml as well

1
  • this solved to problem. thank you very much!
    – Sam
    Sep 1, 2016 at 10:02
0

change your Rules.php

<?php

  class Mycompany_Mymodule_Block_Adminhtml_Rules extends Mage_Adminhtml_Block_Widget_Grid_Container {

     public function __construct(){
       parent::__construct();
    Mage::log( "Mycompany_Mymodule_Block_Adminhtml_Rules" );

    $this->_blockGroup = 'mycompany_mymodule'; # the first part of the grouped class name, i.e. (some_module)/whatever
    $this->_controller = 'adminhtml_rules';
    $this->_headerText = Mage::helper('mycompany_mymodule')->__('Manage Rules');
    $this->_addButtonLabel = Mage::helper('mycompany_mymodule')->__('Add New Rule');



}

}

and put parent::__construct(); at the starting of your function and try again and let me know its working or not

0

Update indexAction() function code to follwing

public function indexAction()
        {

            Mage::log( "Mycompany_Mymodule_Adminhtml_Mycompany_Mymodule_RulesController indexAction" );

            $this->loadLayout();

            $this->_setActiveMenu('mycompany_mymodule');

            $this->renderLayout();
        }

Grid will be included with container block that you already added in mymodule.xml.

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.