Skip to content

Commit 47f9bbf

Browse files
committed
Refactor link generation. Replace getTypoLink_URL with linkFactory.
1 parent 8424c22 commit 47f9bbf

File tree

5 files changed

+67
-53
lines changed

5 files changed

+67
-53
lines changed

Classes/Serializer/Handler/FileReferenceHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ public function serialize(
111111
$fileRenderer->render($originalFile, 1, 1),
112112
$match
113113
)) {
114-
$out['urlEmbed'] = UrlService::forceAbsoluteUrl($match[1], $context->getAttribute('TYPO3_SITE_URL'));
114+
if ($match[1] === '') {
115+
$out['urlEmbed'] = '';
116+
} else {
117+
$out['urlEmbed'] = UrlService::forceAbsoluteUrl(
118+
$match[1],
119+
$context->getAttribute('TYPO3_SITE_URL')
120+
);
121+
}
115122
}
116123
}
117124

Classes/Serializer/Handler/RecordUriHandler.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
use JMS\Serializer\SerializationContext;
99
use JMS\Serializer\Visitor\SerializationVisitorInterface;
1010
use SourceBroker\T3api\Service\UrlService;
11-
use TYPO3\CMS\Core\Utility\GeneralUtility;
11+
use TYPO3\CMS\Core\LinkHandling\LinkService;
1212
use TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject;
1313
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
14+
use TYPO3\CMS\Frontend\Typolink\LinkFactory;
15+
use TYPO3\CMS\Frontend\Typolink\UnableToLinkException;
1416

1517
/**
1618
* Class RecordUriHandler
@@ -24,6 +26,12 @@ class RecordUriHandler extends AbstractHandler implements SerializeHandlerInterf
2426
*/
2527
protected static $supportedTypes = [self::TYPE];
2628

29+
public function __construct(
30+
public readonly LinkFactory $linkFactory,
31+
public readonly LinkService $linkService,
32+
public readonly ContentObjectRenderer $contentObjectRenderer
33+
) {}
34+
2735
/**
2836
* @param SerializationVisitorInterface $visitor
2937
* @param $value
@@ -48,27 +56,25 @@ public function serialize(
4856
);
4957
}
5058

51-
return UrlService::forceAbsoluteUrl(
52-
$this->getContentObjectRenderer()->getTypoLink_URL(sprintf(
53-
't3://record?identifier=%s&uid=%s',
54-
$type['params'][0],
55-
$entity->getUid()
56-
)),
57-
$context->getAttribute('TYPO3_SITE_URL')
58-
);
59-
}
60-
61-
/**
62-
* @return ContentObjectRenderer
63-
*/
64-
protected function getContentObjectRenderer(): ContentObjectRenderer
65-
{
66-
static $contentObjectRenderer;
59+
try {
60+
$url = $this->linkFactory->createUri(
61+
sprintf('t3://record?identifier=%s&uid=%s', $type['params'][0], $entity->getUid()),
62+
$this->contentObjectRenderer
63+
)->getUrl();
64+
} catch (UnableToLinkException $e) {
65+
trigger_error(
66+
$e->getMessage(),
67+
E_USER_WARNING
68+
);
69+
}
6770

68-
if (!$contentObjectRenderer instanceof ContentObjectRenderer) {
69-
$contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
71+
if (empty($url)) {
72+
return '';
7073
}
7174

72-
return $contentObjectRenderer;
75+
return UrlService::forceAbsoluteUrl(
76+
$url,
77+
$context->getAttribute('TYPO3_SITE_URL')
78+
);
7379
}
7480
}

Classes/Serializer/Handler/TypolinkHandler.php

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,48 @@
77
use JMS\Serializer\SerializationContext;
88
use JMS\Serializer\Visitor\SerializationVisitorInterface;
99
use SourceBroker\T3api\Service\UrlService;
10-
use TYPO3\CMS\Core\Utility\GeneralUtility;
10+
use TYPO3\CMS\Core\LinkHandling\LinkService;
1111
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
12+
use TYPO3\CMS\Frontend\Typolink\LinkFactory;
13+
use TYPO3\CMS\Frontend\Typolink\UnableToLinkException;
1214

