From 5a35b44976f8beca2dbb2c6100461ff21c594432 Mon Sep 17 00:00:00 2001
From: Christian Kuhn <lolli@schwarzbu.ch>
Date: Fri, 1 Mar 2024 15:37:51 +0100
Subject: [PATCH] [TASK] Simplify DH by inlining SlugEnricher

DataHandler related class SlugEnricher is a scary
sounding class name for a simple operation that
sets incoming TCA type=slug fields to empty string
if not provided, to trigger slug calculation later.

The patch inlines that operation into DataHandler,
avoiding the class with three methods, a property,
and a set of array_* function calls, substituting
it with three foreach(), two if() and a helper
method that can be re-used.

Change-Id: I4452d9c50b43832db3cd4b7f538395054a3d67ab
Resolves: #103244
Releases: main
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83176
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../core/Classes/DataHandling/DataHandler.php | 28 +++++++++++--
 .../Classes/DataHandling/SlugEnricher.php     |  6 +++
 .../Deprecation-103244-ClassSlugEnricher.rst  | 41 +++++++++++++++++++
 .../ExtensionScanner/Php/ClassNameMatcher.php |  5 +++
 4 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/13.1/Deprecation-103244-ClassSlugEnricher.rst

diff --git a/typo3/sysext/core/Classes/DataHandling/DataHandler.php b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
index 7c9b638b048d..f6f733e0f41b 100644
--- a/typo3/sysext/core/Classes/DataHandling/DataHandler.php
+++ b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
@@ -729,8 +729,15 @@ class DataHandler implements LoggerAwareInterface
             }
             $hookObjectsArr[] = $hookObject;
         }
-        // Pre-process data-map and synchronize localization states
-        $this->datamap = GeneralUtility::makeInstance(SlugEnricher::class)->enrichDataMap($this->datamap);
+
+        foreach ($this->datamap as $tableName => $tableDataMap) {
+            foreach ($tableDataMap as $identifier => $fieldValues) {
+                if (!MathUtility::canBeInterpretedAsInteger($identifier)) {
+                    $this->datamap[$tableName][$identifier] = $this->initializeSlugFieldsToEmptyString($tableName, $fieldValues);
+                }
+            }
+        }
+
         $this->datamap = DataMapProcessor::instance($this->datamap, $this->BE_USER, $this->referenceIndexUpdater)->process();
         // Organize tables so that the pages-table is always processed first. This is required if you want to make sure that content pointing to a new page will be created.
         $orderOfTables = [];
@@ -1085,6 +1092,22 @@ class DataHandler implements LoggerAwareInterface
         }
     }
 
+    /**
+     *  New records capable of handling slugs (TCA type 'slug'), always
+     *  require the field value to be set, in order to run through the validation
+     *  process to create a new slug. Fields having `null` as value are ignored
+     *  and can be used to by-pass implicit slug initialization.
+     */
+    protected function initializeSlugFieldsToEmptyString(string $tableName, array $fieldValues): array
+    {
+        foreach (($GLOBALS['TCA'][$tableName]['columns'] ?? []) as $columnName => $columnConfig) {
+            if (($columnConfig['config']['type'] ?? '') === 'slug' && !isset($fieldValues[$columnName])) {
+                $fieldValues[$columnName] = '';
+            }
+        }
+        return $fieldValues;
+    }
+
     /**
      * Sets the "sorting" DB field and the "pid" field of an incoming record that should be added (NEW1234)
      * depending on the record that should be added or where it should be added.
@@ -1853,7 +1876,6 @@ class DataHandler implements LoggerAwareInterface
      * @param string $field Field name
      * @param array $incomingFieldArray the fields being explicitly set by the outside (unlike $fieldArray) for the record
      * @return array $res The result array. The processed value (if any!) is set in the "value" key.
-     * @see SlugEnricher
      * @see SlugHelper
      */
     protected function checkValueForSlug(string $value, array $tcaFieldConf, string $table, $id, int $realPid, string $field, array $incomingFieldArray = []): array
diff --git a/typo3/sysext/core/Classes/DataHandling/SlugEnricher.php b/typo3/sysext/core/Classes/DataHandling/SlugEnricher.php
index 61163073ca15..52fce3260d79 100644
--- a/typo3/sysext/core/Classes/DataHandling/SlugEnricher.php
+++ b/typo3/sysext/core/Classes/DataHandling/SlugEnricher.php
@@ -27,6 +27,7 @@ use TYPO3\CMS\Core\Utility\MathUtility;
  *
  * @see DataHandler::fillInFieldArray()
  * @see DataHandler::checkValueForSlug()
+ * @deprecated since TYPO3 v13, will be removed in TYPO3 v14.0.
  */
 class SlugEnricher
 {
@@ -35,6 +36,11 @@ class SlugEnricher
      */
     protected $slugFieldNamesPerTable = [];
 
+    public function __construct()
+    {
+        trigger_error('Class ' . __CLASS__ . ' will be removed with TYPO3 v14.0.', E_USER_DEPRECATED);
+    }
+
     public function enrichDataMap(array $dataMap): array
     {
         foreach ($dataMap as $tableName => &$tableDataMap) {
diff --git a/typo3/sysext/core/Documentation/Changelog/13.1/Deprecation-103244-ClassSlugEnricher.rst b/typo3/sysext/core/Documentation/Changelog/13.1/Deprecation-103244-ClassSlugEnricher.rst
new file mode 100644
index 000000000000..f381dbe14ba9
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/13.1/Deprecation-103244-ClassSlugEnricher.rst
@@ -0,0 +1,41 @@
+.. include:: /Includes.rst.txt
+
+.. _deprecation-103244-1709376790:
+
+=========================================
+Deprecation: #103244 - Class SlugEnricher
+=========================================
+
+See :issue:`103244`
+
+Description
+===========
+
+Class :php:`\TYPO3\CMS\Core\DataHandling\SlugEnricher` has been marked as
+deprecated in TYPO3 v13 and will be removed with v14.
+
+The class was used as a helper for :php:`\TYPO3\CMS\Core\DataHandling\DataHandler`,
+which now inlines the code in a simplified variant.
+
+
+Impact
+======
+
+Using the class will raise a deprecation level log entry and a fatal error in TYPO3 v14.
+
+
+Affected installations
+======================
+
+There is little to no reason to use this class in custom extensions, very few
+instances should be affected by this. The extension scanner will find usages
+with a strong match.
+
+
+Migration
+=========
+
+No migration available.
+
+
+.. index:: PHP-API, FullyScanned, ext:core
diff --git a/typo3/sysext/install/Configuration/ExtensionScanner/Php/ClassNameMatcher.php b/typo3/sysext/install/Configuration/ExtensionScanner/Php/ClassNameMatcher.php
index f1ed6e2bd93e..ec186f6d3d6a 100644
--- a/typo3/sysext/install/Configuration/ExtensionScanner/Php/ClassNameMatcher.php
+++ b/typo3/sysext/install/Configuration/ExtensionScanner/Php/ClassNameMatcher.php
@@ -2517,4 +2517,9 @@ return [
             'Deprecation-102763-ExtbaseHashService.rst',
         ],
     ],
+    'TYPO3\CMS\Core\DataHandling\SlugEnricher' => [
+        'restFiles' => [
+            'Deprecation-103244-ClassSlugEnricher.rst',
+        ],
+    ],
 ];
-- 
GitLab