‪TYPO3CMS  11.5
DatabaseRecordLinkBuilder.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 
21 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22 use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
23 
28 {
32  public function ‪build(array &$linkDetails, string $linkText, string $target, array $conf)
33  {
34  $tsfe = $this->‪getTypoScriptFrontendController();
35  $pageTsConfig = $tsfe->getPagesTSconfig();
36  $configurationKey = $linkDetails['identifier'] . '.';
37  $configuration = $tsfe->tmpl->setup['config.']['recordLinks.'] ?? [];
38  $linkHandlerConfiguration = $pageTsConfig['TCEMAIN.']['linkHandler.'] ?? [];
39 
40  if (!isset($configuration[$configurationKey], $linkHandlerConfiguration[$configurationKey])) {
41  throw new ‪UnableToLinkException(
42  'Configuration how to link "' . $linkDetails['typoLinkParameter'] . '" was not found, so "' . $linkText . '" was not linked.',
43  1490989149,
44  null,
45  $linkText
46  );
47  }
48  $typoScriptConfiguration = $configuration[$configurationKey]['typolink.'];
49  $linkHandlerConfiguration = $linkHandlerConfiguration[$configurationKey]['configuration.'];
50  $databaseTable = $linkHandlerConfiguration['table'];
51 
52  if ($configuration[$configurationKey]['forceLink'] ?? false) {
53  $record = $tsfe->sys_page->getRawRecord($databaseTable, $linkDetails['uid']);
54  } else {
55  $record = $tsfe->sys_page->checkRecord($databaseTable, $linkDetails['uid']);
56  $languageAspect = $tsfe->getContext()->getAspect('language');
57  $languageField = (string)(‪$GLOBALS['TCA'][$databaseTable]['ctrl']['languageField'] ?? '');
58 
59  if (is_array($record) && $languageField !== '') {
60  $languageIdOfRecord = $record[$languageField];
61  // If a record is already in a localized version OR if the record is set to "All Languages"
62  // we allow the generation of the link
63  if ($languageIdOfRecord === 0 && $languageAspect->doOverlays()) {
64  $overlay = $tsfe->sys_page->getLanguageOverlay($databaseTable, $record);
65  // If the record is not translated (overlays enabled), even though it should have been done
66  // We avoid linking to it
67  if (empty($overlay['_LOCALIZED_UID'])) {
68  $record = 0;
69  }
70  }
71  }
72  }
73  if ($record === 0) {
74  throw new ‪UnableToLinkException(
75  'Record not found for "' . $linkDetails['typoLinkParameter'] . '" was not found, so "' . $linkText . '" was not linked.',
76  1490989659,
77  null,
78  $linkText
79  );
80  }
81 
82  // Unset the parameter part of the given TypoScript configuration while keeping
83  // config that has been set in addition.
84  unset($conf['parameter.']);
85 
86  $typoLinkCodecService = GeneralUtility::makeInstance(TypoLinkCodecService::class);
87  $parameterFromDb = $typoLinkCodecService->decode($conf['parameter'] ?? '');
88  unset($parameterFromDb['url']);
89  $parameterFromTypoScript = $typoLinkCodecService->decode($typoScriptConfiguration['parameter'] ?? '');
90  $parameter = array_replace_recursive($parameterFromTypoScript, array_filter($parameterFromDb));
91  $typoScriptConfiguration['parameter'] = $typoLinkCodecService->encode($parameter);
92 
93  $typoScriptConfiguration = array_replace_recursive($conf, $typoScriptConfiguration);
94 
95  if (!empty($linkDetails['fragment'])) {
96  $typoScriptConfiguration['section'] = $linkDetails['fragment'];
97  }
98  // Build the full link to the record
99  $request = $this->contentObjectRenderer->getRequest();
100  $localContentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
101  $localContentObjectRenderer->start($record, $databaseTable, $request);
102  $localContentObjectRenderer->parameters = $this->contentObjectRenderer->parameters;
103  $link = $localContentObjectRenderer->typoLink($linkText, $typoScriptConfiguration);
104 
105  $this->contentObjectRenderer->lastTypoLinkLD = $localContentObjectRenderer->lastTypoLinkLD;
106  $this->contentObjectRenderer->lastTypoLinkUrl = $localContentObjectRenderer->lastTypoLinkUrl;
107  $this->contentObjectRenderer->lastTypoLinkTarget = $localContentObjectRenderer->lastTypoLinkTarget;
108  $this->contentObjectRenderer->lastTypoLinkResult = $localContentObjectRenderer->lastTypoLinkResult;
109 
110  // nasty workaround so typolink stops putting a link together, there is a link already built
111  throw new ‪UnableToLinkException(
112  '',
113  1491130170,
114  null,
115  $link
116  );
117  }
118 }
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50