13-
/**
14-
* Class TypolinkHandler
15-
*/
1615
class TypolinkHandler extends AbstractHandler implements SerializeHandlerInterface
1716
{
1817
public const TYPE = 'Typolink';
1918

19+
public function __construct(
20+
public readonly LinkFactory $linkFactory,
21+
public readonly LinkService $linkService,
22+
public readonly ContentObjectRenderer $contentObjectRenderer
23+
) {}
24+
2025
/**
2126
* @var string[]
2227
*/
2328
protected static $supportedTypes = [self::TYPE];
2429

25-
/**
26-
* @param SerializationVisitorInterface $visitor
27-
* @param string|int|array $typolinkParameter
28-
* @param array $type
29-
* @param SerializationContext $context
30-
*
31-
* @return array|string
32-
*/
3330
public function serialize(
3431
SerializationVisitorInterface $visitor,
35-
$typolinkParameter,
32+
mixed $typolinkParameter,
3633
array $type,
3734
SerializationContext $context
38-
) {
39-
if (empty($typolinkParameter)) {
40-
return '';
35+
): string {
36+
try {
37+
$url = $this->linkFactory->createUri(
38+
$typolinkParameter,
39+
$this->contentObjectRenderer
40+
)->getUrl();
41+
} catch (UnableToLinkException $e) {
42+
trigger_error(
43+
$e->getMessage(),
44+
E_USER_WARNING
45+
);
4146
}
42-
return UrlService::forceAbsoluteUrl(
43-
$this->getContentObjectRenderer()->getTypoLink_URL(...(array)$typolinkParameter),
44-
$context->getAttribute('TYPO3_SITE_URL')
45-
);
46-
}
4747

48-
/**
49-
* @return ContentObjectRenderer
50-
*/
51-
protected function getContentObjectRenderer(): ContentObjectRenderer
52-
{
53-
static $contentObjectRenderer;
54-
55-
if (!$contentObjectRenderer instanceof ContentObjectRenderer) {
56-
$contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
48+
if (empty($url)) {
49+
return '';
5750
}
5851

59-
return $contentObjectRenderer;
52+
return UrlService::forceAbsoluteUrl($url, $context->getAttribute('TYPO3_SITE_URL'));
6053
}
6154
}

Classes/Service/UrlService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class UrlService
88
{
99
public static function forceAbsoluteUrl(string $url, string $host): string
1010
{
11-
if (strpos($url, '//') !== 0 && parse_url($url, PHP_URL_SCHEME) === null) {
11+
if (!str_starts_with($url, '//') && parse_url($url, PHP_URL_SCHEME) === null) {
1212
return rtrim($host, '/') . '/' . ltrim($url, '/');
1313
}
1414

Configuration/Services.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,17 @@ services:
8282
public: true
8383

8484
SourceBroker\T3api\Serializer\Handler\RecordUriHandler:
85+
arguments:
86+
$linkFactory: '@TYPO3\CMS\Frontend\Typolink\LinkFactory'
87+
$linkService: '@TYPO3\CMS\Core\LinkHandling\LinkService'
88+
$contentObjectRenderer: '@TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer'
8589
public: true
8690

8791
SourceBroker\T3api\Serializer\Handler\TypolinkHandler:
92+
arguments:
93+
$linkFactory: '@TYPO3\CMS\Frontend\Typolink\LinkFactory'
94+
$linkService: '@TYPO3\CMS\Core\LinkHandling\LinkService'
95+
$contentObjectRenderer: '@TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer'
8896
public: true
8997

9098
SourceBroker\T3api\Serializer\Handler\CurrentFeUserHandler:

0 commit comments

Comments
 (0)