1

I am trying to get a custom grid in an adminhtml module to display. I have tried many things, but cannot get it to work. Here are my files. The page loads, but it is a blank page.

app/design/adminhtml/hewa/newdesign/layout/customer.xml

<layout>
    <adminhtml_customer_messages_index>
        <reference name="content">
            <block type="hwadminhtml/customer_messages" name="messages.list">
            </block>
        </reference>
    </adminhtml_customer_messages_index>
</layout>

app/code/local/Hwareh/Adminhtml/etc/adminhtml.xml

<config>
    <menu>
        <customer translate="title" module="customer">
            <children>
                <messages translate="title" module="customer">
                    <title>Customer Messages</title>
                    <action>adminhtml/customer_messages/</action>
                    <sort_order>3</sort_order>
                </messages>
            </children>
        </customer>
    </menu>
</config>

app/code/local/Hwareh/Adminhtml/etc/config.xml

<customer_messages>Hwareh_Adminhtml_Block_Customer_Messages</customer_messages>
<customer_messages_grid>Hwareh_Adminhtml_Block_Customer_Messages_Grid</customer_messages_grid>

app/code/local/Hwareh/Adminhtml/controllers/Customer/MessagesController.php

class Hwareh_Adminhtml_Customer_MessagesController extends     Mage_Adminhtml_Controller_Action
{
protected $_publicActions = array('edit');

protected function _construct()
{
    $this->setUsedModuleName('Mage_Customer');
}

protected function _initAction()
{
    $this->loadLayout()
    ->_setActiveMenu('customer/messages')
    ->_addBreadcrumb($this->__('Customer'), $this->__('Customer'))
    ->_addBreadcrumb($this->__('Customer Messages'),$this->__('Customer Messages'));
    return $this;
}


public function indexAction()
{
    $this->_title($this->__('Customer'))
        ->_title($this->__('Customer Messages'));
    $this->_initAction()
        ->_addContent($this->getLayout()->createBlock('adminhtml/customer_messages')->toHtml)
        ->renderLayout();
}


public function gridAction()
{
    $this->loadLayout();
    $this->getResponse()->setBody(
        $this->getLayout()->createBlock('adminhtml/customer_messages_grid')->toHtml()
    );
}

//are the next two functions even needed?
public function exportInchooCsvAction()
{
    $fileName = 'customer_messages.csv';
    $grid = $this->getLayout()->createBlock('adminhtml/customer_messages_grid');
    $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
}

public function exportInchooExcelAction()
{
    $fileName = 'customer_messages.xml';
    $grid = $this->getLayout()->createBlock('adminhtml/customer_messages_grid');
    $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
    }
}
?>

app/code/local/Hwareh/Adminhtml/Block/Customer/Messages.php

class Hwareh_Adminhtml_Block_Customer_Messages extends     Mage_Adminhtml_Block_Widget_Container
{
public function __construct()
{
    parent::__construct();
    $this->_blockGroup = 'adminhtml';
    $this->_controller = 'adminhtml/customer_messages';
    $this->_headerText = Mage::helper('customer')->__('Customer Messages');

    parent::__construct();
    $this->_removeButton('add');
    //$this->setTemplate('customer/messages.phtml');
    }
}

app/code/local/Hwareh/Adminhtml/Block/Customer/Messages/Grid.php

class Hwareh_Adminhtml_Block_Customer_Messages_Grid extends     Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
    parent::__construct();
    $this->setId('customer_messages_grid');
    $this->setDefaultSort('entity_id');
    $this->setDefaultDir('desc');
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(true);
}

protected function _prepareCollection()
{
    $collection = Mage::getModel('hwcustomer/messages')->getCollection();

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    $helper = Mage::helper('customer');
    $currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);

    $this->addColumn('entity_id', array(
        'header' => $helper->__('Message Id#'),
        'index'  => 'entity_id'
    ));

    $this->addColumn('subject', array(
        'header' => $helper->__('Subject'),
        'type'   => 'text',
        'index'  => 'subject'
    ));

    $this->addColumn('reply', array(
        'header'       => $helper->__('Replies'),
        'index'        => 'reply',
    ));

    $this->addColumn('status', array(
        'header'       => $helper->__('Status'),
        'index'        => 'status',
    ));


    $this->addExportType('*/*/exportInchooCsv', $helper->__('CSV'));
    $this->addExportType('*/*/exportInchooExcel', $helper->__('Excel XML'));
    return parent::_prepareColumns();
}

public function getGridUrl()
{
    return $this->getUrl('*/*/grid', array('_current'=>true));
    }
}
  • Is that all your etc/config.xml? – Adarsh Khatri Jun 16 '16 at 23:20
1

I assume that you have already got complete etc/config.xml.

Important

I wouldn't name my module as Adminhtml, since it is used by magento core. I would name it something different.

Now,

This class needs to extend from Mage_Adminhtml_Block_Widget_Grid_Container and constructor should be like this:

class Hwareh_Adminhtml_Block_Customer_Messages extends Mage_Adminhtml_Block_Widget_Grid_Container //Mage_Adminhtml_Block_Widget_Container
{
public function __construct()
{
    parent::__construct();
    $this->_blockGroup = 'adminhtml';
    $this->_controller = 'customer_messages'; //changed check below
    $this->_headerText = Mage::helper('customer')->__('Customer Messages');

    parent::__construct();
    $this->_removeButton('add');
    //$this->setTemplate('customer/messages.phtml');
    }
}

Above settings are used in the parents _prepareLayout() method as below.

protected function _prepareLayout()
{
    $this->setChild( 
        'grid',
        $this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
        $this->_controller . '.grid')->setSaveParametersInSession(true) );
    return parent::_prepareLayout();
}

So this line

$this->getLayout()->createBlock(
        $this->_blockGroup.'/' . $this->_controller . '_grid',
        $this->_controller . '.grid')->setSaveParametersInSession(true) );

ends up to this

$this->getLayout()->createBlock('adminhtml/customer_messages_grid');

That means it is calling your grid class Hwareh_Adminhtml_Block_Customer_Messages_Grid

  • Cheers, you check my updated answer for more understanding of grid. – Adarsh Khatri Jun 16 '16 at 23:39
  • Yes, it is related to _prepareLayout() function, I would suggest to change module name adminhtml to something else. $this->getLayout()->createBlock('adminhtml/customer_messages_grid') is not getting called. How is your full config? – Adarsh Khatri Jun 17 '16 at 0:00
  • I have fixed it. In my config, the line <customer_messages>Hwareh_Adminhtml_Block_Customer_Messages</customer_messages> was <customer_messages>Hwareh_Adminhtml_Block_Customer_Messages</customer_messages>> – djames Jun 17 '16 at 0:39

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.