Home > database >  Add custom menu item to sales menu tab in magento 2 admin sidebar with example page
Add custom menu item to sales menu tab in magento 2 admin sidebar with example page

Time:09-18

I study Magento 2, within 2 weeks, and such a task appeared.

I need to add a custom tab to the sales sidebar, maybe someone has already encountered such a task?

CodePudding user response:

To add a menu item to the Magento 2 admin with a default landing page, add these files and adjust to your own needs. This will create a Custom Menu item under sales menu tab with an example page.

app/code/Company/Module/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Company_Module',
    __DIR__
);

app/code/Company/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_Module"/>
</config>

app/code/Company/Module/etc/acl.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Backend::admin">
                <resource id="Magento_Sales::sales">
                    <resource id="Magento_Sales::sales_operation">
                        <resource id="Company_Module::custommenu" title="Custom Menu" sortOrder="10"/>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

app/code/Company/Module/etc/adminhtml/menu.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
    <menu>
        <add id="Company_Module::custommenu"
             title="Custom Menu"
             module="Company_Module"
             sortOrder="100"
             parent="Magento_Sales::sales_operation"
             action="custommenu/index/index"
             resource="Company_Module::custommenu"
        />
    </menu>
</config>

app/code/Company/Module/etc/adminhtml/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="custommenu" frontName="custommenu">
            <module name="Company_Module"/>
        </route>
    </router>
</config>

app/code/Company/Module/Controller/Adminhtml/Index/Index.php

<?php

namespace Company\Module\Controller\Adminhtml\Index;

class Index extends \Magento\Backend\App\Action
{
    protected $resultPageFactory = false;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('Company_Module::custommenu');
        $resultPage->getConfig()->getTitle()->prepend(__('Custom Menu'));
        return $resultPage;
    }

    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Company_Module::custommenu');
    }
}

app/code/Company/Module/view/adminhtml/layout/custommenu_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Backend\Block\Template" template="Company_Module::content.phtml"/>
        </referenceContainer>
    </body>
</page>

app/code/Company/Module/view/adminhtml/templates/content.phtml

<p>Content goes here</p>

After adding the files, run following command from the command line: php bin/magento setup:upgrade

  • Related