您的位置:首页 > 其它

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: