I'm trying to learn Magento following instructions in a book, and I'm making a backend page for the admin. I have a etc/adminhtml/menu.xml page like this:
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd"> <menu> <add id="Foggyline_Helpdesk::ticket_manage" title="Helpdesk Tickets" module="Foggyline_Helpdesk" parent="Magento_Customer::customer" action="foggyline_helpdesk/ticket/index" resource="Foggyline_Helpdesk::ticket_manage"/> </menu> </config>
The page appears on the Customer menu all right, but when I click on it I get the error "Uncaught Error: Call to a member function setActive() on bool" and "Magento\Backend\Model\View\Result\Page->setActiveMenu('Foggyline_Helpd...') #1 C:\xampp\htdocs\magento_test\app\code\Foggyline\HelpDesk\Controller\Adminhtml\Ticket\Index.php(16)". From what I gather this happens when you ask for a menu item that doesn't exists.
Foggyline\HelpDesk\Controller\Adminhtml\Ticket\Index.php looks like this:
namespace Foggyline\Helpdesk\Controller\Adminhtml\Ticket; class Index extends \Foggyline\Helpdesk\Controller\Adminhtml\Ticket { public function execute() { if ($this->getRequest()->getQuery('ajax')) { $resultForward = $this->resultForwardFactory->create(); $resultForward->forward('grid'); return $resultForward; } $resultPage = $this->resultPageFactory->create(); $resultPage->setActiveMenu('Foggyline_Helpdesk::ticket_manage'); $resultPage->getConfig()->getTitle()->prepend(__('Tickets')); $resultPage->addBreadcrumb(__('Tickets'), __('Tickets')); $resultPage->addBreadcrumb(__('Manage Tickets'), __('Manage Tickets')); return $resultPage; } }
Do I have to declare the menu item "Foggyline_Helpdesk::ticket_manage" somewhere else besides menu.xml? I can't understand what I'm doing wrong. I've cleaned and flushed the cache a million times, I'm in developer mode, I've deleted the contents of generated and var/cache. If I comment out the line where I set the active menu I get a blank page. Also, in the Foggyline\Helpdesk\Controller\Adminhtml\Ticket.php file, I have a function
protected function _initAction() { $this->_view->loadLayout(); $this->_setActiveMenu( 'Foggyline_Helpdesk::ticket_manage' )->_addBreadcrumb( __('Helpdesk'), __('Tickets') ); return $this; }
and there doesn't seem to be an error with the menu item there.
Solved! Go to Solution.
After downloading the book code from git and comparing it line-to-line with my code, I found out what the problem was. It was in the view/adminhtml/layout/foggyline_helpdesk_ticket_index.xml file that showed the page.
I had
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="formkey"/>
<update handle="foggyline_helpdesk_ticket_grid_block"/>
<body>
<referenceContainer name="content">
<block class="Foggyline\Helpdesk\Block\Adminhtml\Ticket"
name="foggyline.helpdesk.ticket.index"
template="admin.block.helpdesk.ticket.grid.container"/>
</referenceContainer>
</body>
</page>
I had to remove the layout="1column" from the page tag, and replace the block tag with
<block name="admin.block.helpdesk.ticket.grid.container" class="Foggyline\Helpdesk\Block\Adminhtml\Ticket"> </block>
Now it works.
Hi @myrtopyrli9688 ,
In your app\code\Foggyline\HelpDesk\Controller\Adminhtml\Ticket\Index.php controller try to replace below line of code
$resultPage = $this->resultPageFactory->create();
with
----
Problem solved? Click Accept as Solution!
Please check below code reference
Source Code reference
https://github.com/skar2019/AdminInterface
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="Magento4u_AdminInterface::admininterface_parent" title="Magento4u AdminInterface" translate="title" module="Magento4u_AdminInterface" sortOrder="20" resource="Magento4u_AdminInterface::admininterface_parent"/>
<add id="Magento4u_AdminInterface::admininterface_child" title="AdminInterface Display" action="admininterface/index/index" translate="title" module="Magento4u_AdminInterface" sortOrder="10" parent="Magento4u_AdminInterface::admininterface_parent" resource="Magento4u_AdminInterface::admininterface_child" />
</menu>
</config>
<?php
namespace Magento4u\AdminInterface\Controller\Adminhtml\Index;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\View\Result\PageFactory;
/**
* Class Index
* @package Magento4u\AdminInterface\Controller\Adminhtml\Index
*/
class Index extends Action
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento4u_AdminInterface::admininterface_child';
/**
* Index constructor.
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
/**
* Execute action based on request and return result
* @return ResponseInterface|ResultInterface|void
*/
public function execute()
{
$this->_view->loadLayout();
$this->_setActiveMenu('Magento4u_AdminInterface::admininterface_child');
$this->_view->getPage()->getConfig()->getTitle()->set(__('Admin Interface Display'));
$this->_view->renderLayout();
}
}
@Nishu Jindal wrote:In your app\code\Foggyline\HelpDesk\Controller\Adminhtml\Ticket\Index.php controller try to replace below line of code
$resultPage = $this->resultPageFactory->create();
with
$resultPage = $this->_initAction();
Tried that, and it didn't work either. Now I get the same error on _setActiveMenu('Foggyline_Helpd...') in the _initAction function. It's as if the 'Foggyline_Helpdesk::ticket_manage' menu item doesn't exist. Yet I've copy-pasted it from menu.xml. Do I need to declare it somewhere else, besides menu.xml?
I've also done what Suman Kar suggested and replaced my execute() function with his. Still the same error.
After downloading the book code from git and comparing it line-to-line with my code, I found out what the problem was. It was in the view/adminhtml/layout/foggyline_helpdesk_ticket_index.xml file that showed the page.
I had
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="formkey"/>
<update handle="foggyline_helpdesk_ticket_grid_block"/>
<body>
<referenceContainer name="content">
<block class="Foggyline\Helpdesk\Block\Adminhtml\Ticket"
name="foggyline.helpdesk.ticket.index"
template="admin.block.helpdesk.ticket.grid.container"/>
</referenceContainer>
</body>
</page>
I had to remove the layout="1column" from the page tag, and replace the block tag with
<block name="admin.block.helpdesk.ticket.grid.container" class="Foggyline\Helpdesk\Block\Adminhtml\Ticket"> </block>
Now it works.