‪TYPO3CMS  10.4
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 
23 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
27 use TYPO3Fluid\Fluid\View\Exception;
28 
44 class ‪MenuItemViewHelper extends AbstractViewHelper
45 {
46  use CompileWithRenderStatic;
47 
53  public function ‪initializeArguments()
54  {
55  parent::initializeArguments();
56  $this->registerArgument('label', 'string', 'Label of the menu item', true);
57  $this->registerArgument('uri', 'string', 'Action uri', true);
58  }
59 
67  public static function ‪renderStatic(
68  array $arguments,
69  \Closure $renderChildrenClosure,
70  RenderingContextInterface $renderingContext
71  ) {
72  $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
73  ‪self::ensureProperNesting($viewHelperVariableContainer);
74 
76  $menu = $viewHelperVariableContainer->get(ModuleLayoutViewHelper::class, Menu::class);
77  $menuItem = $menu->makeMenuItem();
78  $menuItem->setTitle($arguments['label']);
79  $menuItem->setHref($arguments['uri']);
80  $menuItem->setActive(self::isCurrentUri($arguments['uri']));
81  $menu->addMenuItem($menuItem);
82  }
83 
88  private static function ‪ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer): void
89  {
90  if (!$viewHelperVariableContainer->exists(ModuleLayoutViewHelper::class, Menu::class)) {
91  throw new Exception(sprintf('%s must be nested in <f.be.moduleLayout.menu> ViewHelper', self::class), 1531235592);
92  }
93  }
94 
99  protected static function ‪isCurrentUri(string $uri): bool
100  {
101  return GeneralUtility::getIndpEnv('REQUEST_URI') === $uri;
102  }
103 }
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper
Definition: MenuItemViewHelper.php:45
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\initializeArguments
‪initializeArguments()
Definition: MenuItemViewHelper.php:52
‪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:66
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayoutViewHelper
Definition: ModuleLayoutViewHelper.php:48
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\ensureProperNesting
‪static ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer)
Definition: MenuItemViewHelper.php:87
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\isCurrentUri
‪static bool isCurrentUri(string $uri)
Definition: MenuItemViewHelper.php:98
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46