2014-02-27 50 views
0

我想在magento admin中創建一個自定義模塊。我已經達到了一個新的鏈接添加到菜單,並點擊它,我可以導航到該模塊的控制器的索引行動。但是在這裏我看不到網格,只有標題文本和塊結構中添加的按鈕出現。網格不顯示在自定義的magento模塊

我的命名空間爲:Mmnamespace
我的模塊是:Mmmodule

我也使用以下命令

CREATE TABLE mmmodule_event (
    `event_id` INTEGER AUTO_INCREMENT PRIMARY KEY, 
    `name` VARCHAR(255), 
    `start` DATETIME, 
    `end` DATETIME, 
    `created_at` DATETIME, 
    `modified_at` DATETIME 
); 

目前有表兩個項目創建了一個名爲mmmodule_event表我填充它與使用magento的管理員

我可以看到,sinc e該塊擴展了Mage_Adminhtml_Block_Widget_Grid_Container類,它將自己將該模塊內的網格塊添加爲其子節點。

我的配置設置的XML文件如下:

我的config.xml

<?xml version="1.0"?> 
<!DOCTYPE config> 
<!-- 
/** 
* app/code/local/Mmnamespace/Mmmodule/etc/config.xml 
* 
* 
* 
* 
* @author Omatsola Isaac Sobotie <[email protected]> 
* @category Mmnamspace 
* @package Mmmodule 
* 
*/ 
--> 
<config> 
    <modules> 
     <Mmnamespace_Mmmodule> 
      <version>0.0.0</version> 
     </Mmnamespace_Mmmodule> 
    </modules> 
    <global> 
     <!-- this code tells magento to use resources --> 
     <models> 
      <mmmodule> 
       <class>Mmnamespace_Mmmodule_Model</class> 
       <resourceModel>mmmodule_resource</resourceModel> 
      </mmmodule> 
      <mmmodule_resource> 
       <class>Mmnamespace_Mmmodule_Model_Resource</class> 
       <entities> 
        <event> 
         <table>mmmodule_event</table> 
        </event> 
        <event_registrant> 
         <table>mmmodule_event_registrant</table> 
        </event_registrant> 
       </entities> 
      </mmmodule_resource> 
     </models> 
     <blocks> 
      <mmmodule> 
       <class>Mmnamespace_Mmmodule_Block</class> 
      </mmmodule> 
     </blocks> 
     <helpers> 
      <mmmodule> 
       <class>Mmnamespace_Mmmodule_Helper</class> 
      </mmmodule> 
     </helpers> 
     <!-- initializing a predispatch observer gets fired anytime a controller is about to render an action --> 
     <events> 
      <controller_action_predispatch> 
       <observers> 
        <mmmodule_observer> 
         <class>mmmodule/observer</class> 
         <method>controllerActionPredispatch</method> 
        </mmmodule_observer> 
       </observers> 
      </controller_action_predispatch> 
     </events> 
    </global> 

    <!-- routing front page menu to appropriate controller --> 
    <admin> 
     <routers> 
      <adminhtml> 
       <args> 
        <modules> 
         <mmmodule before="Mage_Adminhtml">Mmnamespace_Mmmodule_Adminhtml</mmmodule> 
        </modules> 
       </args> 
      </adminhtml> 
     </routers> 
    </admin> 

    <!--used to route urls with module name to module--> 
    <frontend> 
     <routers> 
      <mmmodule> 
       <use>standard</use> 
       <args> 
        <frontName>mmmodule</frontName> 
        <module>Mmnamespace_Mmmodule</module> 
       </args> 
      </mmmodule> 
     </routers> 

     <layout> 
      <updates> 
       <mmmodule> 
        <file>mmmodule.xml</file> 
       </mmmodule> 
      </updates> 
     </layout> 
    </frontend> 
</config> 

我adminhtml.xml

<?xml version="1.0"?> 
<!DOCTYPE config> 
<!-- 
/** 
* app/code/local/Mmnamespace/Mmmodule/etc/adminhtml.xml 
* 
* 

* @category Mmnamespace 
* @package Mmmodule 
*/ 
--> 
     <!-- this code will process urls with that front name --> 
<config> 
    <menu> 
     <mmmodule translate="title" module="mmmodule"> 
      <title>Events</title> 
      <sort_order>1000</sort_order> 
      <action>adminhtml/event</action> 
     </mmmodule> 
    </menu> 
</config> 

我的system.xml

<?xml version="1.0"?> 
<!DOCTYPE config> 
<!-- 
/** 
* app/code/local/Mmnamespace/Mmmodule/etc/system.xml 
* 
* 
* @category Mmnamespace 
* @package Mmmodule 
*/ 
--> 
     <!-- this code adds a fieldset to an existing general section --> 
