‪TYPO3CMS  ‪main
ActionMenuItemViewHelper.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 
25 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
26 
57 final class ‪ActionMenuItemViewHelper extends AbstractTagBasedViewHelper
58 {
62  protected ‪$tagName = 'option';
63 
64  public function ‪initializeArguments(): void
65  {
66  parent::initializeArguments();
67  $this->registerArgument('label', 'string', 'label of the option tag', true);
68  $this->registerArgument('controller', 'string', 'controller to be associated with this ActionMenuItem', true);
69  $this->registerArgument('action', 'string', 'the action to be associated with this ActionMenuItem', true);
70  $this->registerArgument('arguments', 'array', 'additional controller arguments to be passed to the action when this ActionMenuItem is selected', false, []);
71  }
72 
73  public function ‪render(): string
74  {
75  $label = $this->arguments['label'];
76  $controller = $this->arguments['controller'];
77  $action = $this->arguments['action'];
78  $arguments = $this->arguments['arguments'];
79 
80  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
82  $renderingContext = $this->renderingContext;
83  $request = $renderingContext->getRequest();
84  if (!$request instanceof ‪RequestInterface) {
85  // Throw if not an extbase request
86  throw new \RuntimeException(
87  'ViewHelper f:be.menus.actionMenuItem needs an extbase Request object to create URIs.',
88  1639741792
89  );
90  }
91  $uriBuilder->setRequest($request);
92 
93  $uri = $uriBuilder->reset()->uriFor($action, $arguments, $controller);
94  $this->tag->addAttribute('value', $uri);
95 
96  if (!$this->tag->hasAttribute('selected')) {
97  $this->‪evaluateSelectItemState($controller, $action, $arguments);
98  }
99 
100  $this->tag->setContent(htmlspecialchars($label, ENT_QUOTES, '', true));
101  return $this->tag->render();
102  }
103 
104  protected function ‪evaluateSelectItemState(string $controller, string $action, array $arguments): void
105  {
107  $renderingContext = $this->renderingContext;
109  $request = $renderingContext->getRequest();
110  $flatRequestArguments = ‪ArrayUtility::flattenPlain(
111  array_merge([
112  'controller' => $request->getControllerName(),
113  'action' => $request->getControllerActionName(),
114  ], $request->getArguments())
115  );
116  $flatViewHelperArguments = ‪ArrayUtility::flattenPlain(
117  array_merge(['controller' => $controller, 'action' => $action], $arguments)
118  );
119  if (
120  ($this->arguments['selected'] ?? false) ||
121  array_diff($flatRequestArguments, $flatViewHelperArguments) === []
122  ) {
123  $this->tag->addAttribute('selected', 'selected');
124  }
125  }
126 }
‪TYPO3\CMS\Core\Utility\ArrayUtility\flattenPlain
‪static flattenPlain(array $array)
Definition: ArrayUtility.php:496
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\evaluateSelectItemState
‪evaluateSelectItemState(string $controller, string $action, array $arguments)
Definition: ActionMenuItemViewHelper.php:103
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\initializeArguments
‪initializeArguments()
Definition: ActionMenuItemViewHelper.php:63
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\render
‪render()
Definition: ActionMenuItemViewHelper.php:72
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper
Definition: ActionMenuItemViewHelper.php:58
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\$tagName
‪string $tagName
Definition: ActionMenuItemViewHelper.php:61
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus
Definition: ActionMenuItemGroupViewHelper.php:18