方法 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:

<?phpclass 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/

Magento 自定义后台menu Insert dynamical menu in Magento’s Admin相关推荐

  1. Magento: 自定义用户登录导向页面 Redirect Customer to Previous Page After Login

    Configuration Settings – Login to admin panel – Go to System -> Configuration -> CUSTOMERS -&g ...

  2. Magento 自定义分页代码 How to change pagination design in product listing page in magen

    I have worked in the template\catalog\product\list\toolbar.phtml where you can change the total tool ...

  3. java中menu用法_Android Menu用法全面讲解

    说明:本文只介绍Android3.0及以上的Menu知识点. 菜单的分类 菜单是Android应用中非常重要且常见的组成部分,主要可以分为三类:选项菜单.上下文菜单/上下文操作模式以及弹出菜单.它们的 ...

  4. Android中添加Options Menu,按MENU键无反应

    自已开发的一个日历项目,其中一个显示日程列表的Activity只有一个ListView,数据来源于数据库. 在这个xxxActivity.java里面添加了: /** Create menu. */ ...

  5. 联想小新Air15电脑重启后卡在“Boot Menu”或“App Menu”的界面无法启动

    联想小新Air15电脑重启后卡在"Boot Menu"或"App Menu"的界面无法启动 原因 可能是由于UMIS忆联固态硬盘兼容性导致的问题,在系统更新后兼 ...

  6. YUMI制作启动盘安装ubuntu时报错:booting ‘find /menu.lstk /boot/grub/menu.lst, /grub/menu.lst‘

    利用YUMI制作的Linux启动盘,在进入安装的时候显示:booting 'find /menu.lstk /boot/grub/menu.lst, /grub/menu.lst' 解决方法: 在U盘 ...

  7. php项目开发免费后台模板,自定义后台模板文件

    当需要修改后台模板时可以采用此方法来修改,不影响对程序的升级 一.自定义后台模板文件 例如需要修改系统默认模板:/dayrui/Core/Views/api_my.html 此文件位于Core目录下, ...

  8. android actionbar和menu的区别,menu和actionbar

    3.0之后推荐用actionbar,Menu已经过时但是我们还是了解一下吧, Menu学习 在java语句中添加menu组件 重写onCreateOptionsMenu(Menu menu)方法 @O ...

  9. python cms建站教程:Wagtail建站(二、修改主页与自定义后台管理)

    不得不说python的中文cms建站教程实在是太少了,直接用Django/Flask这样的框架从头开始写又实在是有点麻烦,自己摸索着写一点使用Wagtail建站的方法,仅供参考.Wagtail是一款基 ...

最新文章

  1. 解决:Error: Aesthetics must be either length 1 or the same as the data (5): fill
  2. ACL 2020三大奖项出炉!知名学者夫妇曾先后获终身成就奖,时间检验奖回溯95年经典著作...
  3. 14.线程安全?线程不安全?可重入函数?不可重入函数?
  4. 第一部分:数据中心专业名词你知道多少?
  5. iptables的配置实例
  6. 对特朗普获胜感到意外? 那你是被社交媒体迷惑了
  7. ecshop分页类assign_pager分析和扩展
  8. linux条件变量cond,Linux 条件变量 pthread_cond_signal及pthread_cond_wait
  9. 拓端tecdat|R语言法国足球联赛球员多重对应分析(MCA)
  10. 用Java实现文本编辑器:创建、浏览、编辑文件;剪贴、复制、粘贴;保存、另存为;字符统计;自动换行
  11. Windows如何查看.db数据库文件
  12. ps抠图怎么放大图片_PS抠图时选区图片放大后,怎么移动图片抠图选区?
  13. 【爬虫】使用Scrapy框架进行爬虫详解及示例
  14. php中eregi,php eregi
  15. 阿拉伯数字翻译成中文的大写数字
  16. java多线程提交,如何按照时间顺序获取线程结果,看完你就懂了 | Java工具类
  17. scholarscope不显示影响因子_如何根据IF快速筛选文章,ScholarScope来帮你
  18. 宜信漏洞管理平台--洞察搭建
  19. 开发通用资料——常用接口引脚定义
  20. Centos7运行Docker1.13.1报错Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor pres

热门文章

  1. 贪吃蛇程序不要白不要,一个赞就够了
  2. python爬取网上文章_python 爬取微信文章
  3. Classification Example
  4. 【炼丹技巧】指数移动平均(EMA)【在一定程度上提高最终模型在测试数据上的表现(例如accuracy、FID、泛化能力...)】
  5. cycleGAN的改进文章(CyCADA + U-GAT-IT)
  6. jquery.pin 修改浮动的top元素
  7. 抖音很火的失恋表白网页模板
  8. python中的ix是啥_python pandas (ix iloc loc) 的区别
  9. Lolipa魔方财务主题-虚拟主机源码
  10. Ripro子主题-ziyuan-zhankr资源主题 蓝色简约版