Create admin menu items
In this lesson, we will learn how to create a custom menu item in the Magento admin panel. We will create a new heading named “Minerva” with a link named “FAQs” under the “Content” menu.
Setting Up Menu XML File
First, we need to create a new folder called adminhtml
under the etc
directory. Inside this folder, we will create a file named menu.xml
. The menu.xml
files are responsible for building out all of the menu functionality in the Magento admin.
We will start off with a menu root XML node of config
with the no-namespace schema location set to the Magento backend’s menu.xsd
file. The main node under config
is menu
, and it can have any number of add
, remove
, or update
nodes. Since we want to create our own menu item, we will use add
.
Adding Menu Items
There are many attributes on the add
node, so to make things easier to read, it’s recommended to put attributes on their own lines.
The id
for these add
nodes follows a similar format to Access Control Lists (ACLs). First comes the vendor name, followed by an underscore, and then the module name with a colon. In our case, the id
will be Macademy_Minerva::Minerva
.
The title
of the menu item will also be “Minerva”, and it will be set as a heading. To make it translatable, set the translate
attribute equal to title
. We also need to define the module responsible for this menu item, which will be Macademy_Minerva
.
The last required attribute on menu items is the resource
, which will be the ACL resource that we just set up. In this case, it will be the same name as our menu item, “Minerva”.
Nesting Menu Items
We want our “Minerva” heading to be displayed under the “Content” menu. To achieve this, add
nodes can have a parent
attribute. For our case, set the parent
attribute to Magento_Backend::content
.
To make sure our “Minerva” menu item comes after all the other items in the “Content” block, set the sort_order
attribute equal to 900
.
Creating a Sub-menu Item
A heading alone won’t be useful, so let’s create another add
node for the “FAQs” sub-menu item. We will assign the id
as Macademy_Minerva::faq
, and the title
will be “FAQs”. Set the translate
attribute equal to title
, and the module
will be Macademy_Minerva
.
The resource
attribute will be equal to the resource that we set up for “FAQs”. For the parent
attribute, set it equal to the “Minerva” heading’s id
. This will ensure that the “FAQs” menu item appears underneath the “Minerva” menu item.
To create a link for the “FAQs” menu item, the add
attribute accepts an optional action
attribute. Set the action
value equal to the URL that we want to send the admin user to relative to the admin URL. In our case, set it to minerva/faq
.
Now, after saving the changes and refreshing the Magento admin, you will see the new “Minerva” heading with a link to “FAQs” under the “Content” menu.
Setting Up the Admin Route
Clicking on the “FAQs” link will currently give us an error page. Our next step is to set up the admin route for this menu item.