[BUGFIX] Use params set in db in recordLinkHandler
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Typolink / DatabaseRecordLinkBuilder.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
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
20 use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
21
22 /**
23 * Builds a TypoLink to a database record
24 */
25 class DatabaseRecordLinkBuilder extends AbstractTypolinkBuilder
26 {
27 /**
28 * @inheritdoc
29 */
30 public function build(array &$linkDetails, string $linkText, string $target, array $conf): array
31 {
32 $tsfe = $this->getTypoScriptFrontendController();
33 $pageTsConfig = $tsfe->getPagesTSconfig();
34 $configurationKey = $linkDetails['identifier'] . '.';
35 $configuration = $tsfe->tmpl->setup['config.']['recordLinks.'];
36 $linkHandlerConfiguration = $pageTsConfig['TCEMAIN.']['linkHandler.'];
37
38 if (!isset($configuration[$configurationKey], $linkHandlerConfiguration[$configurationKey])) {
39 throw new UnableToLinkException(
40 'Configuration how to link "' . $linkDetails['typoLinkParameter'] . '" was not found, so "' . $linkText . '" was not linked.',
41 1490989149,
42 null,
43 $linkText
44 );
45 }
46 $typoScriptConfiguration = $configuration[$configurationKey]['typolink.'];
47 $linkHandlerConfiguration = $linkHandlerConfiguration[$configurationKey]['configuration.'];
48
49 if ($configuration[$configurationKey]['forceLink']) {
50 $record = $tsfe->sys_page->getRawRecord($linkHandlerConfiguration['table'], $linkDetails['uid']);
51 } else {
52 $record = $tsfe->sys_page->checkRecord($linkHandlerConfiguration['table'], $linkDetails['uid']);
53 }
54 if ($record === 0) {
55 throw new UnableToLinkException(
56 'Record not found for "' . $linkDetails['typoLinkParameter'] . '" was not found, so "' . $linkText . '" was not linked.',
57 1490989659,
58 null,
59 $linkText
60 );
61 }
62
63 // Unset the parameter part of the given TypoScript configuration while keeping
64 // config that has been set in addition.
65 unset($conf['parameter.']);
66
67 $typoLinkCodecService = GeneralUtility::makeInstance(TypoLinkCodecService::class);
68 $parameterFromDb = $typoLinkCodecService->decode($conf['parameter']);
69 unset($parameterFromDb['url']);
70 $parameterFromTypoScript = $typoLinkCodecService->decode($typoScriptConfiguration['parameter']);
71 $parameter = array_replace_recursive($parameterFromTypoScript, array_filter($parameterFromDb));
72 $typoScriptConfiguration['parameter'] = $typoLinkCodecService->encode($parameter);
73
74 $typoScriptConfiguration = array_replace_recursive($conf, $typoScriptConfiguration);
75
76 if (!empty($linkDetails['fragment'])) {
77 $typoScriptConfiguration['section'] = $linkDetails['fragment'];
78 }
79
80 // Build the full link to the record
81 $localContentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
82 $localContentObjectRenderer->start($record, $linkHandlerConfiguration['table']);
83 $localContentObjectRenderer->parameters = $this->contentObjectRenderer->parameters;
84 $link = $localContentObjectRenderer->typoLink($linkText, $typoScriptConfiguration);
85
86 $this->contentObjectRenderer->lastTypoLinkLD = $localContentObjectRenderer->lastTypoLinkLD;
87 $this->contentObjectRenderer->lastTypoLinkUrl = $localContentObjectRenderer->lastTypoLinkUrl;
88 $this->contentObjectRenderer->lastTypoLinkTarget = $localContentObjectRenderer->lastTypoLinkTarget;
89
90 // nasty workaround so typolink stops putting a link together, there is a link already built
91 throw new UnableToLinkException(
92 '',
93 1491130170,
94 null,
95 $link
96 );
97 }
98 }