‪TYPO3CMS  11.5
ActionMenuItemViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
22 
52 class ‪ActionMenuItemViewHelper extends AbstractTagBasedViewHelper
53 {
57  protected ‪$tagName = 'option';
58 
64  public function ‪initializeArguments()
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 
79  public function ‪render()
80  {
81  $label = $this->arguments['label'];
82  $controller = $this->arguments['controller'];
83  $action = $this->arguments['action'];
84  $arguments = $this->arguments['arguments'];
85 
86  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
87  $uriBuilder->setRequest($this->renderingContext->getRequest());
88 
89  $uri = $uriBuilder->reset()->uriFor($action, $arguments, $controller);
90  $this->tag->addAttribute('value', $uri);
91 
92  if (!$this->tag->hasAttribute('selected')) {
93  $this->‪evaluateSelectItemState($controller, $action, $arguments);
94  }
95 
96  $this->tag->setContent(
97  // Double encode can be set to true, once the typo3fluid/fluid fix is released and required
98  htmlspecialchars($label, ENT_QUOTES, '', false)
99  );
100  return $this->tag->render();
101  }
102 
103  protected function ‪evaluateSelectItemState(string $controller, string $action, array $arguments): void
104  {
105  $currentRequest = $this->renderingContext->getRequest();
106  $flatRequestArguments = ‪ArrayUtility::flattenPlain(
107  array_merge([
108  'controller' => $currentRequest->getControllerName(),
109  'action' => $currentRequest->getControllerActionName(),
110  ], $currentRequest->getArguments())
111  );
112  $flatViewHelperArguments = ‪ArrayUtility::flattenPlain(
113  array_merge(['controller' => $controller, 'action' => $action], $arguments)
114  );
115  if (
116  ($this->arguments['selected'] ?? false) ||
117  array_diff($flatRequestArguments, $flatViewHelperArguments) === []
118  ) {
119  $this->tag->addAttribute('selected', 'selected');
120  }
121  }
122 }
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\evaluateSelectItemState
‪evaluateSelectItemState(string $controller, string $action, array $arguments)
Definition: ActionMenuItemViewHelper.php:102
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\initializeArguments
‪initializeArguments()
Definition: ActionMenuItemViewHelper.php:63
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:41
‪TYPO3\CMS\Core\Utility\ArrayUtility\flattenPlain
‪static array flattenPlain(array $array)
Definition: ArrayUtility.php:515
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\render
‪string render()
Definition: ActionMenuItemViewHelper.php:78
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper
Definition: ActionMenuItemViewHelper.php:53
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\$tagName
‪string $tagName
Definition: ActionMenuItemViewHelper.php:56
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus
Definition: ActionMenuItemGroupViewHelper.php:16