[BUGFIX] Handle new record correctly in selectTree form element
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / SelectTreeController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Form\FormDataCompiler;
20 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaDatabaseRecord;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Backend controller for selectTree ajax operations
25 */
26 class SelectTreeController
27 {
28 /**
29 * Returns json representing category tree
30 *
31 * @param ServerRequestInterface $request
32 * @param ResponseInterface $response
33 * @return ResponseInterface
34 */
35 public function fetchDataAction(ServerRequestInterface $request, ResponseInterface $response)
36 {
37 $tableName = $request->getQueryParams()['table'];
38 if (!$this->getBackendUser()->check('tables_select', $tableName)) {
39 return $response;
40 }
41 $formDataGroup = GeneralUtility::makeInstance(TcaDatabaseRecord::class);
42 $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
43
44 $formDataCompilerInput = [
45 'tableName' => $request->getQueryParams()['table'],
46 'vanillaUid' => (int)$request->getQueryParams()['uid'],
47 'command' => $request->getQueryParams()['command'],
48 ];
49 $fieldName = $request->getQueryParams()['field'];
50 $formData = $formDataCompiler->compile($formDataCompilerInput);
51 $treeData = $formData['processedTca']['columns'][$fieldName]['config']['treeData'];
52 $json = json_encode($treeData['items']);
53 $response->getBody()->write($json);
54 return $response;
55 }
56
57 /**
58 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
59 */
60 protected function getBackendUser()
61 {
62 return $GLOBALS['BE_USER'];
63 }
64 }