[FEATURE] Allow extended custom links in FormEngine and TypoLink
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Typolink / FileOrFolderLinkBuilder.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Frontend\Typolink;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17 use TYPO3\CMS\Core\Resource\FileInterface;
18 use TYPO3\CMS\Core\Resource\Folder;
19 use TYPO3\CMS\Frontend\Http\UrlProcessorInterface;
20
21 /**
22 * Builds a TypoLink to a folder or file
23 */
24 class FileOrFolderLinkBuilder extends AbstractTypolinkBuilder
25 {
26 /**
27 * @inheritdoc
28 */
29 public function build(array &$linkDetails, string $linkText, string $target, array $conf): array
30 {
31 $fileOrFolderObject = $linkDetails['file'] ? $linkDetails['file'] : $linkDetails['folder'];
32 // check if the file exists or if a / is contained (same check as in detectLinkType)
33 if (!($fileOrFolderObject instanceof FileInterface) && !($fileOrFolderObject instanceof Folder)) {
34 throw new UnableToLinkException(
35 'File "' . $linkDetails['typoLinkParameter'] . '" did not exist, so "' . $linkText . '" was not linked.',
36 1490989449,
37 null,
38 $linkText
39 );
40 }
41
42 $tsfe = $this->getTypoScriptFrontendController();
43 $linkLocation = $fileOrFolderObject->getPublicUrl();
44 // Setting title if blank value to link
45 $linkText = $this->parseFallbackLinkTextIfLinkTextIsEmpty($linkText, rawurldecode($linkLocation));
46 $linkLocation = (strpos($linkLocation, '/') !== 0 ? $tsfe->absRefPrefix : '') . $linkLocation;
47 $url = $this->processUrl(UrlProcessorInterface::CONTEXT_FILE, $linkLocation, $conf);
48 return [
49 $this->forceAbsoluteUrl($url, $conf),
50 $linkText,
51 $target ?: $this->resolveTargetAttribute($conf, 'fileTarget', false, $tsfe->fileTarget)
52 ];
53 }
54 }