Enjoy Secure & Super Fast Managed Magento Hosting with FREE $25 Credit. Use Promo Code: MGT25
AVAIL NOW

Browsing configuration sections with the Magento 2 Backend Launcher

Comparing with the original PulseStormLauncher, the Magento 2 Backend Launcher is still missing the ability to quickly jump through configuration sections. Today, we will check that off the to-do list.

Introduction

Previously, we <a” href=”/magento-2-backend-launcher-composer-compatibility-code-update” target=”_blank”>updated the Launcher to get it to work with the latest alpha build at that time. We also added Composer compatibility. After a composer update on our local Magento 2 installation, the latest Magento 2 build doesn’t seem to introduce any code breaking changes.

That means we do not have to make any compatibility changes and can work straight on adding this new feature: being able to quickly jump to configuration sections.

The configuration structure

To get this functionality to work, we have to discover how we can retrieve the configuration sections their labels and URLs from Magento so we can feed them to the launcher its interface.

Without existing documentation on the topic, the easiest way to find out how to access the configuration structure, is to look at examples of how Magento 2 does it itself. We start at the highest level we want to be able to jump to: the configuration tabs. To find out how to retrieve them, we open the relevant block: Magento\Backend\Block\System\Config\Tabs.

In the block its constructor Magento\Backend\Model\Config\Structure is injected which supplies ( $configStructure->getTabs()) an iterator for the tabs. The template file making use of this iterator shows that the iterator iterates over Magento\Backend\Model\Config\Structure\Element\Tab objects. Each tab element might have child elements which are instances of Section. These section elements host the display label and id to build the URL.

The Section elements might have children themselves too. Those are the sections which are displayed on the right of the configuration manager. We want them available in the launcher, too.

Magento 2 configuration structure

The configuration manager and the elements we are interested in.

With this information, the boilerplate psuedocode reads like this:

  1. Get an instance of the Structure model by making use of dependency injection
  2. Get the tabs iterator and iterate over the tabs
  3. For each tab, get its sections and add them to the launcher
  4. For each section, get its child sections and add them to the launcher

Dependency injecting a helper

Currently, the LauncherItems block has all kinds of logic which doesn’t really belong in a block class. We will therefore clean the block class up a bit by moving all not necessarily related methods to an independent helper class.

Just like Magento 1.X, Magento 2 helper classes find their way in the Helper directory at the root of the module directory. The Data file is created there and it extends Magento\Framework\App\Helper\AbstractHelper.

Different from Magento 1.X is that in Magento 2 the existence of helper classes are no longer defined in the module its XML configuration. Instead, helpers are either dependency injected or retrieved by making use of the object manager. We will dependency inject it in the LauncherItems its constructor:

Maybe needless to say, but for all methods which are moved, its calls should now read $this->_dataHelper->methodName().

Getting configuration sections

With the psuedocode above in mind, writing a method which can retrieve the configuration sections is not that hard. We start with creating a new method in the just created helper class. The parameters speak for themselves:

Because we need the Structure model, we will inject it in the helper its constructor:

The tabs itself don’t have any value as launcher entries so we will directly iterate over their children:

To build the labels of the sections, we will combine the prefix, the name of the parent tab and the section its label. The URL can be retrieved from the Url model:

The only difference for the sub-sections is the way how URLs are generated. Instead of using the Url model again, the hash location is appended to the URL of the parent section. Upon seeing the hash location in the URL, the Magento 2 backend JavaScript will automatically open the affected accordion. The label simply gets appended to the parent label:

Using the result

In the LauncherItems block the specifically named getMenuJson method is renamed to a broader getItemsJson and a call to the getConfigSectionsArray method is added:

The launcher.phtml now has an updated call to getItemsJson. No client side code changes are required; even though the list of launcher items got a little bit bigger, the JavaScript will still work.

Final words

Because the configuration structure has a sane, dedicated model, not a lot of code is required to retrieve the configuration structure. Due to the missing documentation on the internals, most of the time is spent on actually finding that model and how to interact with it.

Now the Launcher module can jump through configuration sections, it’s time to add one ourselves. Booting the Launcher by clicking the “+” sign in the admin tray is a tedious task and a configurable, dedicated keyboard shortcut can solve this. Until next time.