From 1643c4573cd3e6b39a7a5017d8618854b6fcbbc2 Mon Sep 17 00:00:00 2001
From: Christian Kuhn <lolli@schwarzbu.ch>
Date: Thu, 25 Nov 2021 00:17:11 +0100
Subject: [PATCH] [TASK] Avoid a useless trait in FormEngine

ext:backend Form/Element recently got a trait with #95954
that can be easily avoided by adding the method to
AbstractFormElement, which all elements should extend.

Resolves: #96071
Related: #95954
Releases: master, 11.5
Change-Id: I8dc73f17b976f8191bf39bae5b51f2609778797c
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72281
Tested-by: core-ci <typo3@b13.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Jochen <rothjochen@gmail.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Jochen <rothjochen@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../Form/Element/AbstractFormElement.php      | 38 ++++++++++++++
 .../Form/Element/CustomEvaluationTrait.php    | 52 -------------------
 .../Form/Element/InputColorPickerElement.php  |  2 -
 .../Classes/Form/Element/InputLinkElement.php |  1 -
 .../Classes/Form/Element/InputTextElement.php |  1 -
 5 files changed, 38 insertions(+), 56 deletions(-)
 delete mode 100644 typo3/sysext/backend/Classes/Form/Element/CustomEvaluationTrait.php

diff --git a/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php b/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php
index b6ba4182f8ba..649fd7f1f52a 100644
--- a/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php
+++ b/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php
@@ -23,6 +23,7 @@ use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Localization\LanguageService;
+use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
 use TYPO3\CMS\Core\Utility\ArrayUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
@@ -313,6 +314,43 @@ abstract class AbstractFormElement extends AbstractNode
         return ceil($size * $compensationForFormFields);
     }
 
+    /**
+     * Handle custom javascript `eval` implementations. $evalObject is a hook object
+     * for custom eval's. It is transferred to JS as a requireJsModule if possible.
+     * This is used by a couple of renderType's like various type="input", should
+     * be used with care and is internal for now.
+     *
+     * @param array $resultArray
+     * @param string $name
+     * @param object|null $evalObject
+     * @return array
+     * @internal
+     */
+    protected function resolveJavaScriptEvaluation(array $resultArray, string $name, ?object $evalObject): array
+    {
+        if (!is_object($evalObject) || !method_exists($evalObject, 'returnFieldJS')) {
+            return $resultArray;
+        }
+
+        $javaScriptEvaluation = $evalObject->returnFieldJS();
+        if ($javaScriptEvaluation instanceof JavaScriptModuleInstruction) {
+            // just use the module name and export-name
+            $resultArray['requireJsModules'][] = JavaScriptModuleInstruction::forRequireJS(
+                $javaScriptEvaluation->getName(),
+                $javaScriptEvaluation->getExportName()
+            )->invoke('registerCustomEvaluation', $name);
+        } else {
+            // @todo deprecate inline JavaScript in TYPO3 v12.0
+            $resultArray['additionalJavaScriptPost'][] = sprintf(
+                'TBE_EDITOR.customEvalFunctions[%s] = function(value) { %s };',
+                GeneralUtility::quoteJSvalue($name),
+                $javaScriptEvaluation
+            );
+        }
+
+        return $resultArray;
+    }
+
     /***********************************************
      * CheckboxElement related methods
      ***********************************************/
diff --git a/typo3/sysext/backend/Classes/Form/Element/CustomEvaluationTrait.php b/typo3/sysext/backend/Classes/Form/Element/CustomEvaluationTrait.php
deleted file mode 100644
index eccf56ef5a84..000000000000
--- a/typo3/sysext/backend/Classes/Form/Element/CustomEvaluationTrait.php
+++ /dev/null
@@ -1,52 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-
-namespace TYPO3\CMS\Backend\Form\Element;
-
-use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-
-/**
- * Trait handling custom `eval` implementations.
- */
-trait CustomEvaluationTrait
-{
-    protected function resolveJavaScriptEvaluation(array $resultArray, string $name, ?object $evalObject): array
-    {
-        if (!is_object($evalObject) || !method_exists($evalObject, 'returnFieldJS')) {
-            return $resultArray;
-        }
-
-        $javaScriptEvaluation = $evalObject->returnFieldJS();
-        if ($javaScriptEvaluation instanceof JavaScriptModuleInstruction) {
-            // just use the module name and export-name
-            $resultArray['requireJsModules'][] = JavaScriptModuleInstruction::forRequireJS(
-                $javaScriptEvaluation->getName(),
-                $javaScriptEvaluation->getExportName()
-            )->invoke('registerCustomEvaluation', $name);
-        } else {
-            // @todo deprecate inline JavaScript in TYPO3 v12.0
-            $resultArray['additionalJavaScriptPost'][] = sprintf(
-                'TBE_EDITOR.customEvalFunctions[%s] = function(value) { %s };',
-                GeneralUtility::quoteJSvalue($name),
-                $javaScriptEvaluation
-            );
-        }
-
-        return $resultArray;
-    }
-}
diff --git a/typo3/sysext/backend/Classes/Form/Element/InputColorPickerElement.php b/typo3/sysext/backend/Classes/Form/Element/InputColorPickerElement.php
index 5c2eac87d6be..beb19f4e8911 100644
--- a/typo3/sysext/backend/Classes/Form/Element/InputColorPickerElement.php
+++ b/typo3/sysext/backend/Classes/Form/Element/InputColorPickerElement.php
@@ -26,8 +26,6 @@ use TYPO3\CMS\Core\Utility\StringUtility;
  */
 class InputColorPickerElement extends AbstractFormElement
 {
-    use CustomEvaluationTrait;
-
     /**
      * Default field information enabled for this element.
      *
diff --git a/typo3/sysext/backend/Classes/Form/Element/InputLinkElement.php b/typo3/sysext/backend/Classes/Form/Element/InputLinkElement.php
index a960e39f0d3b..1c4d732daf95 100644
--- a/typo3/sysext/backend/Classes/Form/Element/InputLinkElement.php
+++ b/typo3/sysext/backend/Classes/Form/Element/InputLinkElement.php
@@ -39,7 +39,6 @@ use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
  */
 class InputLinkElement extends AbstractFormElement
 {
-    use CustomEvaluationTrait;
     use OnFieldChangeTrait;
 
     /**
diff --git a/typo3/sysext/backend/Classes/Form/Element/InputTextElement.php b/typo3/sysext/backend/Classes/Form/Element/InputTextElement.php
index 95f9eb00c695..082c8842b4ec 100644
--- a/typo3/sysext/backend/Classes/Form/Element/InputTextElement.php
+++ b/typo3/sysext/backend/Classes/Form/Element/InputTextElement.php
@@ -30,7 +30,6 @@ use TYPO3\CMS\Core\Utility\StringUtility;
  */
 class InputTextElement extends AbstractFormElement
 {
-    use CustomEvaluationTrait;
     use OnFieldChangeTrait;
 
     /**
-- 
GitLab