[TASK] Add hook for resolving custom link types 30/56730/7
authorDaniel Goerz <ervaude@gmail.com>
Wed, 18 Apr 2018 17:11:42 +0000 (19:11 +0200)
committerTymoteusz Motylewski <t.motylewski@gmail.com>
Tue, 24 Apr 2018 14:08:30 +0000 (16:08 +0200)
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>
typo3/sysext/core/Classes/LinkHandling/LinkService.php
typo3/sysext/core/Documentation/Changelog/8.7.x/Important-79647-AddedHookForResolvingCustomLinkTypes.rst [new file with mode: 0644]

index c89e04b..894bc4c 100644 (file)
@@ -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 @@ class LinkService implements SingletonInterface
             $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 (file)
index 0000000..72bb167
--- /dev/null
@@ -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