Skip to content

Commit

Permalink
[TASK] Add hook for resolving custom link types
Browse files Browse the repository at this point in the history
The extendability of the link wizard is incomplete since the
refactoring of it. This patch adds a missing hook to resolve custom
link syntax in the LinkService. It is now possible (again) to
resolve links that start with <identifier>:

Resolves: #79647
Releases: master, 8.7
Change-Id: Iaf0c18f72aa799ab44a8bd14bddd0ca6c3530198
Reviewed-on: https://review.typo3.org/56730
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Riny van Tiggelen <info@online-gamer.nl>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
Tested-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
  • Loading branch information
Daniel Goerz authored and tmotyl committed Apr 24, 2018
1 parent a4c2d98 commit 1f54e70
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
14 changes: 11 additions & 3 deletions typo3/sysext/core/Classes/LinkHandling/LinkService.php
Expand Up @@ -34,8 +34,6 @@ class LinkService implements SingletonInterface
const TYPE_RECORD = 'record';
const TYPE_UNKNOWN = 'unknown';

// @TODO There needs to be an API to make these types extensible as the former 'typolinkLinkHandler' does not work anymore! forge #79647

/**
* All registered LinkHandlers
*
Expand Down Expand Up @@ -123,7 +121,17 @@ public function resolveByStringRepresentation(string $urn): array
$result = $this->handlers[self::TYPE_EMAIL]->resolveHandlerData(['email' => $urn]);
$result['type'] = self::TYPE_EMAIL;
} else {
throw new Exception\UnknownUrnException('No valid URN to resolve found', 1457177667);
$result = [];
if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Link']['resolveByStringRepresentation'] ?? null)) {
$params = ['urn' => $urn, 'result' => &$result];
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Link']['resolveByStringRepresentation'] as $hookMethod) {
$fakeThis = false;
GeneralUtility::callUserFunction($hookMethod, $params, $fakeThis);
}
}
if (empty($result) || empty($result['type'])) {
throw new Exception\UnknownUrnException('No valid URN to resolve found', 1457177667);
}
}

return $result;
Expand Down
@@ -0,0 +1,49 @@
.. include:: ../../Includes.txt

==============================================================
Important: #79647 - Added Hook for resolving custom link types
==============================================================

See :issue:`79647`

Description
===========

A newly introduced hook in :php:`LinkService->resolveByStringRepresentation` allows to resolve custom link types with
special syntax. A reference to the empty :php:`$result` array is passed as well as the :php:`$urn` string that could not be
resolved by the core.

Example
=======

An example implementation for custom links that use `myLinkIdentifier:` as a prefix could look like this:

:file:`EXT:my_site/ext_localconf.php`

.. code-block:: php
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Link']['resolveByStringRepresentation'][] =
\MyVendor\MySite\Hooks\LinkServiceHook::class . '->resolveByStringRepresentation';
:file:`EXT:my_site/Classes/Hooks/LinkServiceHook.php`

.. code-block:: php
namespace MyVendor\MySite\Hooks;
class LinkServiceHook
{
public function resolveByStringRepresentation(array $parameters): void
{
// Only care for links that start with myLinkIdentifier:
if (stripos($parameters['urn'], 'myLinkIdentifier:') !== 0) {
return;
}
$parameters['result'] = ['myLinkIdentifier' => substr($parameters['urn'], 17)]
$parameters['result']['type'] = 'myLinkIdentifier';
}
}
.. index:: Backend, PHP-API

0 comments on commit 1f54e70

Please sign in to comment.