[!!!][TASK] Improve flex and TCA handling in FormEngine
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Wizard / SuggestWizard.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Wizard;
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\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Fluid\View\StandaloneView;
19
20 /**
21 * Wizard for rendering an AJAX selector for records.
22 * See SuggestWizardController for the ajax handling counterpart.
23 */
24 class SuggestWizard
25 {
26
27 /**
28 * @var StandaloneView
29 */
30 protected $view;
31
32 /**
33 * Construct
34 *
35 * @param StandaloneView $view
36 */
37 public function __construct(StandaloneView $view = null)
38 {
39 $this->view = $view ?: $this->getFluidTemplateObject('SuggestWizard.html');
40 }
41
42 /**
43 * Renders an ajax-enabled text field. Also adds required JS
44 *
45 * @param array $data Main data array from FormEngine
46 * @throws \RuntimeException for incomplete incoming arguments
47 * @return string The HTML code for the selector
48 */
49 public function renderSuggestSelector(array $data)
50 {
51 $fieldName = $data['fieldName'];
52 $dataStructureIdentifier = '';
53 $flexFormSheetName = '';
54 $flexFormFieldName = '';
55 $flexFormContainerName = '';
56 $flexFormContainerFieldName = '';
57 if ($data['processedTca']['columns'][$fieldName]['config']['type'] === 'flex') {
58 $flexFormConfig = $data['processedTca']['columns'][$fieldName];
59 $dataStructureIdentifier = $flexFormConfig['config']['dataStructureIdentifier'];
60 if (!isset($flexFormConfig['config']['dataStructureIdentifier'])) {
61 throw new \RuntimeException(
62 'A data structure identifier must be set in [\'config\'] part of a flex form.'
63 . ' This is usually added by TcaFlexPrepare data processor',
64 1478604742
65 );
66 }
67 if (isset($data['flexFormSheetName'])) {
68 $flexFormSheetName = $data['flexFormSheetName'];
69 }
70 if (isset($data['flexFormFieldName'])) {
71 $flexFormFieldName = $data['flexFormFieldName'];
72 }
73 if (isset($data['flexFormContainerName'])) {
74 $flexFormContainerName = $data['flexFormContainerName'];
75 }
76 if (isset($data['flexFormContainerFieldName'])) {
77 $flexFormContainerFieldName = $data['flexFormContainerFieldName'];
78 }
79 }
80
81 // Get minimumCharacters from TCA
82 $minChars = 0;
83 $fieldTca = $data['parameterArray']['fieldConf'];
84 if (isset($fieldTca['config']['wizards']['suggest']['default']['minimumCharacters'])) {
85 $minChars = (int)$fieldTca['config']['wizards']['suggest']['default']['minimumCharacters'];
86 }
87 // Overwrite it with minimumCharacters from TSConfig if given
88 $fieldTsConfig = $data['parameterArray']['fieldTSConfig'];
89 if (isset($fieldTsConfig['suggest.']['default.']['minimumCharacters'])) {
90 $minChars = (int)$fieldTsConfig['suggest.']['default.']['minimumCharacters'];
91 }
92 $minChars = $minChars > 0 ? $minChars : 2;
93
94 $this->view->assignMultiple([
95 'placeholder' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.findRecord',
96 'fieldName' => $data['fieldName'],
97 'tableName' => $data['tableName'],
98 'field' => $data['parameterArray']['itemFormElName'],
99 'uid' => $data['databaseRow']['uid'],
100 'pid' => (int)$data['effectivePid'],
101 'dataStructureIdentifier' => $dataStructureIdentifier,
102 'flexFormSheetName' => $flexFormSheetName,
103 'flexFormFieldName' => $flexFormFieldName,
104 'flexFormContainerName' => $flexFormContainerName,
105 'flexFormContainerFieldName' => $flexFormContainerFieldName,
106 'fieldtype' => $fieldTca['config']['type'],
107 'minchars' => (int)$minChars,
108 ]);
109
110 return $this->view->render();
111 }
112
113 /**
114 * Returns a new standalone view, shorthand function
115 *
116 * @param string $filename Which templateFile should be used.
117 *
118 * @return StandaloneView
119 */
120 protected function getFluidTemplateObject(string $filename = null): StandaloneView
121 {
122 /** @var StandaloneView $view */
123 $view = GeneralUtility::makeInstance(StandaloneView::class);
124 $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
125 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
126 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
127
128 if ($filename === null) {
129 $filename = 'SuggestWizard.html';
130 }
131
132 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/Wizards/' . $filename));
133
134 $view->getRequest()->setControllerExtensionName('Backend');
135 return $view;
136 }
137 }