1

Please help! I'm completely lost in creating Magento Admin Grid. It's been a week now.

I have created a module coming from this tutorial http://ecommerce.eglobeits.com/blog/magento-admin-module-development-with-grid-and-tab/. The problem was, the grid won't show up. I have successfully created database table via install script.

Question:

How do I make the grid work?

Can you spot the problem?

This are the codes.

app/code/local/Rts/Pmadmin/etc/config.xml

<?xml version="1.0" ?>

<config>
<modules>
    <Rts_Pmadmin>
        <version>0.1.0</version>
    </Rts_Pmadmin>
</modules>
<global>
    <models>
        <Rts_Pmadmin>
            <class>Rts_Pmadmin_Model</class>
            <resourceModel>pmadmin_resource</resourceModel>
        </Rts_Pmadmin>
        <pmadmin_resource>
            <class>Rts_Pmadmin_Model_Resource</class>
            <entities>
                <pricematrix>
                    <table>pricematrix</table>
                </pricematrix>
            </entities>
        </pmadmin_resource>
    </models>
    <resources>
        <pricematrix_setup>
            <setup>
                <module>Rts_Pmadmin</module>
                <class>Rts_Pmadmin_Model_Resource_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </pricematrix_setup>
        <pricematrix_write>
            <connection>
                <use>core_write</use>
            </connection>
        </pricematrix_write>
        <pricematrix_read>
            <connection>
                <use>core_read</use>
            </connection>
        </pricematrix_read>
    </resources>
    <helpers>
        <Rts_Pmadmin>
            <class>Rts_Pmadmin_Helper</class>
        </Rts_Pmadmin>
    </helpers>
</global>
<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <Rts_Pmadmin after="Mage_Adminhtml">Rts_Pmadmin</Rts_Pmadmin>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>
</config>

app/code/local/Rts/Pmadmin/etc/adminhtml.xml

<?xml version="1.0" ?>

<config>
<menu>
    <customer>
        <children>
            <pmadmin>
                <title>Price Matrix</title>
                <sort_order>9999</sort_order>
                <action>adminhtml/pmadmin/index</action>
            </pmadmin>
        </children>
    </customer>
</menu>
<acl>
    <resources>
        <admin>
            <children>
                <customer>
                    <children>
                        <padmin>
                            <title>Price Matrix</title>
                            <sort_order>9999</sort_order>
                        </padmin>
                    </children>
                </customer>
            </children>
        </admin>
    </resources>
</acl>
<layout>
    <updates>
        <pmadmin>
            <file>rts/pricematrix.xml</file>
        </pmadmin>
    </updates>
</layout>
</config>

app/design/adminhtml/default/default/layout/rts

<layout version="0.1.0">
<adminhtml_pmadmin_index>
    <reference name="content">
        <block type="pmadmin/pmadmincontainer" name="pmadmincontainer"></block>
    </reference>
</adminhtml_pmadmin_index>

app/code/local/Rts/Pmadmin/controllers/PmadminController.php

class Rts_Pmadmin_PmadminController extends Mage_Adminhtml_Controller_Action
{
public function indexAction() {
    $this->loadLayout();
    $this->renderLayout();
}

public function newAction(){
    $this->loadLayout();
    $this->_addContent($this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit'))
            ->_addLeft($this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit_tabs'));
    $this->renderLayout();
}

public function deleteAction() {
    if ($this->getRequest()->getParam('id') > 0) {
        try {
            $model = Mage::getModel('pmadmin/pricematrix');

            $model->setId($this->getRequest()->getParam('id'))
                    ->delete();

            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
            $this->_redirect('*/*/');
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
        }
    }
    $this->_redirect('*/*/');
}

public function saveAction() {

    if ($data = $this->getRequest()->getPost())
    {
        $model = Mage::getModel('pmadmin/pricematrix');
        $id = $this->getRequest()->getParam('id');
        foreach ($data as $key => $value)
        {
            if (is_array($value))
            {
                    $data[$key] = implode(',',$this->getRequest()->getParam($key));
            }
        }

        if ($id) {
            $model->load($id);
        }
        $model->setData($data);

        Mage::getSingleton('adminhtml/session')->setFormData($data);
        try {
            if ($id) {
                $model->setId($id);
            }

            $model->save();

            if (!$model->getId()) {
                Mage::throwException(Mage::helper('pricematrix')->__('Error saving news details'));
            }

            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('pricematrix')->__('Details was successfully saved.'));

            Mage::getSingleton('adminhtml/session')->setFormData(false);

            // The following line decides if it is a "save" or "save and continue"
            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array('id' => $model->getId()));
            } else {
                $this->_redirect('*/*/');
            }

        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            if ($model && $model->getId()) {
                $this->_redirect('*/*/edit', array('id' => $model->getId()));
            } else {
                $this->_redirect('*/*/');
            }
        }

      return;
    }
    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricematrix')->__('No data found to save'));
    $this->_redirect('*/*/');
}


