3

I'm creating some menu admin and i have a container grid with buttons, but not a grid, logically this means that I don't load my block but I dont find the issue, i tried to get the class of a block and i can get it string(28) "Sd_Menu_Block_Adminhtml_Menu":

$block = $this->getLayout()->createBlock('sd_menu/adminhtml_menu');
var_dump(get_class($block)); //get class of block

enter image description here

Here is a full code, and if you see somethings wrong somewhere, the corrections are welcome:

app/code/local/Sd/Menu/Block/Adminhtml.Menu.php

<?php
class Sd_Menu_Block_Adminhtml_Menu extends Mage_Adminhtml_Block_Widget_Grid_Container{

  public function __construct()
  {
    $this->_blockGroup     = 'sd_menu';
    $this->_controller     = 'adminhtml_menu';
    $this->_headerText     = $this->__('Gérer le Menu');
    $this->_addButtonLabel = $this->__('Ajouter');
    parent::__construct();
  }

}

app/code/local/Sd/Menu/Block/Adminhtml/Menu/Grid.php

<?php

class Sd_Menu_Block_Adminhtml_Menu_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('menuGrid');
        $this->setDefaultSort('id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
    }

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

    protected function _prepareColumns()
    {
        $this->addColumn('entity_id', array(
          'header'    => $this->__('Id'),
          'align'     =>'right',
          'width'     => '10px',
          'index'     => 'id',
        ));

        $this->addColumn('name', array(
          'header'    => $this->__('Nom'),
          'align'     =>'left',
          'index'     => 'name',
          'width'     => '50px',
        ));


        $this->addColumn('address', array(
            'header'    => $this->__('Adresse'),
            'width'     => '150px',
            'index'     => 'content',
        ));
        return parent::_prepareColumns();
    }
}

app/code/local/Sd/Menu/controllers/Adminhtml/MenuController.php

<?php

class Sd_Menu_Adminhtml_MenuController extends Mage_Adminhtml_Controller_Action{

    public function indexAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('Menu 1')
            ->_title($this->__('Submenu1')); // for title
        $this->renderLayout();
    }

    public function listAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('Menu 1')
            ->_title($this->__('Submenu2')); //for title
        $this->renderLayout();
    }
}

app/code/local/Sd/Menu/Model/Menu.php

<?php
class Sd_Menu_Model_Menu extends Mage_Core_Model_Abstract{

    /**
     * Le constructeur
     */
    public function _construct() {
        parent::_construct();
        $this->_init('menu/menu');
    }
}

app/code/local/Sd/Menu/Model/Resource/Menu.php

<?php

class Sd_Menu_Model_Resource_Menu extends Mage_Core_Model_Resource_Db_Abstract
{
    /**
     * Initialize resource model
     *
     */
    protected function _construct()
    {
        $this->_init('menu/menu', 'entity_id');
    }
}

app/code/local/Sd/Menu/Model/Resource/Menu/Collection.php

<?php
class Sd_Menu_Model_Resource_Menu_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{

    protected function _construct()
    {
        parent::_construct();
        $this->_init('menu/menu');
    }
}

app/code/local/Sd/Menu/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Sd_Menu>
            <version>0.1.0</version>
        </Sd_Menu>
    </modules>
    <global>

        <helpers>
            <sd_menu>
                <class>Sd_Menu_Helper</class>
            </sd_menu>
        </helpers>

        <blocks>
            <sd_menu>
                <class>Sd_Menu_Block</class>
            </sd_menu>
        </blocks>

        <models>
            <menu>
                <class>Sd_Menu_Model</class>
                <resourceModel>menu_resource</resourceModel> //the corercted add
            </menu>
            <menu_resource>
                <class>Sd_Menu_Model_Resource</class>
                <entities>
                    <menu>
                        <table>wlc_sd_menu</table>
                    </menu>
                </entities>
            </menu_resource>
        </models>

        <resources>
            <menu_setup>
                <setup>
                    <module>Sd_Menu</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </menu_setup>
        </resources>

    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <sd_menu before="Mage_Adminhtml">Sd_Menu_Adminhtml</sd_menu>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <sd_menu>
                    <file>sd_menu.xml</file>
                </sd_menu>
            </updates>
        </layout>
    </adminhtml>
</config>