<config> 
    <sections> 
     <general translate="label"> 
      <groups> 
       <mmmodule translate="label"> 
        <label>Mmmodule Options</label> 
        <sort_order>0</sort_order> 
        <show_in_default>1</show_in_default> 
        <show_in_website>1</show_in_website> 
        <show_in_store>1</show_in_store> 
        <fields> 
         <some_field translate="label"> 
          <label>Mmmodule Field</label> 
          <frontend_type>text</frontend_type> <!-- More frontend types can be found in the lib/Varien/Data/Form/Element folder --> 
          <show_in_default>1</show_in_default> 
          <show_in_website>1</show_in_website> 
          <show_in_store>1</show_in_store> 
         </some_field> 
        </fields> 
       </mmmodule> 
      </groups> 
     </general> 
    </sections> 
</config> 

我的Mmnamespace_Mmmodule.xml

<?xml version="1.0"?> 
<!DOCTYPE config> 
<!-- 
/** 
* app/etc/modules/Mmnamespace_Mmmodule.xml 
* 
* 
* 
* 
* @author Omatsola Isaac Sobotie <[email protected]> 
* @category Mmnamespace 
* @package Mmodule 
* 
*/ 
--> 
<config> 
    <modules> 
     <Mmnamespace_Mmmodule> 
      <active>true</active> 
      <codePool>local</codePool> 
      <depends /> 
     </Mmnamespace_Mmmodule>> 
    </modules> 
</config> 

我EventController.php

<?php 
/** 
* 
* Package: magentocore 
* Filename: MmmoduleController.php 
* Author: solidstunna101 
* Date: 25/02/14 
* Time: 18:40 
* * app/code/local/Mmnamespace/controllers/Adminhtml/EventController.php 
*/ 

class Mmnamespace_Mmmodule_Adminhtml_EventController extends Mage_Adminhtml_Controller_Action 
{ 
    //this function adds block content to main layout 
    public function indexAction() 
    { 
     $this->loadLayout(); 
     $this->_setActiveMenu('mmmodule/events'); 

     /*$this->_addContent(
      $this->getLayout()->createBlock('mmmodule/adminhtml_event_edit') 
);*/ 

     $this->_addContent(
      $this->getLayout()->createBlock('mmmodule/adminhtml_event') 
); 

     return $this->renderLayout(); 
    } 

    public function saveAction() 
    { 
     //gathering form field parameters from the url 
     $eventId = $this->getRequest()->getParam('event_id'); 
     $eventModel = Mage::getModel('mmmodule/event')->load($eventId); 

     //save event to database 
     if ($data = $this->getRequest()->getPost()) { 
      try { 
       $eventModel->addData($data)->save(); 
       Mage::getSingleton('adminhtml/session')->addSuccess(
        $this->__("Your event has been saved!") 
       ); 
      } catch (Exception $e) { 
       Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); 
      } 
     } 

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

我event.php塊

<?php 
/** 
* 
* Package: magentocore 
* Filename: Event.php 
* Author: solidstunna101 
* Date: 27/02/14 
* Time: 08:14 
* Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event.php 
*/ 

class Mmnamespace_Mmmodule_Block_Adminhtml_Event extends Mage_Adminhtml_Block_Widget_Grid_Container { 


    public function __construct(){ 

     $this->_blockGroup = 'mmmodule'; 
     $this->_controller = 'adminhtml_event'; 
     $this->_headerText = Mage::helper('mmmodule')->__('Events'); 
     $this->_addButtonLabel = Mage::helper('mmmodule')->__('Add New Event'); 
     parent::__construct(); 
    } 
} 

我Grid.php

<?php 
/** 
* 
* Package: magentocore 
* Filename: Grid.php 
* Author: solidstunna101 
* Date: 27/02/14 
* Time: 08:27 
* Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event/Grid.php 
*/ 

class Mmnamespace_Mmmodule_Block_Adminhtml_Event_Grid extends Mage_Adminhtml_Block_Widget_Grid { 

    protected function _prepareColumns() 
    { 
     $this->addColumn('name', array(
      'type' => 'text', 
      'index' => 'name', 
      'header' => $this->__('Name') 
     )); 

     $this->addColumn('start', array(
      'type' => 'date', 
      'index' => 'start', 
      'header' => $this->__('Start Date') 
     )); 

     $this->addColumn('end', array(
      'type' => 'date', 
      'index' => 'end', 
      'header' => $this->__('End Date') 
     )); 

     return $this; 
    } 


