‪TYPO3CMS  9.5
MenuItemViewHelper.php
Go to the documentation of this file.
1 <?php
2 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 
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
25 use TYPO3Fluid\Fluid\View\Exception;
26 
42 class ‪MenuItemViewHelper extends AbstractViewHelper
43 {
44  use CompileWithRenderStatic;
45 
51  public function ‪initializeArguments()
52  {
53  parent::initializeArguments();
54  $this->registerArgument('label', 'string', 'Label of the menu item', true);
55  $this->registerArgument('uri', 'string', 'Action uri', true);
56  }
57 
65  public static function ‪renderStatic(
66  array $arguments,
67  \Closure $renderChildrenClosure,
68  RenderingContextInterface $renderingContext
69  ) {
70  $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
71  ‪self::ensureProperNesting($viewHelperVariableContainer);
72 
74  $menu = $viewHelperVariableContainer->get(ModuleLayoutViewHelper::class, Menu::class);
75  $menuItem = $menu->makeMenuItem();
76  $menuItem->setTitle($arguments['label']);
77  $menuItem->setHref($arguments['uri']);
78  $menuItem->setActive(self::isCurrentUri($arguments['uri']));
79  $menu->addMenuItem($menuItem);
80  }
81 
86  private static function ‪ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer): void
87  {
88  if (!$viewHelperVariableContainer->exists(ModuleLayoutViewHelper::class, Menu::class)) {
89  throw new Exception(sprintf('%s must be nested in <f.be.moduleLayout.menu> ViewHelper', self::class), 1531235592);
90  }
91  }
92 
97  protected static function ‪isCurrentUri(string $uri): bool
98  {
99  return GeneralUtility::getIndpEnv('REQUEST_URI') === $uri;
100  }
101 }
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper
Definition: MenuItemViewHelper.php:43
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\initializeArguments
‪initializeArguments()
Definition: MenuItemViewHelper.php:50
‪TYPO3\CMS\Backend\Template\Components\Menu\Menu
Definition: Menu.php:23
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: MenuItemViewHelper.php:64
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayoutViewHelper
Definition: ModuleLayoutViewHelper.php:46
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\ensureProperNesting
‪static ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer)
Definition: MenuItemViewHelper.php:85
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\MenuItemViewHelper\isCurrentUri
‪static bool isCurrentUri(string $uri)
Definition: MenuItemViewHelper.php:96
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45