2 declare(strict_types
=1);
3 namespace TYPO3\CMS\Backend\Controller
;
6 * This file is part of the TYPO3 CMS project.
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use TYPO3\CMS\Core\Localization\LocalizationFactory
;
19 use TYPO3\CMS\Core\Utility\ArrayUtility
;
20 use TYPO3\CMS\Core\Utility\GeneralUtility
;
21 use TYPO3\CMS\Core\Utility\PathUtility
;
24 * Abstract class for a couple of FormEngine controllers triggered by
25 * ajax calls. The class containers some helpers to for instance prepare
26 * the form render result for json output.
28 * @internal Marked as internal for now, methods in this class may change any time.
30 abstract class AbstractFormEngineAjaxController
33 * Gets result array from FormEngine and returns string with js modules
34 * that need to be loaded and evaluated by JavaScript.
36 * @param array $result
39 public function createExecutableStringRepresentationOfRegisteredRequireJsModules(array $result): array
41 if (empty($result['requireJsModules'])) {
45 foreach ($result['requireJsModules'] as $module) {
48 if (is_string($module)) {
49 // if $module is a string, no callback
50 $moduleName = $module;
52 } elseif (is_array($module)) {
53 // if $module is an array, callback is possible
54 foreach ($module as $key => $value) {
60 if ($moduleName !== null) {
61 $inlineCodeKey = $moduleName;
62 $javaScriptCode = 'require(["' . $moduleName . '"]';
63 if ($callback !== null) {
64 $inlineCodeKey .= sha1($callback);
65 $javaScriptCode .= ', ' . $callback;
67 $javaScriptCode .= ');';
68 $requireJs[] = '/*RequireJS-Module-' . $inlineCodeKey . '*/' . LF
. $javaScriptCode;
75 * Resolve a CSS file position, possibly prefixed with 'EXT:'
77 * @param string $stylesheetFile Given file, possibly prefixed with EXT:
78 * @return string Web root relative position to file
80 protected function getRelativePathToStylesheetFile(string $stylesheetFile): string
82 if (strpos($stylesheetFile, 'EXT:') === 0) {
83 $stylesheetFile = GeneralUtility
::getFileAbsFileName($stylesheetFile);
84 $stylesheetFile = PathUtility
::getRelativePathTo($stylesheetFile);
85 $stylesheetFile = rtrim($stylesheetFile, '/');
87 $stylesheetFile = GeneralUtility
::resolveBackPath($stylesheetFile);
89 $stylesheetFile = GeneralUtility
::createVersionNumberedFilename($stylesheetFile);
90 return PathUtility
::getAbsoluteWebPath($stylesheetFile);
94 * Parse a language file and get a label/value array from it.
96 * @param string $file EXT:path/to/file
97 * @return array Label/value array
99 protected function getLabelsFromLocalizationFile($file)
101 /** @var $languageFactory LocalizationFactory */
102 $languageFactory = GeneralUtility
::makeInstance(LocalizationFactory
::class);
103 $language = $GLOBALS['LANG']->lang
;
104 $localizationArray = $languageFactory->getParsedData($file, $language);
105 if (is_array($localizationArray) && !empty($localizationArray)) {
106 if (!empty($localizationArray[$language])) {
107 $xlfLabelArray = $localizationArray['default'];
108 ArrayUtility
::mergeRecursiveWithOverrule($xlfLabelArray, $localizationArray[$language], true, false);
110 $xlfLabelArray = $localizationArray['default'];
116 foreach ($xlfLabelArray as $key => $value) {
117 if (isset($value[0]['target'])) {
118 $labelArray[$key] = $value[0]['target'];
120 $labelArray[$key] = '';