public function editAction() {

    $id = $this->getRequest()->getParam('pricematrix_id', null);

    $model = Mage::getModel('pmadmin/pricematrix');
    if ($id) {
        $model->load((int) $id);
        if ($model->getId()) {
            $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
            if ($data) {
                $model->setData($data)->setId($id);
            }
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('pricematrix')->__('matrix does not exist'));
            $this->_redirect('*/*/');
        }
    }
    Mage::register('pricematrix_data', $model);

    $this->_title($this->__('pricematrix'))->_title($this->__('Edit Matrix'));
    $this->loadLayout();
    $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

    $this->_addContent($this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit'))
            ->_addLeft($this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit_tabs'));
    $this->renderLayout();
}    
} 

app/code/local/Rts/Pmadmin/Block/Adminhtml/Pmadmincontainer.php

class Rts_Pmadmin_Block_Adminhtml_Pmadmincontainer extends Mage_Adminhtml_Block_Widget_Grid_Container {

public function __construct() {
    $this->_controller = 'Pmadmin_Pmadmin';
    $this->_blockGroup = 'pmadmin';
    $this->_headerText = Mage::helper('pricematrix')->__('Price Matrix Manager');
    $this->_addButtonLabel = Mage::helper('pricematrix')->__('Add New Matrix');
    parent::__construct();
}
}

app/code/local/Rts/Pmadmin/Block/Adminhtml/Pricematrix/Grid.php

class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Grid extends Mage_Adminhtml_Block_Widget_Grid {

public function __construct() {
    parent::__construct();
    $this->setId('pricematrix_grid');
    $this->setDefaultSort('pricematrix_id');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
}

protected function _prepareCollection() {
    $collection = Mage::getModel('pmadmin/pricematrix')->getCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns() {
    $this->addColumn('pricematrix_id', array(
        'header' => Mage::helper('pricematrix')->__('ID'),
        'align' => 'right',
        'width' => '10px',
        'index' => 'pricematrix_id',
    ));

    $this->addColumn('title', array(
        'header' => Mage::helper('pricematrix')->__('Title'),
        'align' => 'left',
        'index' => 'title',
        'width' => '50px',
    ));


    $this->addColumn('short_description', array(
        'header' => Mage::helper('pricematrix')->__('Description'),
        'width' => '150px',
        'index' => 'short_description',
    ));

    $this->addColumn('file_path', array(
        'header' => Mage::helper('pricematrix')->__('File Path'),
        'width' => '150px',
        'index' => 'file_path',
    ));


    $this->addColumn('customer_group', array(
        'header' => Mage::helper('pricematrix')->__('Customer Group'),
        'width' => '150px',
        'index' => 'customer_group',
    ));


    $this->addColumn('creation_time', array(
        'header' => Mage::helper('pricematrix')->__('Posted On'),
        'width' => '150px',
        'index' => 'creation_time',
    ));

    return parent::_prepareColumns();
}


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

app/code/local/Rts/Pmadmin/Block/Adminhtml/Pricematrix/Edit.php

class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Edit extends Mage_Adminhtml_Block_Widget_Form_Container{
   public function __construct()
   {
    parent::__construct();
    //vwe assign the same blockGroup as the Grid Container
    $this->_blockGroup = 'pmadmin';
    //and the same controller
    $this->_controller = 'Pmadmin_Pmadmin';
    //define the label for the save and delete button
    $this->_updateButton('save', 'label','save reference');
    $this->_updateButton('delete', 'label', 'delete reference');
}
   /* Here, we're looking if we have transmitted a form object,
      to update the good text in the header of the page (edit or add) */
public function getHeaderText()
{
    if( Mage::registry('matrix_data')&&Mage::registry('matrix_data')->getId())
     {
          return 'Edit reference '.$this->htmlEscape(
          Mage::registry('matrix_data')->getTitle()).'<br />';
     }
     else
     {
         return 'Add a contact';
     }
}
}

app/code/local/Rts/Pmadmin/Block/Adminhtml/Pricematrix/Edit/Form.php

class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm() {

    if (Mage::registry('matrix_data')) {
        $data = Mage::registry('matrix_data')->getData();
    } else {
        $data = array();
    }

    $form = new Varien_Data_Form(array(
        'id' => 'edit_form',
        'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
        'method' => 'post',
        'enctype' => 'multipart/form-data'
            )
    );

    $form->setUseContainer(true);
    $this->setForm($form);
    $form->setValues($data);
    return parent::_prepareForm();
}
}

app/code/local/Rts/Pmadmin/Block/Adminhtml/Pricematrix/Edit/Tabs.php

class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs {

public function __construct() {
    parent::__construct();
    $this->setId('pricematrix_grid');
    $this->setDestElementId('edit_form'); // this should be same as the form id define above
    $this->setTitle(Mage::helper('pricematrix')->__('Matrix Information'));
}

protected function _beforeToHtml() {
    $this->addTab('form_section', array(
        'label' => Mage::helper('pricematrix')->__('Matrix Information'),
        'title' => Mage::helper('pricematrix')->__('Matrix Information'),
        'content' => $this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit_tab_form')->toHtml(),
    ));

    $this->addTab('form_section1', array(
        'label' => Mage::helper('pricematrix')->__('Content'),
        'title' => Mage::helper('pricematrix')->__('Content'),
        'content' => $this->getLayout()->createBlock('pmadmin/adminhtml_pricematrix_edit_tab_content')->toHtml(),
    ));

    return parent::_beforeToHtml();
}
}

app/code/local/Rts/Pmadmin/Block/Adminhtml/Pricematrix/Edit/Tab/Form.php

