How to add new menu in Magento Admin panel
While creating a new Magento theme I needed to move a few configuration options to the admin panel. For example I want to create an option to change a background color and fonts of a theme. This is very convenient, no need to know which HTML / CSS file have to be edited.
I’ve spent some time with Google to find a solution, have read dozens of articles. In this series of articles I will try to describe briefly the creation of new options in Magento Admin panel. In the first article we will look at the Magento architecture and will create a new menu item. This will be a dummy item, when you click on it nothing will happen.
Some theory.
Magento uses a programming template MVC. I will not dwell on this term, details can be read on Wikipedia page. The main idea of MVC — the separation of appearance, business logic and data into different parts, so that a change in one part, has minimal impact on other parts. You do not need to take MVC as a dogma, it is just a pattern that Magento development team have significantly revised. I met the name Configuration-Based MVC. At the heart of Magento a global XML-configuration file is located, that is used as the script for the Magento engine, it describes what modules are connected, what layout should be used, etc. You may say: «Keeping everything in one file is not very convenient.» So it is, this global XML is gathering from many local files. Thus, each module has a part of the configuration that is required to operate this module only. A very flexible mechanism, isn’t it?
Magento engine includes a very low level/basic functions, such as the formation of the global configuration file, modules connection, the call of controllers, pages formation. Shop functions are not included to Magenta engine. All functions of the store are made in the form of modules. They are in the app/code/core/Mage. To extend the functions we need to create a new module. Unlike modules developed by Magento, modules from other developers need to be located in the app/code/local.
That’s all with the theory, it’s time to have a practice. Lets start with the creating of module.
Creating a Module
In Google you can find a lot of articles on this subject. As I noticed, most of them — it is a smaller version of the article — http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table. It contains a detailed description of the module directory structure and files. At this stage, do not suggest going into details, just download the extension ModuleCreator. You can download the add-on with MagentoConnect, read comments how to upgrade it to the branch 1.7.x. More simple way is to download the updated version here and copy to the root directory of your Magento site. To launch the extension:
- — Open www.youmagentostore.com/moduleCreator
- — Enter your login/password of administrator
- — Fill in 2 fields in the form: Namespace, Module
- — Click on create button
- — ModuleCreator will perform all routine to create the initial file of the module.
As a result, you should see
List of files is pretty big. Lets understand what’s what.
What the module is built from
My goal in this article is to learn how to create a dummy menu item in the admin panel, so all module files will not be considered, we will consider only the important files
File — app/etc/modules/TonyTemplate_Blog.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <config> <modules> <TonyTemplate_Blog> <active>true</active> <codePool>local</codePool> </TonyTemplate_Blog> </modules> </config> |
This file reads by Magento engine and add file to the global config. Now Magento knows about our new module.
File — app/code/local/TonyTemplate/Blog/etc/config.xml
This is the main configuration file of our module (the file will also be added to the global XML configuration file). This file describes the structure of our menu section
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?xml version="1.0"?> <config> <modules> <TonyTemplate_Blog> <version>0.1.0</version> </TonyTemplate_Blog> </modules> <admin> <routers> <blog> <use>admin</use> <args> <module>TonyTemplate_Blog</module> <frontName>blog</frontName> </args> </blog> </routers> </admin> <adminhtml> <menu> <blog module="blog"> <title>Blog</title> <sort_order>71</sort_order> <children> <items module="blog"> <title>Manage Items</title> <sort_order>0</sort_order> <action>blog/adminhtml_blog</action> </items> </children> </blog> </menu> </adminhtml> </config> |
With the help of <sort_order> it is possible to sort the menu items.
To create sub-elements of the menu use <children>. New elements can have arbitrary names. For example to add a new menu item Comments, you can add
1 2 3 4 5 |
<comments module="blog"> <title>Comments</title> <sort_order>1</sort_order> <action>blog/adminhtml_comment</action> </comments> |
As a result, you should get
1 2 3 4 5 6 7 8 9 10 11 12 |
<children> <items module="blog"> <title>Manage Items</title> <sort_order>0</sort_order> <action>blog/adminhtml_blog</action> </items> <comments module="blog"> <title>Comments</title> <sort_order>1</sort_order> <action>blog/adminhtml_comment</action> </comments> </children> |
That’s all with the creation of the menu.
But if you decide to delete all other files, leaving only the two described, you’ll get an error
“Fatal error: Class ‘Mage_Blog_Helper_Data’ not found”.
This is due to the peculiarities of Magento engine, it needs a helper. We don’t need it yet, so lets create an empty helper.
File — app/code/local/TonyTemplate/Blog/Helper/Data.php
1 2 3 4 5 6 7 |
<?php class TonyTemplate_Blog_Helper_Data extends Mage_Core_Helper_Abstract { } |
So that Magento knows about helper in the module config file, the following should be left:
1 2 3 4 5 6 7 |
<global> <helpers> <blog> <class>TonyTemplate_Blog_Helper</class> </blog> </helpers> </global> |
The archive file of the resulting module can be downloaded here
That’s all. See you in the new posts.
If you like Magento, check our premium Magento themes here.
I actually relish, result in I found just the thing I’d been interested in. You’ve finished this 5 time extensive look! God Bless you person. Have a very wonderful working day. Bye
I was very pleased to discover this website. I wanted to thank you for your time for this fantastic read!! decefdbfaceefcgd
Rock on! After trying tutorials on this all day, this one finally worked for me. All I wanted was a simple little link, and with this, I got just that!
Very nice idea. Working Great.!!!
Hello ,
It helped me a ton . Much obliged to YOU.
I might want to take in more … have you proceeded with advance ?
Here are the 5 tips to speed up Magento admin panel. (https://www.cloudways.com/blog/how-to-speed-up-magento-admin-panel/)
Delete unused extensions
Delete unused and outdated products
Reindexing
Tune up the database
Clear the Cache
It will help you to speed up Magento admin panel, ake a back-up before you begin working on your Magento website. Nothing is more frustrating than a broken website.