[BUGFIX] Use sys_file as soft reference for formDefinitions
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Hooks / SoftReferenceParserHook.php
1 <?php
2 namespace TYPO3\CMS\Form\Hooks;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\SoftReferenceIndex;
18 use TYPO3\CMS\Core\Resource\ResourceFactory;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Register new referenced formDefinitions within a plugin as a soft reference.
23 *
24 * This is used in BE to track how often a specific form is used in a content
25 * element. The number is shown in the form module "Manage forms".
26 *
27 * Scope: backend
28 * @internal
29 */
30 class SoftReferenceParserHook extends SoftReferenceIndex
31 {
32 /**
33 * Main function through which all processing happens
34 *
35 * @param string $table Database table name
36 * @param string $field Field name for which processing occurs
37 * @param int $uid UID of the record
38 * @param string $content The content/value of the field
39 * @param string $spKey The softlink parser key. This is only interesting if more than one parser is grouped in the same class. That is the case with this parser.
40 * @param array $spParams Parameters of the softlink parser. Basically this is the content inside optional []-brackets after the softref keys. Parameters are exploded by ";
41 * @param string $structurePath If running from inside a FlexForm structure, this is the path of the tag.
42 * @return array Result array on positive matches, see description above. Otherwise FALSE
43 */
44 public function findRef($table, $field, $uid, $content, $spKey, $spParams, $structurePath = '')
45 {
46 $this->tokenID_basePrefix = $table . ':' . $uid . ':' . $field . ':' . $structurePath . ':' . $spKey;
47 $tokenId = $this->makeTokenID($content);
48
49 $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
50 $file = $resourceFactory->retrieveFileOrFolderObject($content);
51
52 return [
53 'content' => '{softref:' . $tokenId . '}',
54 'elements' => [
55 $tokenId => [
56 'matchString' => $content,
57 'subst' => [
58 'type' => 'db',
59 'recordRef' => 'sys_file:' . $file->getUid(),
60 'tokenID' => $tokenId,
61 'tokenValue' => $content
62 ],
63 ]
64 ]
65 ];
66 }
67 }