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