TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Link/TypolinkViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
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  */
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23 
55 {
56  use CompileWithRenderStatic;
57 
61  protected $escapeOutput = false;
62 
68  public function initializeArguments()
69  {
70  parent::initializeArguments();
71  $this->registerArgument('parameter', 'string', 'stdWrap.typolink style parameter string', true);
72  $this->registerArgument('target', 'string', '', false, '');
73  $this->registerArgument('class', 'string', '', false, '');
74  $this->registerArgument('title', 'string', '', false, '');
75  $this->registerArgument('additionalParams', 'string', '', false, '');
76  $this->registerArgument('additionalAttributes', 'array', '', false, []);
77  }
78 
89  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
90  {
91  $parameter = $arguments['parameter'];
92  $target = $arguments['target'];
93  $class = $arguments['class'];
94  $title = $arguments['title'];
95  $additionalParams = $arguments['additionalParams'];
96  $additionalAttributes = $arguments['additionalAttributes'];
97 
98  // Merge the $parameter with other arguments
99  $typolinkParameter = self::createTypolinkParameterArrayFromArguments($parameter, $target, $class, $title, $additionalParams);
100 
101  // array(param1 -> value1, param2 -> value2) --> param1="value1" param2="value2" for typolink.ATagParams
102  $extraAttributes = [];
103  foreach ($additionalAttributes as $attributeName => $attributeValue) {
104  $extraAttributes[] = $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
105  }
106  $aTagParams = implode(' ', $extraAttributes);
107 
108  // If no link has to be rendered, the inner content will be returned as such
109  $content = (string)$renderChildrenClosure();
110 
111  if ($parameter) {
113  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
114  $contentObject->start([], '');
115  $content = $contentObject->stdWrap(
116  $content,
117  [
118  'typolink.' => [
119  'parameter' => $typolinkParameter,
120  'ATagParams' => $aTagParams,
121  ]
122  ]
123  );
124  }
125 
126  return $content;
127  }
128 
140  protected static function createTypolinkParameterArrayFromArguments($parameter, $target = '', $class = '', $title = '', $additionalParams = '')
141  {
142  $typoLinkCodec = GeneralUtility::makeInstance(TypoLinkCodecService::class);
143  $typolinkConfiguration = $typoLinkCodec->decode($parameter);
144  if (empty($typolinkConfiguration)) {
145  return $typolinkConfiguration;
146  }
147 
148  // Override target if given in target argument
149  if ($target) {
150  $typolinkConfiguration['target'] = $target;
151  }
152 
153  // Combine classes if given in both "parameter" string and "class" argument
154  if ($class) {
155  $classes = explode(' ', trim($typolinkConfiguration['class']) . ' ' . trim($class));
156  $typolinkConfiguration['class'] = implode(' ', array_unique(array_filter($classes)));
157  }
158 
159  // Override title if given in title argument
160  if ($title) {
161  $typolinkConfiguration['title'] = $title;
162  }
163 
164  // Combine additionalParams
165  if ($additionalParams) {
166  $typolinkConfiguration['additionalParams'] .= $additionalParams;
167  }
168 
169  return $typoLinkCodec->encode($typolinkConfiguration);
170  }
171 }
static makeInstance($className,...$constructorArguments)