43dfe3534a22ba9ee299d6d0100cbc30fb1e7057
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Uri / TypolinkViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
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 TYPO3\CMS\Core\Utility\GeneralUtility;
17 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
18 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
19 use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22
23 /**
24 * A ViewHelper to create uris from fields supported by the link wizard
25 *
26 * == Example ==
27 *
28 * {link} contains "19 - - - &X=y"
29 * Please note that due to the nature of typolink you have to provide a
30 * full set of parameters if you use the parameter only. Target, class
31 * and title will be discarded.
32 *
33 * <code title="minimal usage">
34 * <f:uri.typolink parameter="{link}" />
35 * <output>
36 * index.php?id=19&X=y
37 * </output>
38 * </code>
39 *
40 * <code title="Full parameter usage">
41 * <f:uri.typolink parameter="{link}" additionalParams="&u=b" useCacheHash="true" />
42 * </code>
43 * <output>
44 * index.php?id=19&X=y&u=b
45 * </output>
46 */
47 class TypolinkViewHelper extends AbstractViewHelper
48 {
49 use CompileWithRenderStatic;
50
51 /**
52 * Initialize arguments
53 */
54 public function initializeArguments()
55 {
56 parent::initializeArguments();
57 $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
58 $this->registerArgument('additionalParams', 'string', 'stdWrap.typolink additionalParams', false, '');
59 $this->registerArgument('useCacheHash', 'bool', '', false, false);
60 }
61
62 /**
63 * @param array $arguments
64 * @param \Closure $renderChildrenClosure
65 * @param RenderingContextInterface $renderingContext
66 *
67 * @return string
68 */
69 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
70 {
71 $parameter = $arguments['parameter'];
72 $additionalParams = $arguments['additionalParams'];
73 $useCacheHash = $arguments['useCacheHash'];
74
75 $content = '';
76 if ($parameter) {
77 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
78 $content = $contentObject->typoLink_URL(
79 [
80 'parameter' => self::createTypolinkParameterFromArguments($parameter, $additionalParams),
81 'useCacheHash' => $useCacheHash,
82 ]
83 );
84 }
85
86 return $content;
87 }
88
89 /**
90 * Transforms ViewHelper arguments to typo3link.parameters.typoscript option as array.
91 *
92 * @param string $parameter Example: 19 _blank - "testtitle with whitespace" &X=y
93 * @param string $additionalParameters
94 *
95 * @return string The final TypoLink string
96 */
97 protected static function createTypolinkParameterFromArguments($parameter, $additionalParameters = '')
98 {
99 $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
100 $typolinkConfiguration = $typoLinkCodec->decode($parameter);
101
102 // Combine additionalParams
103 if ($additionalParameters) {
104 $typolinkConfiguration['additionalParams'] .= $additionalParameters;
105 }
106
107 return $typoLinkCodec->encode($typolinkConfiguration);
108 }
109 }