[FEATURE] Allow extended custom links in FormEngine and TypoLink
[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
21 /**
22 * Builds a TypoLink to a database record
23 */
24 class DatabaseRecordLinkBuilder extends AbstractTypolinkBuilder
25 {
26 /**
27 * @inheritdoc
28 */
29 public function build(array &$linkDetails, string $linkText, string $target, array $conf): array
30 {
31 $tsfe = $this->getTypoScriptFrontendController();
32 $configurationKey = $linkDetails['identifier'] . '.';
33 $configuration = $tsfe->tmpl->setup['config.']['recordLinks.'];
34 $linkHandlerConfiguration = $tsfe->pagesTSconfig['TCEMAIN.']['linkHandler.'];
35
36 if (!isset($configuration[$configurationKey]) || !isset($linkHandlerConfiguration[$configurationKey])) {
37 throw new UnableToLinkException(
38 'Configuration how to link "' . $linkDetails['typoLinkParameter'] . '" was not found, so "' . $linkText . '" was not linked.',
39 1490989149,
40 null,
41 $linkText
42 );
43 }
44 $typoScriptConfiguration = $configuration[$configurationKey]['typolink.'];
45 $linkHandlerConfiguration = $linkHandlerConfiguration[$configurationKey]['configuration.'];
46
47 if ($configuration[$configurationKey]['forceLink']) {
48 $record = $tsfe->sys_page->getRawRecord($linkHandlerConfiguration['table'], $linkDetails['uid']);
49 } else {
50 $record = $tsfe->sys_page->checkRecord($linkHandlerConfiguration['table'], $linkDetails['uid']);
51 }
52 if ($record === 0) {
53 throw new UnableToLinkException(
54 'Record not found for "' . $linkDetails['typoLinkParameter'] . '" was not found, so "' . $linkText . '" was not linked.',
55 1490989659,
56 null,
57 $linkText
58 );
59 }
60
61 // Build the full link to the record
62 $localContentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
63 $localContentObjectRenderer->start($record, $linkHandlerConfiguration['table']);
64 $localContentObjectRenderer->parameters = $this->contentObjectRenderer->parameters;
65 $link = $localContentObjectRenderer->typoLink($linkText, $typoScriptConfiguration);
66
67 $this->contentObjectRenderer->lastTypoLinkLD = $localContentObjectRenderer->lastTypoLinkLD;
68 $this->contentObjectRenderer->lastTypoLinkUrl = $localContentObjectRenderer->lastTypoLinkUrl;
69 $this->contentObjectRenderer->lastTypoLinkTarget = $localContentObjectRenderer->lastTypoLinkTarget;
70
71 // nasty workaround so typolink stops putting a link together, there is a link already built
72 throw new UnableToLinkException(
73 '', 1491130170, null, $link
74 );
75 }
76 }