[!!!][TASK] Remove legacy soft references functionality
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Hook / SoftReferenceHook.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Hook;
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 /**
18 * Class for processing of the FAL soft references on img tags inserted in RTE content
19 */
20 class SoftReferenceHook extends \TYPO3\CMS\Core\Database\SoftReferenceIndex
21 {
22 /**
23 * Main function through which all processing happens
24 *
25 * @param string $table Database table name
26 * @param string $field Field name for which processing occurs
27 * @param int $uid UID of the record
28 * @param string $content The content/value of the field
29 * @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.
30 * @param array $spParams Parameters of the softlink parser. Basically this is the content inside optional []-brackets after the softref keys. Parameters are exploded by ";
31 * @param string $structurePath If running from inside a FlexForm structure, this is the path of the tag.
32 * @return array|bool Result array on positive matches. Otherwise FALSE
33 */
34 public function findRef($table, $field, $uid, $content, $spKey, $spParams, $structurePath = '')
35 {
36 $this->tokenID_basePrefix = $table . ':' . $uid . ':' . $field . ':' . $structurePath . ':' . $spKey;
37 if ($spKey === 'rtehtmlarea_images') {
38 return $this->findRef_rtehtmlarea_images($content, $spParams);
39 } else {
40 return false;
41 }
42 }
43
44 /**
45 * Finding image tags with data-htmlarea-file-uid attribute in the content.
46 * All images that have an data-htmlarea-file-uid attribute will be returned with an info text
47 *
48 * @param string $content The input content to analyse
49 * @param array $spParams Parameters set for the softref parser key in TCA/columns
50 * @return array|bool Result array on positive matches, see description above. Otherwise FALSE
51 */
52 public function findRef_rtehtmlarea_images($content, $spParams)
53 {
54 $retVal = false;
55 // Start HTML parser and split content by image tag
56 $htmlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Html\HtmlParser::class);
57 $imgTags = $htmlParser->splitTags('img', $content);
58 $elements = array();
59 // Traverse splitted parts
60 foreach ($imgTags as $k => $v) {
61 if ($k % 2) {
62 // Get FAL uid reference
63 $attribs = $htmlParser->get_tag_attributes($v);
64 $fileUid = $attribs[0]['data-htmlarea-file-uid'];
65 // If there is a file uid, continue. Otherwise ignore this img tag.
66 if ($fileUid) {
67 // Initialize the element entry with info text here
68 $tokenID = $this->makeTokenID($k);
69 $elements[$k] = array();
70 $elements[$k]['matchString'] = $v;
71 // Token and substitute value
72 $imgTags[$k] = str_replace('data-htmlarea-file-uid="' . $fileUid . '"', 'data-htmlarea-file-uid="{softref:' . $tokenID . '}"', $imgTags[$k]);
73 $elements[$k]['subst'] = array(
74 'type' => 'db',
75 'recordRef' => 'sys_file:' . $fileUid,
76 'tokenID' => $tokenID,
77 'tokenValue' => $fileUid
78 );
79 }
80 }
81 }
82 // Assemble result array
83 if (!empty($elements)) {
84 $retVal = array(
85 'content' => implode('', $imgTags),
86 'elements' => $elements
87 );
88 }
89 return $retVal;
90 }
91 }