    public function _prepareCollection() 
    { 
     $collection = Mage::getModel('mmmodule/event')->getCollection(); 
     $this->setCollection($collection); 

     return parent::_prepareCollection(); 

    } 




} 

我的資源event.php

<?php 
/** 
* 
* Package: magentocore 
* Filename: Event.php 
* Author: solidstunna101 
* Date: 26/02/14 
* Time: 08:30 
* Location: app/code/local/Mmnamespace/Mmmodule/Model/Event.php 
*/ 

class Mmnamespace_Mmmodule_Model_Event extends Mage_Core_Model_Abstract 
{ 
    //this function initializes resources to be used 
    public function _construct() 
    { 
     $this->_init('mmmodule/event'); 
    } 
} 

我的資源事件abstract.php

<?php 
/** 
* 
* Package: magentocore 
* Filename: Event.php 
* Author: solidstunna101 
* Date: 26/02/14 
* Time: 08:33 
*Location: app/code/local/Mmnamespace/Mmmodule/Model/Resource/Event.php 
*/ 

class Mmnamespace_Mmmodule_Model_Resource_Event extends Mage_Core_Model_Resource_Db_Abstract 
{ 
    //initializes primary key in the table 
    public function _construct() 
    { 
     $this->_init('mmmodule/event', 'event_id'); 
    } 
} 

我的塊edit.php

<?php 
/** 
* 
* Package: magentocore 
* Filename: Edit.php 
* Author: solidstunna101 
* Date: 26/02/14 
* Time: 14:31 
* Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event/Edit.php 
*/ 

class Mmnamespace_Mmmodule_Block_Adminhtml_Event_Edit extends Mage_Adminhtml_Block_Widget_Form_Container 
{ 
    public function __construct() 
    { 
     //creating components required to render the form 
     $this->_objectId = 'event_id'; 
     $this->_blockGroup = 'mmmodule'; 
     $this->_controller = 'adminhtml_event'; 

     parent::__construct(); 
    } 

    /** 
    * Get edit form container header text 
    * 
    * @return string 
    */ 
    public function getHeaderText() 
    { 
     return Mage::helper('mmmodule')->__('New Event'); 
    } 

    public function getSaveUrl() 
    { 
     return $this->getUrl('*/event/save'); 
    } 
} 

form.php的

<?php 
/** 
* 
* Package: magentocore 
* Filename: Form.php 
* Author: solidstunna101 
* Date: 26/02/14 
* Time: 14:34 
* Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event/Edit/Form.php 
*/ 

class Mmnamespace_Mmmodule_Block_Adminhtml_Event_Edit_Form extends Mage_Adminhtml_Block_Widget_Form 
{ 

    //this function will override the prepare form function add data forms/fieldset 
    public function _prepareForm() 
    { 
     $form = new Varien_Data_Form(
      array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post') 
     ); 

     $fieldset = $form->addFieldset('base_fieldset', array('legend' => Mage::helper('mmmodule')->__('General Information'), 'class' => 'fieldset-wide')); 

     $fieldset->addField('name', 'text', array(
      'name'  => 'name', 
      'label'  => Mage::helper('mmmodule')->__('Event Name'), 
      'title'  => Mage::helper('mmmodule')->__('Event Name'), 
      'required' => true 
     )); 

     $dateFormatIso = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT); 
     $fieldset->addField('start', 'date', array(
      'name'  => 'start', 
      'format' => $dateFormatIso, 
      'image'  => $this->getSkinUrl('images/grid-cal.gif'), 
      'label'  => Mage::helper('mmmodule')->__('Start Date'), 
      'title'  => Mage::helper('mmmodule')->__('Start Date'), 
      'required' => true 
     )); 

     $fieldset->addField('end', 'date', array(
      'name'  => 'end', 
      'format' => $dateFormatIso, 
      'image'  => $this->getSkinUrl('images/grid-cal.gif'), 
      'label'  => Mage::helper('mmmodule')->__('End Date'), 
      'title'  => Mage::helper('mmmodule')->__('End Date'), 
      'required' => true 
     )); 

     $form->setUseContainer(true); 
     $this->setForm($form); 
    } 
} 

PS我的編輯形式顯示,但我確實格不顯示,有什麼我在這裏失蹤

+0

OK,它會檢查 –

+0

請刪除<!DOCTYPE配置>文件 –

+0

我剛剛那樣做,但這並沒有改變任何東西 – user729229

回答

0

請按照以下鏈接創建前端和管理模塊。 這將工作清晰,沒有在管理網格中發生任何問題。

Custom Module Creation Link

0

嘗試調用parent :: _在Grid.php _prepareColumns prepareColumns。沒有調用父級,您的網格可能不會觸發呈現。

p.s.在我的管理模塊中,我實際上返回parent :: _ prepareColumns而不是$ this。從所有XML

0

Mmnamespace_Mmmodule_Adminhtml_EventController變化return $this->renderLayout();$this->renderLayout();

protected function indexAction() 
{ 
    $this->loadLayout() 
     ->_setActiveMenu('mmmodule/items') 
     ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager')); 

    $this->_addContent($this->getLayout()->createBlock('mmmodule/adminhtml_event')); 
    $this->renderLayout(); //<-- not return $this->renderLayout(); 
} 

看看@Custom_module_with_custom_database_table因爲你的代碼似乎有其他的小問題

相關問題