Jan
Jan

Reputation: 1

Typo3 Function Menu in Backend Module not working

I am developing a Backend Module for Typo3. The Extension Builder put this Code in the layout

<be:moduleLayout>
    <f:be.pageRenderer />

    <be:moduleLayout.menu identifier="ModuleMenu">
        <be:moduleLayout.menuItem label="Overview of Products"
            uri="{f:uri.action(controller: 'BackendProduct', action: 'index')}" />
        <be:moduleLayout.menuItem label="Overview of Categories"
            uri="{f:uri.action(controller: 'BackendCategory', action: 'index')}" />
        <be:moduleLayout.menuItem label="Overview of Orders"
            uri="{f:uri.action(controller: 'BackendOrder', action: 'index')}" />
    </be:moduleLayout.menu>

    <f:render section="Buttons" />
    <be:moduleLayout.button.shortcutButton displayName="Shortcut" />

    <f:render section="Content" />
</be:moduleLayout>

The Select is rendered but if i select one of the options nothing happens. Do i need to configure something else?

Upvotes: 0

Views: 842

Answers (1)

Sebastian Krings
Sebastian Krings

Reputation: 1

If you are using TYPO3 V11 LTS I would recommend using ModuleTemplate:

use TYPO3\CMS\Backend\Template\Components\Menu\Menu;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;

class MyBackendController extends ActionController
{
    /**
     * @var ModuleTemplate
     */
    protected ModuleTemplate $moduleTemplate;

    public function initializeAction()
    {
        $moduleTemplateFactory = GeneralUtility::makeInstance(ModuleTemplateFactory::class);
        $this->moduleTemplate = $moduleTemplateFactory->create($this->request);
        $this->generateMenu();
    }

    protected function generateMenu()
    {
        $menuItems = [
            'view' => [
            'controller' => 'MyBackendController',
                'action' => 'view',
                'label' => 'View items'
                ],
            'edit' => [
                'controller' => 'MyBackendController',
                'action' => 'edit',
                'label' => 'Edit items'
            ]
        ];
        $menu = new Menu();
        foreach ($menuItems as $menuItem) {
            $menuItem = $menu->makeMenuItem()
                ->setTitle($menuItem['label'])
                ->setHref($this->getHref($menuItem['controller'], $menuItem['action']))
            $menu->addMenuItem($menuItem);
        }
        $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
    }

    /**
     * viewAction
     * @param string $myParam
     */
    public function myAction(string $myParam): ResponseInterface
    {
        $this->view->assign('myParam', $myParam);
        $this->moduleTemplate->setContent($this->view->render());
        return $this->htmlResponse($this->moduleTemplate->renderContent());
    }
}

In your initializeAction() you set the moduleTemplate. By calling generateMenu() you can assign menu items according to the defined action-controller combinations set in $menuItems.

You could even simplyfy things by initializing the module template instance variable as well as the menu in an AbstractBackendController. This of course only makes sense if you use multiple ActionControllers.

Upvotes: 0

Related Questions