Magento 2 Basics Part 5 - Creating an Admin Page
10 minsAdding elements to the admin area is always a requirement when working with custom modules as you need to let your user content-manage and configure their functionality.
- Part 1 - Setting up your module
- Part 2 - Creating a frontend controller
- Part 3 - Creating a helper
- Part 4 - Creating an observer
- Part 5 - Creating an admin page
- Part 6 - Using plugins
Adding sections to the admin area is in some ways very different to Magento 1. In this tutorial I’m not going to cover admin grids, instead I’m going to focus on creating a grid-less admin page and adding to the configuration.
Configuration
The Magento 2 configuration is now found in Stores > Settings > Configuration, however a familiar file is needed to add to this:
Step 1 - Create Magefox/Example/etc/adminhtml/system.xml
In the example below I have created a new tab called “Magento Fox” with a section called “Settings” underneath. There is then a option group called “Settings” with a single Yes/No field called “Show Something?”.
There are obviously tonnes more options you can choose from here - my suggestion is to look at the core modules system.xml to find the code you need for each field type. The options are basically the same as Magento 1 system.xml.
Step 2 - Create Magefox/Example/etc/config.xml
Config.xml is back, except this time is for setting default configuration values only. At least it’s a lot more readable this time round!
Admin Pages
Creating an actual page in the admin requires a bit more work because (like the frontend) we have to create a controller using the admin route object and we have to build blocks to make the content. Grids & forms aside, this is how you do it:
Step 1 - Create Magefox/Example/etc/adminhtml/routes.xml
First we create our admin route as I explained above (more XML!):
Step 2 - Create Magefox/Example/etc/adminhtml/menu.xml
Now you have to decide how you want people to be able to access your page by creating a conveniently named “menu.xml” file within etc/adminhtml.
In the example above I have added a new section to the “Stores” tab which is referenced in the first <add> node where it says parent=”Magento_Backend::stores”. Underneath this I have then added a link to my new page which will be located at magefox/foxypage, and I’ve also linked to our new configuration page we created above.
Step 3 - Create Magefox/Example/etc/acl.xml
We need to ensure that our admin page can be restricted to certain users only. This can be done by making it available to the ACL object.
Step 4 - Create Magefox/Example/Controller/adminhtml/Foxypage/Index.php
We create our admin controller action in pretty much the same way as the frontend action, an execute method renders the page. An additional method here will also check that the user has the sufficient priveledges that we assigned above using ACL.
Step 5 - Create Magefox/Example/view/adminhtml/layout/magefox_foxypage_index.xml
Just as we did with the front controller layout XML we do the same with the admin. All I’m doing here is adding a custom block to the content block:
Step 6 Create Magefox\Example\Block\Adminhtml/Hello.php
Normal block class creation. I won’t add any methods as I think ypu’ve got the hang of it by now!
Step 7 Create Magefox/Example/view/adminhtml/templates/hello.phtml
Create your template file, add some content and you will see it magically appear within the content of your admin page.
I’m pretty sure you’re XML-ed out by now so I’ll leave this tutorial here, thanks for reading!