Magento 自定义后台menu Insert dynamical menu in Magento’s Admin
2014-08-19 11:17
218 查看
Magento 自定义后台menu Insert dynamical menu in Magento’s Admin
时间 2013-06-13 14:48:29 Just Code原文
http://justcoding.iteye.com/blog/1886183 方法 1. 外联
Hello everyone! I’ve recently got frustrated with Magento’s core functionality which requires XML definition for Administration menus. And I really wanted to add quick website / store links to it. Solution was to overwrite one of Magento’s Adminhtml blocks,
and inject my non-XML, dynamic menu to it. If you’re interested in how I did it, read on.
To make it upgradeable, and painless, I’ve decided to make an extension just for this. It consists of only 2 Files, config.xml, and one Block rewrite.
First, I’ve created config.xml, and set it like this:
<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Inchoo_ExtendedMenu> <version>0.1.0</version> </Inchoo_ExtendedMenu> </modules> <global> <blocks> <configurable> <class>Inchoo_Configurable_Block</class> </configurable> <adminhtml> <rewrite> <page_menu>Inchoo_ExtendedMenu_Block_Adminhtml_Menu</page_menu> </rewrite> </adminhtml> </blocks> </global> </config>
And since Magento parses XML as always, ideal solution for me was to hook on that parsed data just before printing the menu. So, block I mentioned earlier is “Mage_Adminhtml_Block_Page_Menu”. And here, I was interested in getMenuArray() method, and since
it looks like this:
/** * Retrieve Adminhtml Menu array * * @return array */ public function getMenuArray() { return $this->_buildMenuArray(); }
This was the bes place to interfere in Magento’s core. I’ve created my own class “Inchoo_ExtendedMenu_Block_Adminhtml_Menu” with source code:
<?php class Inchoo_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; } }
What I did her was overwrite of “getMenuArray()” call on parent method, and population of resulting array with my own dynamic menu.
Result looks like this on default Magento 1.5.0.1 installation:
And you can
download it here .
Note:
As always, please make backup before installation, as this is provided for usage at your own risk.
来源: http://inchoo.net/ecommerce/magento/insert-dynamical-menu-in-magentos-admin/
方法 2. 内联
We have already seen in a previous post how to
override a controller into Magento. This time I’ll show you how to create your own controller in admin panel in an
efficiant way , because there are many solutions here and there but they do not always work and are sometimes obsolete. We will access our controller under a new custom tab.
Overview
Before showing you the code, here is what the admin panel will look like:And here is the module structure:
Module configuration
app/code/community/JR/CreateAdminController/etc/config.xml:<?xml version="1.0"?> <config> <modules> <JR_CreateAdminController> <version>1.0.0</version> </JR_CreateAdminController> </modules> <global> <helpers> <jr_createadmincontroller> <!-- Helper definition needed by Magento --> <class>Mage_Core_Helper</class> </jr_createadmincontroller> </helpers> </global> <admin> <routers> <adminhtml> <args> <modules> <foo_bar before="Mage_Adminhtml">JR_CreateAdminController_Adminhtml</foo_bar> </modules> </args> </adminhtml> </routers> </admin> </config>
Tabs and sub-tabs creation
app/code/community/JR/CreateAdminController/etc/adminhtml.xml:<?xml version="1.0" encoding="UTF-8"?> <config> <menu> <mycustomtab module="jr_createadmincontroller" translate="title"> <title>My Custom Tab</title> <sort_order>100</sort_order> <children> <index module="jr_createadmincontroller" translate="title"> <title>Index Action</title> <sort_order>1</sort_order> <action>adminhtml/custom</action> </index> <list module="jr_createadmincontroller" translate="title"> <title>List Action</title> <sort_order>2</sort_order> <action>adminhtml/custom/list</action> </list> </children> </mycustomtab> </menu> <acl> <resources> <admin> <children> <custom translate="title" module="jr_createadmincontroller"> <title>My Controller</title> <sort_order>-100</sort_order> <children> <index translate="title"> <title>Index Action</title> <sort_order>1</sort_order> </index> <list translate="title"> <title>List Action</title> <sort_order>2</sort_order> </list> </children> </custom> </children> </admin> </resources> </acl> </config>
Controller creation
app/code/community/JR/CreateAdminController/controllers/Adminhtml/CustomController.php:<?php class JR_CreateAdminController_Adminhtml_CustomController extends Mage_Adminhtml_Controller_Action { public function indexAction() { $this->loadLayout() ->_setActiveMenu('mycustomtab') ->_title($this->__('Index Action')); // add template $this->_addContent($this->getLayout()->createBlock('adminhtml/template')->setTemplate('lookbook/list.phtml')); $this->renderLayout(); } public function listAction() { $this->loadLayout() ->_setActiveMenu('mycustomtab') ->_title($this->__('List Action')); // my stuff $this->renderLayout(); } }
Download
Code of the extension is available on GitHub: https://github.com/jreinke/magento-create-admin-controller-example/downloads . Your turn now!来源: http://www.bubblecode.net/en/2012/02/08/magento-create-your-own-admin-controller-in-a-new-tab/
相关文章推荐
- awblog 报错 magento 后台admin路径修改
- Magento 后台menu不能进入的解决办法
- Magento后台,自定义导出一段时间内销售最好的商品
- 自定义django的admin后台action
- magento移动端后台管理插件mobile Admin by snm portal
- Magento Admin Menu Not Working (js 404 error)
- django-admin管理后台高级自定义
- 如何在magento后台增加一个自定义订单状态
- magento 后台取产品自定义的select(下拉菜单)字段
- inspinia admin 最新版 inspinia 2.7.1 一套非常优秀的bootstrap后台管理模板
- 自定义django的admin后台action
- How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单
- How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单
- Magento 后台自定义导出订单
- django后台自带User,Group。下面是给admin添加自定义表,并为admin添加副文本编辑器
- magento自定义后台配置
- ylbtech-cnblogs(博客园)-数据库设计-2,Admin(用户后台)-用户自定义参数设置
- Magento入门基础 - 在magento后台增加一个自定义订单状态
- 生成后台管理菜单 admin_menu 类
- Django——报错:admin后台添加中文数据时报错——UnicodeEncodeError: 'ascii' codec can't encode characters in position..