From 1f54e70484901d3799e01e1c166c2df23934ad8e Mon Sep 17 00:00:00 2001 From: Daniel Goerz Date: Wed, 18 Apr 2018 19:11:42 +0200 Subject: [PATCH] [TASK] Add hook for resolving custom link types 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 : Resolves: #79647 Releases: master, 8.7 Change-Id: Iaf0c18f72aa799ab44a8bd14bddd0ca6c3530198 Reviewed-on: https://review.typo3.org/56730 Reviewed-by: Markus Klein Tested-by: Markus Klein Tested-by: TYPO3com Reviewed-by: Frans Saris Reviewed-by: Riny van Tiggelen Reviewed-by: Joerg Boesche Reviewed-by: Tymoteusz Motylewski Tested-by: Tymoteusz Motylewski --- .../core/Classes/LinkHandling/LinkService.php | 14 ++++-- ...7-AddedHookForResolvingCustomLinkTypes.rst | 49 +++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 typo3/sysext/core/Documentation/Changelog/8.7.x/Important-79647-AddedHookForResolvingCustomLinkTypes.rst diff --git a/typo3/sysext/core/Classes/LinkHandling/LinkService.php b/typo3/sysext/core/Classes/LinkHandling/LinkService.php index c89e04bb2745..894bc4cfef8a 100644 --- a/typo3/sysext/core/Classes/LinkHandling/LinkService.php +++ b/typo3/sysext/core/Classes/LinkHandling/LinkService.php @@ -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 * @@ -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; diff --git a/typo3/sysext/core/Documentation/Changelog/8.7.x/Important-79647-AddedHookForResolvingCustomLinkTypes.rst b/typo3/sysext/core/Documentation/Changelog/8.7.x/Important-79647-AddedHookForResolvingCustomLinkTypes.rst new file mode 100644 index 000000000000..72bb167815c5 --- /dev/null +++ b/typo3/sysext/core/Documentation/Changelog/8.7.x/Important-79647-AddedHookForResolvingCustomLinkTypes.rst @@ -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