configuration manager magento 2 with faded code snippet on top - dull comes to mind but it gets the job done
<!--?php namespace Magenticians\Launcher\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper { protected $_iteratorFactory; protected $_blockMenu; protected $_url; // moved from LauncherItems block public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Backend\Model\Menu\Filter\IteratorFactory $iteratorFactory, \Magento\Backend\Block\Menu $blockMenu \Magento\Backend\Model\UrlInterface $url, ) { parent::__construct($context); $this->_iteratorFactory = $iteratorFactory;<br ?--> $this->_blockMenu = $blockMenu; $this->_url = $url; } // moved from LauncherItems block public function getMenuModel() { /* ... */ } // moved from LauncherItems block protected function getMenuIterator($menu) { /*...*/ } // moved from LauncherItems block public function getMenuArray($menu, $itemsSeparator = ' ', & $result = array(), $fullName = '') { /*...*/ } }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:
public function __construct( \Magento\Backend\Block\Template\Context $context, \Magenticians\Launcher\Helper\Data $dataHelper, \Magento\Backend\Model\UrlInterface $url, array $data = array() ) { parent::__construct($context, $data); $this->_dataHelper = $dataHelper; $this->_url = $url; }Maybe needless to say, but for all methods which are moved, its calls should now read $this->_dataHelper->methodName().
public function getConfigSectionsArray($itemsSeparator = ' ', $itemPrefix = '') {}Because we need the Structure model, we will inject it in the helper its constructor:
public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Backend\Model\Menu\Filter\IteratorFactory $iteratorFactory, \Magento\Backend\Block\Menu $blockMenu, \Magento\Backend\Model\UrlInterface $url, \Magento\Backend\Model\Config\Structure $configStructure // << here we inject it ) { parent::__construct($context); $this->_iteratorFactory = $iteratorFactory; $this->_blockMenu = $blockMenu; $this->_url = $url; $this->_configStructure = $configStructure; }The tabs itself don’t have any value as launcher entries so we will directly iterate over their children:
public function getConfigSectionsArray($itemsSeparator = ' ', $itemPrefix = '') { $sections = array(); foreach ($this->_configStructure->getTabs() as $tab) { /** @var $tab \Magento\Backend\Model\Config\Structure\Element\Tab */ foreach ($tab->getChildren() as $section) { /** @var $section \Magento\Backend\Model\Config\Structure\Element\Section */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:
$sectionLabel = $itemPrefix . $tab->getLabel() . $itemsSeparator . $section->getLabel(); $sectionUrl = $this->_url->getUrl('adminhtml/system_config/edit', array('section' => $section->getId())); // First add global section to the launcher items... $sections[] = ['label' => $sectionLabel, 'value' => $sectionUrl];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:
/* [...] */ // First add global section to the launcher items... $sections[] = ['label' => $sectionLabel, 'value' => $sectionUrl]; foreach ($section->getChildren() as $subSection) { /** @var $subSection \Magento\Backend\Model\Config\Structure\Element\Section */ // ...then add all sub sections $sections[] = [ 'label' => $sectionLabel . $itemsSeparator . $subSection->getLabel(), 'value' => $sectionUrl . '#' . $section->getId() . '_' . $subSection->getId() . '-link' ]; } } } return $sections; }
const ITEMS_SEPARATOR = ' - '; const CONFIG_ITEMS_PREFIX = 'Configuration - '; public function getItemsJson() { $menuArray = $this->_dataHelper->getMenuArray($this->_dataHelper->getMenuModel(), self::ITEMS_SEPARATOR); $configSectionsArray = $this->_dataHelper->getConfigSectionsArray(self::ITEMS_SEPARATOR, self::CONFIG_ITEMS_PREFIX); return json_encode(array_merge($menuArray, $configSectionsArray), JSON_UNESCAPED_SLASHES); }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.
Your greatest possible competitive advantage can be your clients and the interactions they have with… Read More
Digital marketing KPIs are measurable values that marketing teams use to track progress toward desired… Read More
In today's digital age, fraud poses a significant threat to businesses of all sizes. As… Read More
Financial crimes continue to evolve and proliferate in our increasingly digital, global economy. From complex… Read More
In the highly competitive modern workplace, trust, and employee loyalty are crucial factors for long-term… Read More
In the ever-evolving world of small business developing and implementing effective marketing strategies is critical to… Read More