[TASK] Resolve cross dep between EXT:backend and EXT:feedit
[Packages/TYPO3.CMS.git] / typo3 / sysext / feedit / Classes / FrontendEditAssetLoader.php
1 <?php
2 namespace TYPO3\CMS\Feedit;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Controller\EditDocumentController;
18 use TYPO3\CMS\Core\Page\PageRenderer;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Class FrontendEditAssetLoader
23 */
24 class FrontendEditAssetLoader
25 {
26 /**
27 * @param EditDocumentController $controller
28 * @return void
29 */
30 public function attachAssets(EditDocumentController $controller)
31 {
32 if ((int)GeneralUtility::_GP('feEdit') === 1) {
33 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
34 // We have to load some locallang strings and push them into TYPO3.LLL if this request was
35 // triggered by feedit. Originally, this object is fed by BackendController which is not
36 // called here. This block of code is intended to be removed at a later point again.
37 $lang = $this->getLanguageService();
38 $coreLabels = [
39 'csh_tooltip_loading' => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:csh_tooltip_loading')
40 ];
41 $generatedLabels = [];
42 $generatedLabels['core'] = $coreLabels;
43 $code = 'TYPO3.LLL = ' . json_encode($generatedLabels) . ';';
44 $filePath = 'typo3temp/assets/js/backend-' . sha1($code) . '.js';
45 if (!file_exists(PATH_site . $filePath)) {
46 // writeFileToTypo3tempDir() returns NULL on success (please double-read!)
47 $error = GeneralUtility::writeFileToTypo3tempDir(PATH_site . $filePath, $code);
48 if ($error !== null) {
49 throw new \RuntimeException('Locallang JS file could not be written to ' . $filePath . '. Reason: ' . $error, 1446118286);
50 }
51 }
52 $pageRenderer->addJsFile('../' . $filePath);
53 }
54 }
55
56 /**
57 * Returns LanguageService
58 *
59 * @return \TYPO3\CMS\Lang\LanguageService
60 */
61 protected function getLanguageService()
62 {
63 return $GLOBALS['LANG'];
64 }
65 }