[BUGFIX] Support fragment/section for record links
[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 $pageTsConfig = $tsfe->getPagesTSconfig();
33 $configurationKey = $linkDetails['identifier'] . '.';
34 $configuration = $tsfe->tmpl->setup['config.']['recordLinks.'];
35 $linkHandlerConfiguration = $pageTsConfig['TCEMAIN.']['linkHandler.'];
36
37 if (!isset($configuration[$configurationKey], $linkHandlerConfiguration[$configurationKey])) {
38 throw new UnableToLinkException(
39 'Configuration how to link "' . $linkDetails['typoLinkParameter'] . '" was not found, so "' . $linkText . '" was not linked.',
40 1490989149,
41 null,
42 $linkText
43 );
44 }
45 $typoScriptConfiguration = $configuration[$configurationKey]['typolink.'];
46 $linkHandlerConfiguration = $linkHandlerConfiguration[$configurationKey]['configuration.'];
47
48 if ($configuration[$configurationKey]['forceLink']) {
49 $record = $tsfe->sys_page->getRawRecord($linkHandlerConfiguration['table'], $linkDetails['uid']);
50 } else {
51 $record = $tsfe->sys_page->checkRecord($linkHandlerConfiguration['table'], $linkDetails['uid']);
52 }
53 if ($record === 0) {
54 throw new UnableToLinkException(
55 'Record not found for "' . $linkDetails['typoLinkParameter'] . '" was not found, so "' . $linkText . '" was not linked.',
56 1490989659,
57 null,
58 $linkText
59 );
60 }
61
62 // Unset the parameter part of the given TypoScript configuration while keeping
63 // config that has been set in addition.
64 unset($conf['parameter.']);
65 $typoScriptConfiguration = array_replace_recursive($conf, $typoScriptConfiguration);
66
67 if (!empty($linkDetails['fragment'])) {
68 $typoScriptConfiguration['section'] = $linkDetails['fragment'];
69 }
70
71 // Build the full link to the record
72 $localContentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
73 $localContentObjectRenderer->start($record, $linkHandlerConfiguration['table']);
74 $localContentObjectRenderer->parameters = $this->contentObjectRenderer->parameters;
75 $link = $localContentObjectRenderer->typoLink($linkText, $typoScriptConfiguration);
76
77 $this->contentObjectRenderer->lastTypoLinkLD = $localContentObjectRenderer->lastTypoLinkLD;
78 $this->contentObjectRenderer->lastTypoLinkUrl = $localContentObjectRenderer->lastTypoLinkUrl;
79 $this->contentObjectRenderer->lastTypoLinkTarget = $localContentObjectRenderer->lastTypoLinkTarget;
80
81 // nasty workaround so typolink stops putting a link together, there is a link already built
82 throw new UnableToLinkException(
83 '',
84 1491130170,
85 null,
86 $link
87 );
88 }
89 }