[TASK] Add logic to include languageLablelFiles via return structure 69/46669/2
authorFrank Naegler <frank.naegler@typo3.org>
Mon, 1 Feb 2016 15:56:43 +0000 (16:56 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 15 Feb 2016 15:43:09 +0000 (16:43 +0100)
Adds a structure to accept language files like EXT:foo/bar.xlf
in FormEngine elements that are added in inline ajax and on
page load.

Since it is very hard for FormEngine element developers to
handle this case without bugs in own code and renders label
handling useless otherwise, this patch is requested for 7.6, too.

Resolves: #73072
Releases: master, 7.6
Change-Id: Icbfe707000931037b9579bb8bc6a96dfca023d3e
Reviewed-on: https://review.typo3.org/46669
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Controller/FormInlineAjaxController.php
typo3/sysext/backend/Classes/Form/AbstractNode.php
typo3/sysext/backend/Classes/Form/FormResultCompiler.php

index 9575aa9..7bc1b69 100644 (file)
@@ -23,6 +23,8 @@ use TYPO3\CMS\Backend\Form\InlineStackProcessor;
 use TYPO3\CMS\Backend\Form\NodeFactory;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\DataHandling\DataHandler;
+use TYPO3\CMS\Core\Localization\LocalizationFactory;
+use TYPO3\CMS\Core\Utility\ArrayUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
 
@@ -626,6 +628,27 @@ class FormInlineAjaxController
             $jsonResult['scriptCall'][] = $singleAdditionalJavaScriptPost;
         }
         $jsonResult['scriptCall'][] = $childResult['extJSCODE'];
+        if (!empty($childResult['additionalInlineLanguageLabelFiles'])) {
+            $labels = [];
+            foreach ($childResult['additionalInlineLanguageLabelFiles'] as $additionalInlineLanguageLabelFile) {
+                ArrayUtility::mergeRecursiveWithOverrule(
+                    $labels,
+                    $this->addInlineLanguageLabelFile($additionalInlineLanguageLabelFile)
+                );
+            }
+            $javaScriptCode = [];
+            $javaScriptCode[] = 'if (typeof TYPO3 === \'undefined\' || typeof TYPO3.lang === \'undefined\') {';
+            $javaScriptCode[] = '   TYPO3.lang = {}';
+            $javaScriptCode[] = '}';
+            $javaScriptCode[] = 'var additionalInlineLanguageLabels = ' . json_encode($labels) . ';';
+            $javaScriptCode[] = 'for (var attributeName in additionalInlineLanguageLabels) {';
+            $javaScriptCode[] = '   if (typeof TYPO3.lang[attributeName] === \'undefined\') {';
+            $javaScriptCode[] = '       TYPO3.lang[attributeName] = additionalInlineLanguageLabels[attributeName]';
+            $javaScriptCode[] = '   }';
+            $javaScriptCode[] = '}';
+
+            $jsonResult['scriptCall'][] = implode(LF, $javaScriptCode);
+        }
         if (!empty($childResult['requireJsModules'])) {
             foreach ($childResult['requireJsModules'] as $module) {
                 $moduleName = null;
@@ -658,6 +681,43 @@ class FormInlineAjaxController
     }
 
     /**
+     * @param string $file
+     *
+     * @return array
+     */
+    protected function addInlineLanguageLabelFile($file)
+    {
+        /** @var $languageFactory LocalizationFactory */
+        $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
+        $language = $GLOBALS['LANG']->lang;
+        $localizationArray = $languageFactory->getParsedData(
+            $file,
+            $language,
+            'utf-8',
+            1
+        );
+        if (is_array($localizationArray) && !empty($localizationArray)) {
+            if (!empty($localizationArray[$language])) {
+                $xlfLabelArray = $localizationArray['default'];
+                ArrayUtility::mergeRecursiveWithOverrule($xlfLabelArray, $localizationArray[$language], true, false);
+            } else {
+                $xlfLabelArray = $localizationArray['default'];
+            }
+        } else {
+            $xlfLabelArray = [];
+        }
+        $labelArray = [];
+        foreach ($xlfLabelArray as $key => $value) {
+            if (isset($value[0]['target'])) {
+                $labelArray[$key] = $value[0]['target'];
+            } else {
+                $labelArray[$key] = '';
+            }
+        }
+        return $labelArray;
+    }
+
+    /**
      * Gets an array with the uids of related records out of a list of items.
      * This list could contain more information than required. This methods just
      * extracts the uids.
index ae9d255..4fd9b2e 100644 (file)
@@ -66,6 +66,7 @@ abstract class AbstractNode implements NodeInterface
             'additionalJavaScriptPost' => array(),
             'additionalJavaScriptSubmit' => array(),
             'additionalHiddenFields' => array(),
+            'additionalInlineLanguageLabelFiles' => array(),
             'stylesheetFiles' => array(),
             // can hold strings or arrays, string = requireJS module, array = requireJS module + callback e.g. array('TYPO3/Foo/Bar', 'function() {}')
             'requireJsModules' => array(),
@@ -107,6 +108,11 @@ abstract class AbstractNode implements NodeInterface
                 $existing['requireJsModules'][] = $module;
             }
         }
+        if (!empty($childReturn['additionalInlineLanguageLabelFiles'])) {
+            foreach ($childReturn['additionalInlineLanguageLabelFiles'] as $inlineLanguageLabelFile) {
+                $existing['additionalInlineLanguageLabelFiles'][] = $inlineLanguageLabelFile;
+            }
+        }
         if (!empty($childReturn['inlineData'])) {
             $existingInlineData = $existing['inlineData'];
             $childInlineData = $childReturn['inlineData'];
index df65127..a1814e5 100644 (file)
@@ -76,6 +76,13 @@ class FormResultCompiler
     protected $additionalJS_submit = array();
 
     /**
+     * Additional language label files to include.
+     *
+     * @var array
+     */
+    protected $additionalInlineLanguageLabelFiles = array();
+
+    /**
      * Array with requireJS modules, use module name as key, the value could be callback code.
      * Use NULL as value if no callback is used.
      *
@@ -151,6 +158,12 @@ class FormResultCompiler
             ArrayUtility::mergeRecursiveWithOverrule($resultArrayInlineData, $resultInlineData);
             $this->inlineData = $resultArrayInlineData;
         }
+
+        if (!empty($resultArray['additionalInlineLanguageLabelFiles'])) {
+            foreach ($resultArray['additionalInlineLanguageLabelFiles'] as $additionalInlineLanguageLabelFile) {
+                $this->additionalInlineLanguageLabelFiles[] = $additionalInlineLanguageLabelFile;
+            }
+        }
     }
 
     /**
@@ -254,6 +267,11 @@ class FormResultCompiler
             \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('lang') . 'locallang_core.xlf',
             'file_upload'
         );
+        if (!empty($this->additionalInlineLanguageLabelFiles)) {
+            foreach ($this->additionalInlineLanguageLabelFiles as $additionalInlineLanguageLabelFile) {
+                $pageRenderer->addInlineLanguageLabelFile($additionalInlineLanguageLabelFile);
+            }
+        }
         // Load codemirror for T3Editor
         if (ExtensionManagementUtility::isLoaded('t3editor')) {
             $this->loadJavascriptLib(ExtensionManagementUtility::extRelPath('t3editor') . 'Resources/Public/JavaScript/Contrib/codemirror/js/codemirror.js');