app/code/local/Sd/Menu/etc/adminhtml.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <menu>
        <menu1 module="sd_menu" translate="title">
            <title>Menu 1</title>
            <sort_order>100</sort_order>
            <children>
                <item1 module="sd_menu" translate="title">
                    <title>Submenu 1</title>
                    <sort_order>1</sort_order>
                    <action>adminhtml/menu/index</action>
                </item1>
                <item2 module="sd_menu" translate="title">
                    <title>Submenu 2</title>
                    <sort_order>2</sort_order>
                    <action>adminhtml/menu/list</action>
                </item2>
            </children>
        </menu1>
    </menu>
    <acl>
        <resources>
            <admin>
                <children>
                    <menu1 translate="title" module="sd_menu">
                        <title>My Controller</title>
                        <sort_order>-100</sort_order>
                        <children>
                            <item1 translate="title">
                                <title>Index Action</title>
                                <sort_order>1</sort_order>
                            </item1>
                            <item2 translate="title">
                                <title>List Action</title>
                                <sort_order>2</sort_order>
                            </item2>
                        </children>
                    </menu1>
                </children>
            </admin>
        </resources>
    </acl>
</config>

app/design/adminhtml/default/default/layout/sd_menu.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
    <adminhtml_menu_index>
        <reference name="content">
            <block type="sd_menu/adminhtml_menu" name="menu"/>
        </reference>
    </adminhtml_menu_index>
</layout>

app/code/local/Sd/Menu/Helper/Data.php

<?php
class Sd_Menu_Helper_Data extends Mage_Core_Helper_Abstract
{

}

app/etc/modules/Sd_Menu.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Sd_Menu>
            <active>true</active>
            <codePool>local</codePool>
        </Sd_Menu>
    </modules>
</config>

app/code/local/Sd/Menu/sql/sd_menu_setup/mysql4-install-0.1.0

<?php

$installer = $this;
$installer->startSetup();
Mage::log('START SCRIPT install sd_menu 0.1.0');

