Implementing a custom LinkHandler
It is possible to implement a custom LinkHandler if links are to be created and handled that cannot be handled by any of the Core LinkHandlers.
The example below is part of the TYPO3 Documentation Team extension examples.
Implementing the LinkHandler
You can have a look at the existing LinkHandler in the system extension
"backend", found at typo3/
.
Changed in version 12.0
Due to the integration of EXT:recordlist into EXT:backend the path has
changed from
typo3/
to
typo3/
.
For TYPO3 v12 the moved classes are available as an alias under the old
namespace to allow extensions to be compatible with TYPO3 v11 and v12.
However please note that all these extensions extend the
\TYPO3\
,
which is marked as @internal
and subject to change without further notice.
You should therefore implement the interface
\TYPO3\
in your custom
LinkHandlers:
<?php
namespace T3docs\Examples\LinkHandler;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Backend\Controller\AbstractLinkBrowserController;
use TYPO3\CMS\Backend\LinkHandler\LinkHandlerInterface;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\View\ViewFactoryData;
use TYPO3\CMS\Core\View\ViewFactoryInterface;
#[Autoconfigure(public: true)]
final class GitHubLinkHandler implements LinkHandlerInterface
{
protected $linkAttributes = ['target', 'title', 'class', 'params', 'rel'];
protected $configuration;
private array $linkParts;
public function __construct(
private readonly PageRenderer $pageRenderer,
private readonly ViewFactoryInterface $viewFactory,
) {}
/**
* Initialize the handler
*
* @param AbstractLinkBrowserController $linkBrowser
* @param string $identifier
* @param array $configuration Page TSconfig
*/
public function initialize(AbstractLinkBrowserController $linkBrowser, $identifier, array $configuration)
{
$this->configuration = $configuration;
}
/**
* Checks if this is the handler for the given link
*
* Also stores information locally about currently linked issue
*
* @param array $linkParts Link parts as returned from TypoLinkCodecService
*
* @return bool
*/
public function canHandleLink(array $linkParts)
{
if (isset($linkParts['url']['github'])) {
$this->linkParts = $linkParts;
return true;
}
return false;
}
/**
* Format the current link for HTML output
*
* @return string
*/
public function formatCurrentUrl(): string
{
return $this->linkParts['url']['github'];
}
/**
* Render the link handler
*/
public function render(ServerRequestInterface $request): string
{
$this->pageRenderer->loadJavaScriptModule('@vendor/my-extension/GitHubLinkHandler.js');
$viewFactoryData = new ViewFactoryData(
templateRootPaths: ['EXT:myExt/Resources/Private/Templates/LinkBrowser'],
partialRootPaths: ['EXT:myExt/Resources/Private/Partials/LinkBrowser'],
layoutRootPaths: ['EXT:myExt/Resources/Private/Layouts/LinkBrowser'],
request: $request,
);
$view = $this->viewFactory->create($viewFactoryData);
$view->assign('project', $this->configuration['project']);
$view->assign('action', $this->configuration['action']);
$view->assign('github', !empty($this->linkParts) ? $this->linkParts['url']['github'] : '');
return $view->render('GitHub');
}
/**
* @return string[] Array of body-tag attributes
*/
public function getBodyTagAttributes(): array
{
return [];
}
/**
* @return array
*/
public function getLinkAttributes()
{
return $this->linkAttributes;
}
/**
* @param string[] $fieldDefinitions Array of link attribute field definitions
* @return string[]
*/
public function modifyLinkAttributes(array $fieldDefinitions)
{
return $fieldDefinitions;
}
/**
* We don't support updates since there is no difference to simply set the link again.
*
* @return bool
*/
public function isUpdateSupported()
{
return false;
}
}
Changed in version 14.0
Use the Using the generic view factory (ViewFactoryInterface) to create a view, previously
used \TYPO3\
was deprecated with TYPO3
v13.3 and removed with v14.0.
The LinkHandler then has to be registered via page TSconfig:
TCEMAIN.linkHandler {
github {
handler = T3docs\\Examples\\LinkHandler\\GitHubLinkHandler
label = LLL:EXT:examples/Resources/Private/Language/locallang_browse_links.xlf:github
displayAfter = url
scanBefore = url
configuration {
project = TYPO3-Documentation/TYPO3CMS-Reference-CoreApi
action = issues
}
}
}
And the JavaScript, depending on RequireJS (Removed), has to be added in a file
Resources/
:
/**
* Module: TYPO3/CMS/Examples/GitHubLinkHandler
* GitHub issue link interaction
*/
define(['jquery', 'TYPO3/CMS/Recordlist/LinkBrowser'], function($, LinkBrowser) {
'use strict';
/**
*
* @type {{}}
* @exports T3docs/Examples/GitHubLinkHandler
*/
var GitHubLinkHandler = {};
$(function() {
$('#lgithubform').on('submit', function(event) {
event.preventDefault();
var value = $(this).find('[name="lgithub"]').val();
if (value === 'github:') {
return;
}
if (value.indexOf('github:') === 0) {
value = value.substr(7);
}
LinkBrowser.finalizeFunction('github:' + value);
});
});
return GitHubLinkHandler;
});
This would create a link looking like this:
<a href="github:123">Example Link</a>
Which could, for example, be interpreted by a custom protocol handler on a company computer's operating system.