‪TYPO3CMS  10.4
FileLinkHandler.php
Go to the documentation of this file.
1 <?php
2 
3 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  */
17 
19 
24 
31 {
32 
37  protected ‪$baseUrn = 't3://file';
38 
43  protected ‪$resourceFactory;
44 
51  public function ‪asString(array $parameters): string
52  {
53  if ($parameters['file'] === null) {
54  return '';
55  }
56  $uid = $parameters['file']->getUid();
57  // I am not sure about this use case. Maybe if the file was not indexed and saved to DB (migration from old systems)
58  if ($uid > 0) {
59  $urn = '?uid=' . $uid;
60  } else {
61  $identifier = $parameters['file']->getIdentifier();
62  $urn = '?identifier=' . urlencode($identifier);
63  }
64  if (!empty($parameters['fragment'])) {
65  $urn .= '#' . $parameters['fragment'];
66  }
67  return $this->baseUrn . $urn;
68  }
69 
77  public function ‪resolveHandlerData(array $data): array
78  {
79  try {
80  $file = $this->‪resolveFile($data);
81  } catch (‪FileDoesNotExistException $e) {
82  $file = null;
83  }
84  $result = ['file' => $file];
85  if (!empty($data['fragment'])) {
86  $result['fragment'] = $data['fragment'];
87  }
88  return $result;
89  }
90 
96  protected function ‪resolveFile(array $data): ?‪FileInterface
97  {
98  if (isset($data['uid'])) {
99  return $this->‪getResourceFactory()->‪getFileObject($data['uid']);
100  }
101  if (isset($data['identifier'])) {
102  return $this->‪getResourceFactory()->‪getFileObjectFromCombinedIdentifier($data['identifier']);
103  }
104  return null;
105  }
106 
112  protected function ‪getResourceFactory(): ‪ResourceFactory
113  {
114  if (!$this->resourceFactory) {
115  $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
116  }
118  }
119 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
Definition: FileDoesNotExistException.php:22
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObjectFromCombinedIdentifier
‪File ProcessedFile null getFileObjectFromCombinedIdentifier($identifier)
Definition: ResourceFactory.php:415
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObject
‪File getFileObject($uid, array $fileData=[])
Definition: ResourceFactory.php:390
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46