Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Add Actions in ACL in Magento 2

    In this blog, we will learn to Add Actions in ACL in Magento 2.
    In Magento 2, when we create a module that contains some functionality in the admin section. We create a menu to access the pages.
    And when the admin creates a new role, he can assign the menu access to that role using Access Control List concept.
    Here we are going to learn how we can add some additional actions in ACL which are not on the menu list.
    Let’s follow the following steps:

    1. Create menu.xml file inside the app/code/Vendor/CustomModule/etc/adminhtml directory.

    <?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="Vendor_Module::manager"
                title="Blogs Manager"
                module="Vendor_Module"
                sortOrder="10"
                resource="Vendor_Module::manager" />
                
            <add 
                id="Vendor_Module::index" 
                title="Blogs" 
                module="Vendor_Module" 
                sortOrder="12" 
                parent="Vendor_Module::manager" 
                action="route/controller/index" 
                resource="Vendor_Module::index" />
            
            <add 
                id="Vendor_Module::configuration" 
                title="Blogs Configuration" 
                module="Vendor_Module" 
                parent="Vendor_Module::manager" 
                sortOrder="16" 
                action="adminhtml/system_config/edit/section/route/" 
                resource="Vendor_Module::configuration"/>
    
        </menu>
    </config>
    

    2. Create acl.xml file inside Vendor/Module/etc/ directory.

    <?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="Vendor_Module::manager" title="Blogs Menu" sortOrder="90">
                        <resource id="Vendor_Module::index" title="Blogs" translate="title" sortOrder="50">
                            <resource id="Vendor_Module::actions" title="Actions" translate="title" sortOrder="10">
                                <resource id="Vendor_Module::masscancel" title="MassCancel" translate="title" sortOrder="130" />
                            </resource>
                        </resource>
                        <resource id="Vendor_Module::configuration" 
                            title="Blogs Configuration" sortOrder="52" />
                    </resource>
                    <resource id="Magento_Backend::stores">
                        <resource id="Magento_Backend::stores_settings">
                            <resource id="Vendor_Module::config_blogs" 
                                title="Blogs Configuration" sortOrder="50"/>
                        </resource>
                    </resource>
                </resource>
    		</resources>
    	</acl>
    </config>
    

    3. Create action controller MassAction.php file inside app/code/Vendor/Module/Controller/Adminhtml/Index/ directory.

    <?php
    namespace Vendor\Module\Controller\Adminhtml\Index;
    
    class MassCancel extends \Magento\Backend\App\Action
    {
        /**
         * __construct
         *
         * @param \Magento\Backend\App\Action\Context $context
         */
        public function __construct(
            \Magento\Backend\App\Action\Context $context
        ) {
            parent::__construct($context);
        }
    
        /**
         * MassCancel Action
         *
         * @return \Magento\Framework\App\ResponseInterface
         */
        public function execute()
        {
            try {
                //write your code here
            } catch (\Exception $e) {
                $this->messageManager->addError($e->getMessage());
            }
            $this->_redirect("*/*/index");
        }
        
        /**
         * Check if user has permissions to access this controller
         *
         * @return bool
         */
        protected function _isAllowed()
        {
            return $this->_authorization->isAllowed("Vendor_Module::masscancel");
        }
    }
    

    4. After login into the admin section. Result will be as following images:

    download-2-1
    Module’s Menu
    download-3
    Menu and Action in ACL when creating new Role

    Hope this will be helpful. Thanks 🙂

    Next Blog: Make Submenu position right adjacent of parent menu at frontend in Magento 2

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top