80ce278ba58f7f4a6a12d9541e6c04308a39a74c
[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" />
42 * </code>
43 * <output>
44 * index.php?id=19&X=y&u=b
45 * </output>
46 *
47 */
48 class TypolinkViewHelper extends AbstractViewHelper
49 {
50 use CompileWithRenderStatic;
51
52 /**
53 * Initialize arguments
54 */
55 public function initializeArguments()
56 {
57 parent::initializeArguments();
58 $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
59 $this->registerArgument('additionalParams', 'string', 'stdWrap.typolink additionalParams', 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
74 $content = '';
75 if ($parameter) {
76 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
77 $content = $contentObject->typoLink_URL(
78 [
79 'parameter' => self::createTypolinkParameterFromArguments($parameter, $additionalParams),
80 ]
81 );
82 }
83
84 return $content;
85 }
86
87 /**
88 * Transforms ViewHelper arguments to typo3link.parameters.typoscript option as array.
89 *
90 * @param string $parameter Example: 19 _blank - "testtitle with whitespace" &X=y
91 * @param string $additionalParameters
92 *
93 * @return string The final TypoLink string
94 */
95 protected static function createTypolinkParameterFromArguments($parameter, $additionalParameters = '')
96 {
97 $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
98 $typolinkConfiguration = $typoLinkCodec->decode($parameter);
99
100 // Combine additionalParams
101 if ($additionalParameters) {
102 $typolinkConfiguration['additionalParams'] .= $additionalParameters;
103 }
104
105 return $typoLinkCodec->encode($typolinkConfiguration);
106 }
107 }