‪TYPO3CMS  11.5
MenuItemViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
26 use TYPO3Fluid\Fluid\View\Exception;
27 
45 class ‪MenuItemViewHelper extends AbstractViewHelper
46 {
47  use CompileWithRenderStatic;
48 
54  public function ‪initializeArguments()
55  {
56  parent::initializeArguments();
57  $this->registerArgument('label', 'string', 'Label of the menu item', true);
58  $this->registerArgument('uri', 'string', 'Action uri', true);
59  }
60 
68  public static function ‪renderStatic(
69  array $arguments,
70  \Closure $renderChildrenClosure,
71  RenderingContextInterface $renderingContext
72  ) {
73  trigger_error(__CLASS__ . ' will be removed in TYPO3 v12.', E_USER_DEPRECATED);
74  $request = $renderingContext->getRequest();
75  $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
76  ‪self::ensureProperNesting($viewHelperVariableContainer);
77 
79  $menu = $viewHelperVariableContainer->get(ModuleLayoutViewHelper::class, Menu::class);
80  $menuItem = $menu->makeMenuItem();
81  $menuItem->setTitle($arguments['label']);
82  $menuItem->setHref($arguments['uri']);
83  $menuItem->setActive($request->getAttribute('normalizedParams')->getRequestUri() === $arguments['uri']);
84  $menu->addMenuItem($menuItem);
85  }
86 
91  private static function ‪ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer): void
92  {
93  if (!$viewHelperVariableContainer->exists(ModuleLayoutViewHelper::class, Menu::class)) {
94  throw new Exception(sprintf('%s must be nested in <f.be.moduleLayout.menu> ViewHelper', self::class), 1531235592);
95  }
96  }
97 }
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper
Definition: MenuItemViewHelper.php:46
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\initializeArguments
‪initializeArguments()
Definition: MenuItemViewHelper.php:53
‪TYPO3\CMS\Backend\Template\Components\Menu\Menu
Definition: Menu.php:24
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: MenuItemViewHelper.php:67
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayoutViewHelper
Definition: ModuleLayoutViewHelper.php:52
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\ensureProperNesting
‪static ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer)
Definition: MenuItemViewHelper.php:90
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout