[TASK] Make \Be\Menus\ActionMenuItemViewHelper compilable
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Be / Menus / ActionMenuItemViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Be\Menus;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
25 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
26 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
27
28 /**
29 * View helper which returns a option tag.
30 * This view helper only works in conjunction with \TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuViewHelper
31 * Note: This view helper is experimental!
32 *
33 * = Examples =
34 *
35 * <code title="Simple">
36 * <f:be.menus.actionMenu>
37 * <f:be.menus.actionMenuItem label="Overview" controller="Blog" action="index" />
38 * <f:be.menus.actionMenuItem label="Create new Blog" controller="Blog" action="new" />
39 * <f:be.menus.actionMenuItem label="List Posts" controller="Post" action="index" arguments="{blog: blog}" />
40 * </f:be.menus.actionMenu>
41 * </code>
42 * <output>
43 * Selectbox with the options "Overview", "Create new Blog" and "List Posts"
44 * </output>
45 *
46 * <code title="Localized">
47 * <f:be.menus.actionMenu>
48 * <f:be.menus.actionMenuItem label="{f:translate(key='overview')}" controller="Blog" action="index" />
49 * <f:be.menus.actionMenuItem label="{f:translate(key='create_blog')}" controller="Blog" action="new" />
50 * </f:be.menus.actionMenu>
51 * </code>
52 * <output>
53 * localized selectbox
54 * <output>
55 */
56 class ActionMenuItemViewHelper extends AbstractTagBasedViewHelper implements CompilableInterface {
57
58 /**
59 * Renders an ActionMenu option tag
60 *
61 * @param string $label label of the option tag
62 * @param string $controller controller to be associated with this ActionMenuItem
63 * @param string $action the action to be associated with this ActionMenuItem
64 * @param array $arguments additional controller arguments to be passed to the action when this ActionMenuItem is
65 * selected
66 * @return string the rendered option tag
67 * @see \TYPO3\CMS\Fluid\ViewHelpers\Be\Menus\ActionMenuViewHelper
68 */
69 public function render($label, $controller, $action, array $arguments = array()) {
70 return self::renderStatic(
71 array(
72 'label' => $label,
73 'controller' => $controller,
74 'action' => $action,
75 'arguments' => $arguments
76 ),
77 $this->buildRenderChildrenClosure(),
78 $this->renderingContext
79 );
80 }
81
82 /**
83 * Render static for more speed in fluid
84 *
85 * @param array $arguments
86 * @param callable $renderChildrenClosure
87 * @param RenderingContextInterface $renderingContext
88 * @return string
89 */
90 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
91 $label = $arguments['label'];
92 $controller = $arguments['controller'];
93 $action = $arguments['action'];
94 $arguments = $arguments['arguments'];
95
96 $tag = static::getTagBuilder();
97 $tag->setTagName('option');
98 $uriBuilder = $renderingContext->getControllerContext()->getUriBuilder();
99 $uri = $uriBuilder->reset()->uriFor($action, $arguments, $controller);
100
101 $tag->addAttribute('value', $uri);
102 $currentRequest = $renderingContext->getControllerContext()->getRequest();
103 $currentController = $currentRequest->getControllerName();
104 $currentAction = $currentRequest->getControllerActionName();
105 if ($action === $currentAction && $controller === $currentController) {
106 $tag->addAttribute('selected', 'selected');
107 }
108 $tag->setContent($label);
109 return $tag->render();
110 }
111
112
113 }