‪TYPO3CMS  9.5
ActionMenuItemViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
19 
49 class ‪ActionMenuItemViewHelper extends AbstractTagBasedViewHelper
50 {
54  protected ‪$tagName = 'option';
55 
61  public function ‪initializeArguments()
62  {
63  parent::initializeArguments();
64  $this->registerArgument('label', 'string', 'label of the option tag', true);
65  $this->registerArgument('controller', 'string', 'controller to be associated with this ActionMenuItem', true);
66  $this->registerArgument('action', 'string', 'the action to be associated with this ActionMenuItem', true);
67  $this->registerArgument('arguments', 'array', 'additional controller arguments to be passed to the action when this ActionMenuItem is selected', false, []);
68  }
69 
76  public function ‪render()
77  {
78  $label = $this->arguments['label'];
79  $controller = $this->arguments['controller'];
80  $action = $this->arguments['action'];
81  $arguments = $this->arguments['arguments'];
82 
83  $uri = $this->renderingContext->getControllerContext()->getUriBuilder()->reset()->uriFor($action, $arguments, $controller);
84  $this->tag->addAttribute('value', $uri);
85 
86  if (!$this->tag->hasAttribute('selected')) {
87  $this->‪evaluateSelectItemState($controller, $action, $arguments);
88  }
89 
90  $this->tag->setContent(
91  // Double encode can be set to true, once the typo3fluid/fluid fix is released and required
92  htmlspecialchars($label, ENT_QUOTES, null, false)
93  );
94  return $this->tag->render();
95  }
96 
97  protected function ‪evaluateSelectItemState(string $controller, string $action, array $arguments): void
98  {
99  $currentRequest = $this->renderingContext->getControllerContext()->getRequest();
100  $flatRequestArguments = ‪ArrayUtility::flatten(
101  array_merge([
102  'controller' => $currentRequest->getControllerName(),
103  'action' => $currentRequest->getControllerActionName()
104  ], $currentRequest->getArguments())
105  );
106  $flatViewHelperArguments = ‪ArrayUtility::flatten(
107  array_merge(['controller' => $controller, 'action' => $action], $arguments)
108  );
109  if (
110  $this->arguments['selected'] ||
111  array_diff($flatRequestArguments, $flatViewHelperArguments) === []
112  ) {
113  $this->tag->addAttribute('selected', 'selected');
114  }
115  }
116 }
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\evaluateSelectItemState
‪evaluateSelectItemState(string $controller, string $action, array $arguments)
Definition: ActionMenuItemViewHelper.php:96
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\initializeArguments
‪initializeArguments()
Definition: ActionMenuItemViewHelper.php:60
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Core\Utility\ArrayUtility\flatten
‪static array flatten(array $array, $prefix='')
Definition: ArrayUtility.php:479
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\render
‪string render()
Definition: ActionMenuItemViewHelper.php:75
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper
Definition: ActionMenuItemViewHelper.php:50
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuItemViewHelper\$tagName
‪string $tagName
Definition: ActionMenuItemViewHelper.php:53
‪TYPO3\CMS\Fluid\ViewHelpers\Be\Menus
Definition: ActionMenuItemGroupViewHelper.php:2