最近Magento的一个核心功能(后台管理员菜单需要XML定义)让我觉得沮丧。我想要加一些快速的网站/店铺链接到后台。解决方法是重写Magento的一个后台块,然后连接我的动态菜单(没有XML)。如果你对此感兴趣,那么读下去。 为了让它可升级,我决定做成插件。它值包含2个文件,config.xml和一个重写块。 首先,创建config.xml,我的是这样的: <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Alwayly_ExtendedMenu> <version>0.1.0</version> </Alwayly_ExtendedMenu> </modules> <global> <blocks> <configurable> <class>Alwayly_Configurable_Block</class> </configurable> <adminhtml> <rewrite> <page_menu>Alwayly_ExtendedMenu_Block_Adminhtml_Menu</page_menu> </rewrite> </adminhtml> </blocks> </global> </config> 由于Magento一如既往地解析XML,对我来说理想的解决方法就是在输出菜单前与解析数据挂钩。所以,我之前提到的块是“Mage_Adminhtml_Block_Page_Menu”。我对getMenuArray方法比较感兴趣,它是这样的: /** * Retrieve Adminhtml Menu array * * @return array */ public function getMenuArray() { return $this->_buildMenuArray(); } 这是最合适的地方来修改Magento核心,我用下面的代码创建了“Alwayly_ExtendedMenu_Block_Adminhtml_Menu”: <?php class Alwayly_ExtendedMenu_Block_Adminhtml_Menu extends Mage_Adminhtml_Block_Page_Menu { public function getMenuArray() { //Load standard menu $parentArr = parent::getMenuArray(); //Prepare "View Sites" menu $parentArr['view_sites'] = array( 'label' => 'View Sites', 'active'=>false , 'sort_order'=>0, 'click' => 'return false;', 'url'=>'#', 'level'=>0, 'last'=> true, 'children' => array() ); $app = Mage::app(); $j = 0; $allWebsites = $app->getWebsites(); $totalWebsiteCount = count($allWebsites) - 1; foreach ($allWebsites as $_eachWebsiteId => $websiteVal){ $_storeName = $app->getWebsite($_eachWebsiteId)->getName(); $_websiteUrl = array( 'label' => $_storeName, 'active' => false , 'url' => '#', 'click' => "return false", 'sort_order' => $j++ * 10, 'level' => 1, 'children' => array() ); if(count($parentArr['view_sites']['children']) == $totalWebsiteCount){ $_websiteUrl['last'] = true; } else { $_websiteUrl['last'] = false; } $parentArr['view_sites']['children'][$j - 1] = $_websiteUrl; $allStores = $app->getWebsite($app->getWebsite($_eachWebsiteId)->getId())->getStores(); $totalCount = count($allStores); $i = 0; foreach ($allStores as $_eachStoreId => $val){ $_websiteId = $app->getStore($_eachStoreId)->getWebsiteId(); if($_websiteId == $j){ $_storeName = $app->getStore($_eachStoreId)->getName(); $baseUrl = $app->getStore($_eachStoreId)->getUrl(); $_websiteUrl = array( 'label' => $_storeName, 'active' => false , 'click' => "window.open(this.href, 'Website - ' + this.href); return false;", 'sort_order' => $i++ * 10, 'level' => 2, 'url' => $baseUrl ); if(count($parentArr['view_sites']['children'][$j - 1]['children']) + 1 == $totalCount or $totalCount == 0) $_websiteUrl['last'] = true; else $_websiteUrl['last'] = false; $parentArr['view_sites']['children'][$j - 1]['children'][$i] = $_websiteUrl; } } } return $parentArr; } } 我这里重写了父类方法“getMenuArray()”,我的动态菜单也在结果数组中。 (责任编辑:好模板) |