TYPO3 CMS  TYPO3_7-6
TypolinkViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is part of the TYPO3 project - inspiring people to share! *
6  * *
7  * TYPO3 is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU General Public License version 2 as published by *
9  * the Free Software Foundation. *
10  * *
11  * This script is distributed in the hope that it will be useful, but *
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14  * Public License for more details. *
15  * */
16 
23 
55 {
68  public function render($parameter, $target = '', $class = '', $title = '', $additionalParams = '', $additionalAttributes = [])
69  {
70  return static::renderStatic(
71  [
72  'parameter' => $parameter,
73  'target' => $target,
74  'class' => $class,
75  'title' => $title,
76  'additionalParams' => $additionalParams,
77  'additionalAttributes' => $additionalAttributes
78  ],
80  $this->renderingContext
81  );
82  }
83 
93  {
94  $parameter = $arguments['parameter'];
95  $target = $arguments['target'];
96  $class = $arguments['class'];
97  $title = $arguments['title'];
98  $additionalParams = $arguments['additionalParams'];
99  $additionalAttributes = $arguments['additionalAttributes'];
100 
101  // Merge the $parameter with other arguments
102  $typolinkParameter = self::createTypolinkParameterArrayFromArguments($parameter, $target, $class, $title, $additionalParams);
103 
104  // array(param1 -> value1, param2 -> value2) --> param1="value1" param2="value2" for typolink.ATagParams
105  $extraAttributes = [];
106  foreach ($additionalAttributes as $attributeName => $attributeValue) {
107  $extraAttributes[] = $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
108  }
109  $aTagParams = implode(' ', $extraAttributes);
110 
111  // If no link has to be rendered, the inner content will be returned as such
112  $content = (string)$renderChildrenClosure();
113 
114  if ($parameter) {
116  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
117  $contentObject->start([], '');
118  $content = $contentObject->stdWrap(
119  $content,
120  [
121  'typolink.' => [
122  'parameter' => $typolinkParameter,
123  'ATagParams' => $aTagParams,
124  ]
125  ]
126  );
127  }
128 
129  return $content;
130  }
131 
143  protected static function createTypolinkParameterArrayFromArguments($parameter, $target = '', $class = '', $title = '', $additionalParams = '')
144  {
145  $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
146  $typolinkConfiguration = $typoLinkCodec->decode($parameter);
147  if (empty($typolinkConfiguration)) {
148  return $typolinkConfiguration;
149  }
150 
151  // Override target if given in target argument
152  if ($target) {
153  $typolinkConfiguration['target'] = $target;
154  }
155 
156  // Combine classes if given in both "parameter" string and "class" argument
157  if ($class) {
158  $classes = explode(' ', trim($typolinkConfiguration['class']) . ' ' . trim($class));
159  $typolinkConfiguration['class'] = implode(' ', array_unique(array_filter($classes)));
160  }
161 
162  // Override title if given in title argument
163  if ($title) {
164  $typolinkConfiguration['title'] = $title;
165  }
166 
167  // Combine additionalParams
168  if ($additionalParams) {
169  $typolinkConfiguration['additionalParams'] .= $additionalParams;
170  }
171 
172  return $typoLinkCodec->encode($typolinkConfiguration);
173  }
174 }
static renderStatic(array $arguments, \Closure $renderChildrenClosure, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)