Feature: #106405 - TypolinkBuilderInterface
See forge#106405
Description
A new interface
\TYPO3\
has been introduced to provide a more flexible way to generate links in TYPO3.
The interface defines a
build method that replaces the previous
build method approach used when extending from
Abstract.
All Core Typolink implementations now implement this interface and
use dependency injection for improved service composition and testability.
The interface method signature is as follows:
use TYPO3\CMS\Frontend\Typolink;
public function buildLink(
array $linkDetails,
array $configuration,
ServerRequestInterface $request,
string $linkText = ''
): LinkResultInterface;
Impact
- All implementations of
Typolinkare automatically configured as public services in the dependency injection container, removing the need for manual service configuration.Builder Interface Typolinkclasses can now use proper dependency injection through their constructors, improving testability and aligning with TYPO3's architectural best practices.Builder - The
\Serveris now passed directly, providing access to the request context without relying on global state.Request Interface - The new interface introduces a cleaner separation of concerns and more explicit parameter passing.
Example usage
Creating a custom Typolink using the new interface:
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Frontend\Typolink\LinkResult;
use TYPO3\CMS\Frontend\Typolink\LinkResultInterface;
use TYPO3\CMS\Frontend\Typolink\TypolinkBuilderInterface;
final readonly class MyCustomLinkBuilder implements TypolinkBuilderInterface
{
public function __construct(
private MyCustomService $customService,
private AnotherService $anotherService,
) {}
public function buildLink(
array $linkDetails,
array $configuration,
ServerRequestInterface $request,
string $linkText = ''
): LinkResultInterface {
// Access ContentObjectRenderer from the request
$contentObjectRenderer = $request->getAttribute('currentContentObject');
// Use injected services
$processedData = $this->customService->process($linkDetails);
// Build and return link result
return new LinkResult($processedData['url'], $linkText);
}
}
Registering the TypolinkBuilder class is still necessary via
$GLOBALS.