Frontend link factory
The
\TYPO3\ class is the main entry point
for generating links in the TYPO3 frontend from PHP. It creates any kind of link:
to a page, a file, a folder, an external URL, an email address, a telephone
number or a database record, such as a news entry.
This functionality previously resided in
\TYPO3\ and
\TYPO3\.
It has been extracted into a dedicated class that only deals with generating
links.
Table of contents
Note
For rendering links in Fluid templates or TypoScript, the
established path is still recommended: the ViewHelper
Link.typolink ViewHelper <f:link.typolink> or the TypoScript function
typolink. Use
Link when an extension needs
the raw result programmatically, with access to more than just the anchor
tag.
Methods of the LinkFactory
Link provides two public
methods, both returning a
\TYPO3\:
- class LinkFactory
-
- Fully qualified name
-
\TYPO3\
CMS\ Frontend\ Typolink\ Link Factory
Creates links in the TYPO3 frontend from a TypoLink configuration.
- create ( string $linkText, array $linkConfiguration, ContentObjectRenderer $contentObjectRenderer)
-
Creates a link from a link text and a TypoLink configuration array. The
$linkuses the same keys as a TypoScript typolink, most importantlyConfiguration parameter, and optionallytarget,class,titleandadditional. Throws anParams \TYPO3\if the link cannot be built.CMS\ Frontend\ Typolink\ Unable To Link Exception - param $linkText
-
the text to be used as the link text
- param $linkConfiguration
-
the TypoLink configuration array
- param $contentObjectRenderer
-
the current content object renderer
- Returns
-
TYPO3CMSFrontendTypolink Link Result Interface
- createUri ( string $urlParameter, ?ContentObjectRenderer $contentObjectRenderer = null)
-
Creates a link result for a single TypoLink parameter string, for example
't3://. Convenient when only the URL is needed. Thepage?uid=42 _ blank css- class "My title"' Contentis optional here, so this method can also be used outside a typical content rendering context, including in the TYPO3 backend.Object Renderer - param $urlParameter
-
the TypoLink parameter string
- param $contentObjectRenderer
-
the current content object renderer, optional
- Returns
-
TYPO3CMSFrontendTypolink Link Result Interface
Obtaining a LinkFactory instance
Inject
Link through
dependency injection and call it from your own
service:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Service;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Typolink\LinkFactory;
use TYPO3\CMS\Frontend\Typolink\LinkResultInterface;
use TYPO3\CMS\Frontend\Typolink\UnableToLinkException;
readonly class MyLinkService
{
public function __construct(
protected LinkFactory $linkFactory,
) {}
/**
* Build a link to a page and return the ready-to-use URL.
*/
public function pageUrl(int $pageUid, ContentObjectRenderer $contentObjectRenderer): string
{
try {
$linkResult = $this->linkFactory->create(
'Read more',
['parameter' => 't3://page?uid=' . $pageUid],
$contentObjectRenderer,
);
} catch (UnableToLinkException) {
return '';
}
return $linkResult->getUrl();
}
/**
* Return the full anchor tag instead of just the URL.
*/
public function pageLink(int $pageUid, ContentObjectRenderer $contentObjectRenderer): LinkResultInterface
{
return $this->linkFactory->create(
'Read more',
[
'parameter' => 't3://page?uid=' . $pageUid,
'target' => '_blank',
'title' => 'Opens in a new window',
],
$contentObjectRenderer,
);
}
}
Working with the LinkResult
Both methods return a
\TYPO3\. It gives programmatic
access to the individual parts of the generated link, rather than only the
rendered anchor tag: the resolved URL, the link type, the link text, the link
target and the HTML attributes. Immutable
with* methods each return a
modified copy of the result. The concrete
\TYPO3\ additionally renders the result
as a complete anchor tag or as JSON, which is useful for headless or API output.
See the API documentation of LinkResultInterface and LinkResult for the complete list of available methods.
$linkResult = $this->linkFactory->createUri('t3://page?uid=42');
$url = $linkResult->getUrl(); // "/the/page/path"
$html = $linkResult->getHtml(); // '<a href="/the/page/path">...</a>'
$json = $linkResult->getJson(); // '{"href":"/the/page/path", ...}'
Examples per link type
The link type is determined by the
parameter
value. The following
examples show the configuration for each type. They all use
create; the same
parameter
values work with
create as the first part of the parameter string.
Link to a page
$linkResult = $this->linkFactory->create(
'Read more',
['parameter' => 't3://page?uid=42'],
$contentObjectRenderer,
);
Link to a file
$linkResult = $this->linkFactory->create(
'Download the file',
['parameter' => 't3://file?uid=17'],
$contentObjectRenderer,
);
Link to an email address
$linkResult = $this->linkFactory->create(
'Write us',
['parameter' => 'mailto:info@example.org'],
$contentObjectRenderer,
);
Link to a telephone number
$linkResult = $this->linkFactory->create(
'Call us',
['parameter' => 'tel:+1234567890'],
$contentObjectRenderer,
);
Link to a record
$linkResult = $this->linkFactory->create(
'Read the article',
['parameter' => 't3://record?identifier=tx_news&uid=1'],
$contentObjectRenderer,
);
How the link is built by the link builders
Once
Link has determined the
link type, the actual link is built by the matching
link builder, and the
AfterLinkIsGeneratedEvent is dispatched so
the result can still be modified. To learn how the individual link types are
resolved, continue with the frontend link builder.