 class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {

protected function _prepareForm() {

    if (Mage::registry('matrix_data')) {
        $data = Mage::registry('matrix_data')->getData();
    } else {
        $data = array();
    }

    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset('news_news', array('legend' => Mage::helper('pricematrix')->__('news information')));

    $fieldset->addField('title', 'text', array(
        'label' => Mage::helper('pricematrix')->__('Matrix Title'),
        'class' => 'required-entry',
        'required' => true,
        'name' => 'title',
    ));

    $fieldset->addField('tag', 'text', array(
        'label' => Mage::helper('pricematrix')->__('Tag'),
        'class' => 'required-entry',
        'required' => true,
        'name' => 'tag',
    ));

    $form->setValues($data);

    return parent::_prepareForm();
}
}

app/code/local/Rts/Pmadmin/Block/Adminhtml/Pricematrix/Edit/Tab/Content.php

 class Rts_Pmadmin_Block_Adminhtml_Pricematrix_Edit_Tab_Content extends Mage_Adminhtml_Block_Widget_Form {

protected function _prepareForm() {

    if (Mage::registry('matrix_data')) {
        $data = Mage::registry('matrix_data')->getData();
    } else {
        $data = array();
    }

    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset('news_news', array('legend' => Mage::helper('news')->__('More information')));

    /*
     * Editing the form field in wysiwyg editor.
     */

    $wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config')->getConfig();
    $wysiwygConfig->addData(array('add_variables' => false,
        'add_widgets' => true,
        'add_images' => true,
        'directives_url' => Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive'),
        'directives_url_quoted' => preg_quote(Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive')),
        'widget_window_url' => Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/widget/index'),
        'files_browser_window_url' => Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg_images/index'),
        'files_browser_window_width' => (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_width'),
        'files_browser_window_height' => (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_height')
    ));


    $fieldset->addField('description', 'editor', array(
        'name' => 'description',
        'label' => Mage::helper('news')->__('Description'),
        'title' => Mage::helper('news')->__('Description'),
        'style' => 'width:800px; height:500px;',
        'config' => $wysiwygConfig,
        'required' => false,
        'wysiwyg' => true
    ));

    $form->setValues($data);
}
}

I don't know were the problem is.

Please help!

Thanks

4
  • did you check error log for error var/log? Commented Nov 16, 2015 at 6:47
  • There are no error displayed. The page display the header, update messages, and footer but the grid is missing.
    – rodge
    Commented Nov 16, 2015 at 7:08
  • did you enable error log from admin panel? Commented Nov 16, 2015 at 7:09
  • Yes, I also enable it.
    – rodge
    Commented Nov 16, 2015 at 7:32

1 Answer 1

1

the problem with you admin router change it to pmadmin as you calling it.

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <Rts_Pmadmin after="Mage_Adminhtml">pmadmin</Rts_Pmadmin>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>
8
  • Thanks @Qaisar Satti! I tried to change it to pmadmin however it gives me 404 error.
    – rodge
    Commented Nov 16, 2015 at 7:04
  • second problem you did't define the block in global tag <blocks><pmadmin> <class>Rts_Pmadmin_Block</class> </pmadmin></blocks> Commented Nov 16, 2015 at 7:08
  • Thanks! I already updated the code but no grid
    – rodge
    Commented Nov 16, 2015 at 7:19
  • cache is disabled or not? and go to index.php on main folder and uncomment ini_set('display_errors', 1); it will show you error. Commented Nov 16, 2015 at 7:20
  • cached are disabled.
    – rodge
    Commented Nov 16, 2015 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.