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
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();
app/code/local/Sd/Menu/Block/Adminhtml.Menu.php
ORapp/code/local/Sd/Menu/Block/Adminhtml/Menu.php
?app/code/local/Sd/Menu/Block/Adminhtml/Menu.php