try {
    $this->run("
        CREATE TABLE IF NOT EXISTS `wlc_sd_menu` (
          `entity_id` int(11) NOT NULL AUTO_INCREMENT,
          `name` text NOT NULL,
          `photo` varchar(255) DEFAULT NULL,
          `position` int(11) DEFAULT NULL,
          `address` varchar(255) NOT NULL,
          `email` varchar(255) NOT NULL,
          `facebook` varchar(255) DEFAULT NULL,
          `twitter` varchar(255) DEFAULT NULL,
          `linkedin` varchar(255) DEFAULT NULL,
          `instagram` varchar(255) DEFAULT NULL,
          `google_plus` varchar(255) DEFAULT NULL,
          `status` int(11) NOT NULL DEFAULT '1',
            PRIMARY KEY (`entity_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    ");
    Mage::log("Script OK!");
} catch (Exception $e) { Mage::log('Script KO: '.$e); }


Mage::log('END SCRIPT install sd_menu 0.1.0');
$installer->endSetup();
2
  • app/code/local/Sd/Menu/Block/Adminhtml.Menu.php OR app/code/local/Sd/Menu/Block/Adminhtml/Menu.php ? Apr 28, 2017 at 10:16
  • 1
    it's app/code/local/Sd/Menu/Block/Adminhtml/Menu.php
    – PЯINCƎ
    Apr 28, 2017 at 10:48

3 Answers 3

2

Here are some things you are missing in your module:
in config.xml under the models->menu tag you need this <resourceModel>menu_resource</resourceModel>.

You need the resource model class /Sd/Menu/Model/Resource/Menu.php

<?php

class Sd_Menu_Model_Resource_Menu extends Mage_Core_Model_Resource_Db_Abstract
{
    /**
     * Initialize resource model
     *
     */
    protected function _construct()
    {
        $this->_init('menu/menu', 'entity_id');
    }
}

and the resource collection model Sd/Menu/Model/Resource/Menu/Collection.php

<?php
class Sd_Menu_Model_Resource_Menu_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{

    protected function _construct()
    {
        parent::_construct();
        $this->_init('menu/menu');
    }
}

Also there is no install script that should create your table Sd/Menu/sql/menu_setup/install-0.1.0.php

<?php
/* @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;

$installer->startSetup();
$table = $installer->getConnection()
    ->newTable($installer->getTable('menu/menu'))
    ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
        'identity'  => true,
        'nullable'  => false,
        'primary'   => true,
    ), 'Menu ID')
    ->addColumn('name', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
        'nullable'  => false,
    ), 'Menu name')
    ->addColumn('content', Varien_Db_Ddl_Table::TYPE_TEXT, '2M', array(
    ), 'Menu Content')
    ->addColumn('creation_time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
    ), 'Menu Creation Time')
    ->addColumn('update_time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
    ), 'Menu Modification Time')
    ->setComment('Menu Table');
$installer->getConnection()->createTable($table);

You may need to add more columns to the table. I just guessed what you have in it.

4
  • Thank you for your answer @Marius, I just added what you had written to me i cleared caches and i have the same problem, about the script install I already had it and I forgot to put it. Look at my edit please
    – PЯINCƎ
    Apr 28, 2017 at 8:47
  • I just recreated your module with the additions I mentioned and I got proper grid.
    – Marius
    Apr 28, 2017 at 8:48
  • It's incredible, I have exactly the same module, at logs level I have a warning ERR (3): Warning: simplexml_load_string(): Entity: line 32: parser error : Start tag expected, '&lt;' not found but I don't think that it comes from this module, even by disabling this module it appears, I just haven't had time to fixe it. I also note that when I click on submenu 2, I lose a container grid (the buttons)
    – PЯINCƎ
    Apr 28, 2017 at 9:01
  • I was mistaken about the positioning of the resource like the answer of John Thank you @Marius
    – PЯINCƎ
    Apr 28, 2017 at 11:56
2

To addition to Marius's answer, now there is only one mistake in your code. You have placed resourceModel tag in your config.xml in wrong location

<models>
    <menu>
        <class>Sd_Menu_Model</class>
        <resourceModel>menu_resource</resourceModel> <!-- ADD RESOURCE MODEL HERE -->
    </menu>
    <!-- <resourceModel>menu_resource</resourceModel> REMOVE THIS -->
    <menu_resource>
        <class>Sd_Menu_Model_Resource</class>
        <entities>
            <menu>
                <table>wlc_sd_menu</table>
            </menu>
        </entities>
    </menu_resource>
</models>
1
  • Thank you for your time, do you know why when i click on submenu2 i lose a container grid(buttons)
    – PЯINCƎ
    Apr 28, 2017 at 11:58
1

Below is the corrected code for the entire module (for menu1). I have tested and verified. (In front of each file I have mentioned whether it has been newly added or modified or not)

app/code/local/Sd/Menu/Block/Adminhtml/Menu/Grid.php (Modified)

<?php
class Sd_Menu_Block_Adminhtml_Menu_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('menuGrid');
        $this->setDefaultSort('entity_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }

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

    protected function _prepareColumns()
    {
        $this->addColumn('entity_id', array(
            'header'    => $this->__('Id'),
            'align'     =>'right',
            'width'     => '10px',
            'index'     => 'entity_id',
        ));

        $this->addColumn('name', array(
            'header'    => $this->__('Name'),
            'align'     =>'left',
            'index'     => 'name',
            'width'     => '50px',
        ));


        $this->addColumn('address', array(
            'header'    => $this->__('Address'),
            'width'     => '150px',
            'index'     => 'content',
        ));
        return parent::_prepareColumns();
    }
}

app/code/local/Sd/Menu/Block/Adminhtml/Menu.php (Not modified)

<?php
class Sd_Menu_Block_Adminhtml_Menu extends Mage_Adminhtml_Block_Widget_Grid_Container{

    public function __construct()
    {
        $this->_blockGroup     = 'sd_menu';
        $this->_controller     = 'adminhtml_menu';
        $this->_headerText     = $this->__('Gérer le Menu');
        $this->_addButtonLabel = $this->__('Ajouter');
        parent::__construct();
    }

}

app/code/local/Sd/Menu/controllers/Adminhtml/MenuController.php (Not modified)

<?php
class Sd_Menu_Adminhtml_MenuController extends Mage_Adminhtml_Controller_Action{

    public function indexAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('Menu 1')
            ->_title($this->__('Submenu1')); // for title
        $this->renderLayout();
    }

    public function listAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('Menu 1')
            ->_title($this->__('Submenu2')); //for title
        $this->renderLayout();
    }
}

app/code/local/Sd/Menu/etc/adminhtml.xml (Not modified)

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <menu>
        <menu1 module="sd_menu" translate="title">
            <title>Menu 1</title>
            <sort_order>100</sort_order>
            <children>
                <item1 module="sd_menu" translate="title">
                    <title>Submenu 1</title>
                    <sort_order>1</sort_order>
                    <action>adminhtml/menu/index</action>
                </item1>
                <item2 module="sd_menu" translate="title">
                    <title>Submenu 2</title>
                    <sort_order>2</sort_order>
                    <action>adminhtml/menu/list</action>
                </item2>
            </children>
        </menu1>
    </menu>
    <acl>
        <resources>
            <admin>
                <children>
                    <menu1 translate="title" module="sd_menu">
                        <title>My Controller</title>
                        <sort_order>-100</sort_order>
                        <children>
                            <item1 translate="title">
                                <title>Index Action</title>
                                <sort_order>1</sort_order>
                            </item1>
                            <item2 translate="title">
                                <title>List Action</title>
                                <sort_order>2</sort_order>
                            </item2>
                        </children>
                    </menu1>
                </children>
            </admin>
        </resources>
    </acl>
</config>

app/code/local/Sd/Menu/etc/config.xml (Modified)

<?xml version="1.0"?>
<config>
    <modules>
        <Sd_Menu>
            <version>0.1.0</version>
        </Sd_Menu>
    </modules>
    <global>

        <helpers>
            <sd_menu>
                <class>Sd_Menu_Helper</class>
            </sd_menu>
        </helpers>

        <blocks>
            <sd_menu>
                <class>Sd_Menu_Block</class>
            </sd_menu>
        </blocks>

        <models>
            <sd_menu>
                <class>Sd_Menu_Model</class>
                <resourceModel>sd_menu_resource</resourceModel>
            </sd_menu>
            <sd_menu_resource>
                <class>Sd_Menu_Model_Resource</class>
                <entities>
                    <menu>
                        <table>wlc_sd_menu</table>
                    </menu>
                </entities>
            </sd_menu_resource>
        </models>

        <resources>
            <sd_menu_setup>
                <setup>
                    <module>Sd_Menu</module>
                </setup>
            </sd_menu_setup>
        </resources>

    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <sd_menu before="Mage_Adminhtml">Sd_Menu_Adminhtml</sd_menu>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <sd_menu>
                    <file>sd_menu.xml</file>
                </sd_menu>
            </updates>
        </layout>
    </adminhtml>
</config>

app/code/local/Sd/Menu/Helper/Data.php (Not modified)

<?php
class Sd_Menu_Helper_Data extends Mage_Core_Helper_Abstract

{

}

app/code/local/Sd/Menu/Model/Resource/Menu/Collection.php (Newly Added)

<?php
class Sd_Menu_Model_Resource_Menu_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    public function _construct()
    {
        $this->_init('sd_menu/menu');
    }
}

app/code/local/Sd/Menu/Model/Resource/Menu.php (Newly Added)

<?php
class Sd_Menu_Model_Resource_Menu extends Mage_Core_Model_Resource_Db_Abstract
{
    protected function _construct()
    {
        $this->_init('sd_menu/menu', 'entity_id');
    }
}

app/code/local/Sd/Menu/Model/Menu.php (Modified)

<?php
class Sd_Menu_Model_Menu extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('sd_menu/menu');
    }
}

app/code/local/Sd/Menu/sql/sd_menu_setup/install-0.1.0.php (Newly Added)

<?php
$installer = $this;

$installer->startSetup();

$table = $installer->getConnection()
    ->newTable($installer->getTable('sd_menu/menu'))
    ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
    ), 'Id')
    ->addColumn('name', Varien_Db_Ddl_Table::TYPE_VARCHAR, null, array(
        'nullable'  => false,
    ), 'Name')
    ->addColumn('address', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
        'nullable'  => false,
    ), 'Address');
$installer->getConnection()->createTable($table);

$installer->endSetup();

app/etc/modules/Sd_Menu.xml (Not modified)

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Sd_Menu>
            <active>true</active>
            <codePool>local</codePool>
        </Sd_Menu>
    </modules>
</config>

app/design/adminhtml/default/default/layout/sd_menu.xml (Not modified)

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
    <adminhtml_menu_index>
        <reference name="content">
            <block type="sd_menu/adminhtml_menu" name="menu"/>
        </reference>
    </adminhtml_menu_index>
</layout>
2
  • Thank you for your time, do you know why when i click on submenu2 i lose a container grid(buttons)
    – PЯINCƎ
    Apr 28, 2017 at 11:57
  • You have to create separate container classes and relevant grid class as you did for the submenu1
    – Sukeshini
    May 2, 2017 at 5:33

Your Answer

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

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