[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Widget / UriViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Widget;
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
17 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
20
21 /**
22 * A view helper for creating URIs to extbase actions within widgets.
23 *
24 * = Examples =
25 *
26 * <code title="URI to the show-action of the current controller">
27 * <f:widget.uri action="show" />
28 * </code>
29 * <output>
30 * index.php?id=123&tx_myextension_plugin[widgetIdentifier][action]=show&tx_myextension_plugin[widgetIdentifier][controller]=Standard&cHash=xyz
31 * (depending on the current page, widget and your TS configuration)
32 * </output>
33 */
34 class UriViewHelper extends AbstractViewHelper
35 {
36 use CompileWithRenderStatic;
37
38 /**
39 * Initialize arguments
40 */
41 public function initializeArguments()
42 {
43 $this->registerArgument('useCacheHash', 'bool', 'True whether the cache hash should be appended to the URL', false, false);
44 $this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
45 $this->registerArgument('action', 'string', 'Target action');
46 $this->registerArgument('arguments', 'array', 'Arguments', false, []);
47 $this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
48 $this->registerArgument('format', 'string', 'The requested format, e.g. ".html', false, '');
49 $this->registerArgument('ajax', 'bool', 'TRUE if the URI should be to an AJAX widget, FALSE otherwise.', false, false);
50 }
51
52 /**
53 * @param array $arguments
54 * @param \Closure $renderChildrenClosure
55 * @param RenderingContextInterface $renderingContext
56 * @return string
57 */
58 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
59 {
60 $ajax = $arguments['ajax'];
61
62 if ($ajax === true) {
63 return static::getAjaxUri($renderingContext, $arguments);
64 }
65 return static::getWidgetUri($renderingContext, $arguments);
66 }
67
68 /**
69 * Get the URI for an AJAX Request.
70 *
71 * @param RenderingContextInterface $renderingContext
72 * @param array $arguments
73 * @return string the AJAX URI
74 */
75 protected static function getAjaxUri(RenderingContextInterface $renderingContext, array $arguments)
76 {
77 $controllerContext = $renderingContext->getControllerContext();
78 $action = $arguments['action'];
79 $arguments = $arguments['arguments'];
80 if ($action === null) {
81 $action = $controllerContext->getRequest()->getControllerActionName();
82 }
83 $arguments['id'] = $GLOBALS['TSFE']->id;
84 // @todo page type should be configurable
85 $arguments['type'] = 7076;
86 $arguments['fluid-widget-id'] = $controllerContext->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier();
87 $arguments['action'] = $action;
88 return '?' . http_build_query($arguments, null, '&');
89 }
90
91 /**
92 * Get the URI for a non-AJAX Request.
93 *
94 * @param RenderingContextInterface $renderingContext
95 * @param array $arguments
96 * @return string the Widget URI
97 */
98 protected static function getWidgetUri(RenderingContextInterface $renderingContext, array $arguments)
99 {
100 $controllerContext = $renderingContext->getControllerContext();
101 $uriBuilder = $controllerContext->getUriBuilder();
102 $argumentPrefix = $controllerContext->getRequest()->getArgumentPrefix();
103 $parameters = $arguments['arguments'] ?? [];
104 if ($arguments['action'] ?? false) {
105 $parameters['action'] = $arguments['action'];
106 }
107 if (($arguments['format'] ?? '') !== '') {
108 $parameters['format'] = $arguments['format'];
109 }
110 return $uriBuilder->reset()
111 ->setArguments([$argumentPrefix => $parameters])
112 ->setSection($arguments['section'])
113 ->setUseCacheHash($arguments['useCacheHash'])
114 ->setAddQueryString(true)
115 ->setAddQueryStringMethod($arguments['addQueryStringMethod'])
116 ->setArgumentsToBeExcludedFromQueryString([$argumentPrefix, 'cHash'])
117 ->setFormat($arguments['format'])
118 ->build();
119 }
120 }