TYPO3 CMS  TYPO3_8-7
TypolinkViewHelper.php
Go to the documentation of this file.
1 <?php
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  */
23 
54 {
56 
60  protected $escapeOutput = false;
61 
67  public function initializeArguments()
68  {
69  parent::initializeArguments();
70  $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
71  $this->registerArgument('target', 'string', '', false, '');
72  $this->registerArgument('class', 'string', '', false, '');
73  $this->registerArgument('title', 'string', '', false, '');
74  $this->registerArgument('additionalParams', 'string', '', false, '');
75  $this->registerArgument('additionalAttributes', 'array', '', false, []);
76  $this->registerArgument('useCacheHash', 'bool', '', false, false);
77  $this->registerArgument('addQueryString', 'bool', '', false, false);
78  $this->registerArgument('addQueryStringMethod', 'string', '', false, 'GET');
79  $this->registerArgument('addQueryStringExclude', 'string', '', false, '');
80  $this->registerArgument('absolute', 'bool', 'Ensure the resulting URL is an absolute URL', false, false);
81  }
82 
93  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
94  {
95  $parameter = $arguments['parameter'];
96  $target = $arguments['target'];
97  $class = $arguments['class'];
98  $title = $arguments['title'];
99  $additionalParams = $arguments['additionalParams'];
100  $additionalAttributes = $arguments['additionalAttributes'];
101  $useCacheHash = $arguments['useCacheHash'];
102  $addQueryString = $arguments['addQueryString'];
103  $addQueryStringMethod = $arguments['addQueryStringMethod'];
104  $addQueryStringExclude = $arguments['addQueryStringExclude'];
105  $absolute = $arguments['absolute'];
106 
107  // Merge the $parameter with other arguments
108  $typolinkParameter = self::createTypolinkParameterArrayFromArguments($parameter, $target, $class, $title, $additionalParams);
109 
110  // array(param1 -> value1, param2 -> value2) --> param1="value1" param2="value2" for typolink.ATagParams
111  $extraAttributes = [];
112  foreach ($additionalAttributes as $attributeName => $attributeValue) {
113  $extraAttributes[] = $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
114  }
115  $aTagParams = implode(' ', $extraAttributes);
116 
117  // If no link has to be rendered, the inner content will be returned as such
118  $content = (string)$renderChildrenClosure();
119 
120  if ($parameter) {
122  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
123  $contentObject->start([], '');
124  $content = $contentObject->stdWrap(
125  $content,
126  [
127  'typolink.' => [
128  'parameter' => $typolinkParameter,
129  'ATagParams' => $aTagParams,
130  'useCacheHash' => $useCacheHash,
131  'addQueryString' => $addQueryString,
132  'addQueryString.' => [
133  'method' => $addQueryStringMethod,
134  'exclude' => $addQueryStringExclude
135  ],
136  'forceAbsoluteUrl' => $absolute
137  ]
138  ]
139  );
140  }
141 
142  return $content;
143  }
144 
156  protected static function createTypolinkParameterArrayFromArguments($parameter, $target = '', $class = '', $title = '', $additionalParams = '')
157  {
158  $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
159  $typolinkConfiguration = $typoLinkCodec->decode($parameter);
160  if (empty($typolinkConfiguration)) {
161  return $typolinkConfiguration;
162  }
163 
164  // Override target if given in target argument
165  if ($target) {
166  $typolinkConfiguration['target'] = $target;
167  }
168 
169  // Combine classes if given in both "parameter" string and "class" argument
170  if ($class) {
171  $classes = explode(' ', trim($typolinkConfiguration['class']) . ' ' . trim($class));
172  $typolinkConfiguration['class'] = implode(' ', array_unique(array_filter($classes)));
173  }
174 
175  // Override title if given in title argument
176  if ($title) {
177  $typolinkConfiguration['title'] = $title;
178  }
179 
180  // Combine additionalParams
181  if ($additionalParams) {
182  $typolinkConfiguration['additionalParams'] .= $additionalParams;
183  }
184 
185  return $typoLinkCodec->encode($typolinkConfiguration);
186  }
187 }
static makeInstance($className,... $constructorArguments)