‪TYPO3CMS  10.4
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 
19 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
20 
50 class ‪ActionMenuItemViewHelper extends AbstractTagBasedViewHelper
51 {
55  protected ‪$tagName = 'option';
56 
62  public function ‪initializeArguments()
63  {
64  parent::initializeArguments();
65  $this->registerArgument('label', 'string', 'label of the option tag', true);
66  $this->registerArgument('controller', 'string', 'controller to be associated with this ActionMenuItem', true);
67  $this->registerArgument('action', 'string', 'the action to be associated with this ActionMenuItem', true);
68  $this->registerArgument('arguments', 'array', 'additional controller arguments to be passed to the action when this ActionMenuItem is selected', false, []);
69  }
70 
77  public function ‪render()
78  {
79  $label = $this->arguments['label'];
80  $controller = $this->arguments['controller'];
81  $action = $this->arguments['action'];
82  $arguments = $this->arguments['arguments'];
83 
84  $uri = $this->renderingContext->getControllerContext()->getUriBuilder()->reset()->uriFor($action, $arguments, $controller);
85  $this->tag->addAttribute('value', $uri);
86 
87  if (!$this->tag->hasAttribute('selected')) {
88  $this->‪evaluateSelectItemState($controller, $action, $arguments);
89  }
90 
91  $this->tag->setContent(
92  // Double encode can be set to true, once the typo3fluid/fluid fix is released and required
93  htmlspecialchars($label, ENT_QUOTES, null, false)
94  );
95  return $this->tag->render();
96  }
97 
98  protected function ‪evaluateSelectItemState(string $controller, string $action, array $arguments): void
99  {
100  $currentRequest = $this->renderingContext->getControllerContext()->getRequest();
101  $flatRequestArguments = ‪ArrayUtility::flattenPlain(
102  array_merge([
103  'controller' => $currentRequest->getControllerName(),
104  'action' => $currentRequest->getControllerActionName()
105  ], $currentRequest->getArguments())
106  );
107  $flatViewHelperArguments = ‪ArrayUtility::flattenPlain(
108  array_merge(['controller' => $controller, 'action' => $action], $arguments)
109  );
110  if (
111  $this->arguments['selected'] ||
112  array_diff($flatRequestArguments, $flatViewHelperArguments) === []
113  ) {
114  $this->tag->addAttribute('selected', 'selected');
115  }
116  }
117 }
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\evaluateSelectItemState
‪evaluateSelectItemState(string $controller, string $action, array $arguments)
Definition: ActionMenuItemViewHelper.php:97
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\initializeArguments
‪initializeArguments()
Definition: ActionMenuItemViewHelper.php:61
‪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:76
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper
Definition: ActionMenuItemViewHelper.php:51
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\$tagName
‪string $tagName
Definition: ActionMenuItemViewHelper.php:54
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus
Definition: ActionMenuItemGroupViewHelper.php:16