Snippets

Heiko Bihlmaier TYPO3 Frontend Editing: Repair links when saving

Created by Heiko Bihlmaier

File PreProcess.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+namespace SCW\Extkey\Hooks;
+
+class PreProcess implements \TYPO3\CMS\FrontendEditing\RequestPreProcess\RequestPreProcessInterface
+{
+    public function preProcess(string $table, array $record, string &$fieldName, string $content, bool &$isFinished): string {
+
+        $content = preg_replace_callback('/<a\s[^>]*href=\"([^\"]*)\"[^>]*>(.*)<\/a>/siU', function($matches) {
+            $wrong = $matches[1];
+
+            if(!preg_match('/^(t3|http|https|tel):\/\//', $wrong)) {
+                if (substr($wrong, strlen($wrong)-1 ) == "/") {
+                    $wrong = substr($wrong, 0, strlen($wrong) - 1);
+                }
+
+                $queryBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable('tx_realurl_pathdata');
+                $page = $queryBuilder
+                    ->select('page_id')
+                    ->from('tx_realurl_pathdata')
+                    ->where(
+                        $queryBuilder->expr()->eq('pagepath', $queryBuilder->createNamedParameter($wrong))
+                    )
+                    ->execute()
+                    ->fetchColumn(0);
+
+                if($page) {
+                    $replace = "t3://page?uid=" . $page;
+                } else {
+                    $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||
+                        $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
+                    $domainName = $_SERVER['HTTP_HOST'];
+                    $replace = $protocol.$domainName . '/' . $wrong . '/';
+                }
+
+                $return = str_replace($matches[1], $replace, $matches[0]);
+
+                return $return;
+            } else {
+                return  $matches[0];
+            }
+        }, $content);
+        $isFinished = true;
+        return $content;
+    }
+}

File ext_localconf.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+// ......
+
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['FrontendEditing']['requestPreProcess'][] = \SCW\Origo\Hooks\PreProcess::class;
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.