‪TYPO3CMS  9.5
FileOrFolderLinkBuilder.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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  */
20 
25 {
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  if ($linkLocation === null) {
45  // set the linkLocation to an empty string if null,
46  // so it does not collide with the various string functions
47  $linkLocation = '';
48  }
49  // Setting title if blank value to link
50  $linkText = $this->‪encodeFallbackLinkTextIfLinkTextIsEmpty($linkText, rawurldecode($linkLocation));
51  if (strpos($linkLocation, '/') !== 0
52  && parse_url($linkLocation, PHP_URL_SCHEME) === null
53  ) {
54  $linkLocation = $tsfe->absRefPrefix . $linkLocation;
55  }
56  $url = $this->‪processUrl(‪UrlProcessorInterface::CONTEXT_FILE, $linkLocation, $conf);
57  if (!empty($linkDetails['fragment'])) {
58  $url .= '#' . $linkDetails['fragment'];
59  }
60  return [
61  $this->‪forceAbsoluteUrl($url, $conf),
62  $linkText,
63  $target ?: $this->‪resolveTargetAttribute($conf, 'fileTarget', false, $tsfe->fileTarget)
64  ];
65  }
66 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Frontend\Http\UrlProcessorInterface
Definition: UrlProcessorInterface.php:24
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Frontend\Http\UrlProcessorInterface\CONTEXT_FILE
‪const CONTEXT_FILE
Definition: UrlProcessorInterface.php:26