This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
FieldViewHelper.php
379 lines (320 loc) 路 13.2 KB
/
FieldViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/*
* 2017 Romain CANON <romain.hydrocanon@gmail.com>
*
* This file is part of the TYPO3 FormZ project.
* It is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, either
* version 3 of the License, or any later version.
*
* For the full copyright and license information, see:
* http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Romm\Formz\ViewHelpers;
use Romm\Formz\Configuration\View\Layouts\Layout;
use Romm\Formz\Configuration\View\View;
use Romm\Formz\Exceptions\ContextNotFoundException;
use Romm\Formz\Exceptions\EntryNotFoundException;
use Romm\Formz\Exceptions\InvalidArgumentTypeException;
use Romm\Formz\Exceptions\InvalidArgumentValueException;
use Romm\Formz\Exceptions\PropertyNotAccessibleException;
use Romm\Formz\Service\StringService;
use Romm\Formz\Service\ViewHelper\Field\FieldViewHelperService;
use Romm\Formz\Service\ViewHelper\Form\FormViewHelperService;
use Romm\Formz\Service\ViewHelper\Slot\SlotViewHelperService;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
/**
* This view helper is used to automatize the rendering of a field layout. It
* will use the TypoScript properties at the path `config.tx_formz.view.layout`.
*
* You need to indicate the name of the field which will be rendered, and the
* name of the layout which should be used (it must be present in the TypoScript
* configuration).
*
* Example of layout: `bootstrap.3-cols`. You may indicate only the group, then
* the name of the layout will be set to `default` (if you use the layout group
* `bootstrap`, the layout `default` will be used, only if it does exist of
* course).
*/
class FieldViewHelper extends AbstractViewHelper
{
/**
* @var bool
*/
protected $escapeOutput = false;
/**
* @var array
*/
public static $reservedVariablesNames = ['layout', 'formName', 'fieldName', 'fieldId'];
/**
* @var FormViewHelperService
*/
protected $formService;
/**
* @var FieldViewHelperService
*/
protected $fieldService;
/**
* @var SlotViewHelperService
*/
protected $slotService;
/**
* @inheritdoc
*/
public function initializeArguments()
{
$this->registerArgument('name', 'string', 'Name of the field which should be rendered.', true);
$this->registerArgument('layout', 'string', 'Path of the TypoScript layout which will be used.', true);
$this->registerArgument('arguments', 'array', 'Arbitrary arguments which will be sent to the field template.', false, []);
}
/**
* @inheritdoc
*/
public function render()
{
/*
* First, we check if this view helper is called from within the
* `FormViewHelper`, because it would not make sense anywhere else.
*/
if (false === $this->formService->formContextExists()) {
throw ContextNotFoundException::fieldViewHelperFormContextNotFound();
}
/*
* Then, we inject the wanted field in the `FieldService` so we can know
* later which field we're working with.
*/
$this->injectFieldInService($this->arguments['name']);
/*
* Activating the slot service, which will be used all along the
* rendering of this very field.
*/
$this->slotService->activate($this->renderingContext);
/*
* Calling this here will process every view helper beneath this one,
* allowing options and slots to be used correctly in the field layout.
*/
$this->renderChildren();
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '8.0.0', '<')) {
$restoreCallback = $this->storeViewDataLegacy();
}
$templateArguments = $this->getTemplateArguments();
$result = $this->renderLayoutView($templateArguments);
/*
* Resetting all services data.
*/
$this->fieldService->removeCurrentField();
$this->slotService->resetState();
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '8.0.0', '<')) {
/** @noinspection PhpUndefinedVariableInspection */
$restoreCallback($templateArguments);
}
return $result;
}
/**
* Will return the associated Fluid view for this field (configured with the
* `layout` argument).
*
* @param array $templateArguments
* @return string
*/
protected function renderLayoutView(array $templateArguments)
{
$fieldName = $this->arguments['name'];
$formObject = $this->formService->getFormObject();
$formConfiguration = $formObject->getConfiguration();
$viewConfiguration = $formConfiguration->getRootConfiguration()->getView();
$layout = $this->getLayout($viewConfiguration);
$templateArguments['layout'] = $layout->getLayout();
$templateArguments['formName'] = $formObject->getName();
$templateArguments['fieldName'] = $fieldName;
$templateArguments['fieldId'] = ($templateArguments['fieldId']) ?: StringService::get()->sanitizeString('formz-' . $formObject->getName() . '-' . $fieldName);
$view = $this->fieldService->getView($layout);
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '8.0.0', '<')) {
$view->setRenderingContext($this->renderingContext);
} else {
$renderingContext = $view->getRenderingContext();
// Removing all variables previously added to the provider.
$provider = $renderingContext->getVariableProvider();
foreach ($provider->getAllIdentifiers() as $key) {
$provider->remove($key);
}
/*
* Updating the view dependencies: the variable container as well as
* the controller context must be injected in the view.
*/
$renderingContext->setViewHelperVariableContainer($this->viewHelperVariableContainer);
$view->setControllerContext($this->controllerContext);
$this->viewHelperVariableContainer->setView($view);
}
$view->setLayoutRootPaths($this->getPaths('layout'));
$view->setPartialRootPaths($this->getPaths('partial'));
$view->assignMultiple($templateArguments);
return $view->render();
}
/**
* Temporary solution for TYPO3 6.2 to 7.6 that will store the current view
* variables, to be able to restore them later.
*
* A callback function is returned; it will be called once the field layout
* view was processed, and will restore all the view data.
*
* @return \Closure
*
* @deprecated Will be deleted when TYPO3 7.6 is not supported anymore.
*/
protected function storeViewDataLegacy()
{
$originalArguments = [];
$variableProvider = $this->getVariableProvider();
foreach (self::$reservedVariablesNames as $key) {
if ($variableProvider->exists($key)) {
$originalArguments[$key] = $variableProvider->get($key);
}
}
$viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
$currentView = $viewHelperVariableContainer->getView();
return function (array $templateArguments) use ($originalArguments, $variableProvider, $viewHelperVariableContainer, $currentView) {
$viewHelperVariableContainer->setView($currentView);
/*
* Cleaning up the variables in the provider: the original
* values are restored to make the provider like it was before
* the field rendering started.
*/
foreach ($variableProvider->getAllIdentifiers() as $identifier) {
if (array_key_exists($identifier, $templateArguments)) {
$variableProvider->remove($identifier);
}
}
foreach ($originalArguments as $key => $value) {
if ($variableProvider->exists($key)) {
$variableProvider->remove($key);
}
$variableProvider->add($key, $value);
}
};
}
/**
* Will check that the given field exists in the current form definition and
* inject it in the `FieldService` as `currentField`.
*
* Throws an error if the field is not found or incorrect.
*
* @param string $fieldName
* @throws InvalidArgumentTypeException
* @throws PropertyNotAccessibleException
*/
protected function injectFieldInService($fieldName)
{
$formObject = $this->formService->getFormObject();
$formConfiguration = $formObject->getConfiguration();
if (false === is_string($fieldName)) {
throw InvalidArgumentTypeException::fieldViewHelperInvalidTypeNameArgument();
} elseif (false === $formConfiguration->hasField($fieldName)) {
throw PropertyNotAccessibleException::fieldViewHelperFieldNotAccessibleInForm($formObject, $fieldName);
}
$this->fieldService->setCurrentField($formConfiguration->getField($fieldName));
}
/**
* Returns the layout instance used by this field.
*
* @param View $viewConfiguration
* @return Layout
* @throws EntryNotFoundException
* @throws InvalidArgumentTypeException
* @throws InvalidArgumentValueException
*/
protected function getLayout(View $viewConfiguration)
{
$layout = $this->arguments['layout'];
if (false === is_string($layout)) {
throw InvalidArgumentTypeException::invalidTypeNameArgumentFieldViewHelper($layout);
}
list($layoutName, $templateName) = GeneralUtility::trimExplode('.', $layout);
if (empty($templateName)) {
$templateName = 'default';
}
if (empty($layoutName)) {
throw InvalidArgumentValueException::fieldViewHelperEmptyLayout();
}
if (false === $viewConfiguration->hasLayout($layoutName)) {
throw EntryNotFoundException::fieldViewHelperLayoutNotFound($layout);
}
if (false === $viewConfiguration->getLayout($layoutName)->hasItem($templateName)) {
throw EntryNotFoundException::fieldViewHelperLayoutItemNotFound($layout, $templateName);
}
return $viewConfiguration->getLayout($layoutName)->getItem($templateName);
}
/**
* Merging the arguments with the ones registered with the
* `OptionViewHelper`.
*
* @return array
*/
protected function getTemplateArguments()
{
$templateArguments = $this->arguments['arguments'] ?: [];
ArrayUtility::mergeRecursiveWithOverrule($templateArguments, $this->fieldService->getFieldOptions());
return $templateArguments;
}
/**
* This function will determinate the layout/partial root paths that should
* be given to the standalone view. This must be a merge between the paths
* given in the TypoScript configuration and the paths of the current view.
*
* This way, the user can use the layouts/partials from both the form
* rendering extension, as well as the ones used by the field layout.
*
* Please note that TYPO3 v8+ has this behaviour by default, meaning only
* the TypoScript configuration paths are needed, however in TYPO3 v7.6- we
* need to access the root paths, which is *not* granted by Fluid... We are
* then forced to use reflection, please don't do this at home!
*
* @param string $type `partial` or `layout`
* @return array
*
* @deprecated Must be removed when TYPO3 7.6 is not supported anymore!
*/
protected function getPaths($type)
{
$viewConfiguration = $this->formService->getFormObject()->getConfiguration()->getRootConfiguration()->getView();
$paths = $type === 'partial'
? $viewConfiguration->getAbsolutePartialRootPaths()
: $viewConfiguration->getAbsoluteLayoutRootPaths();
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '8.0.0', '>=')) {
return $paths;
} else {
$currentView = $this->renderingContext->getViewHelperVariableContainer()->getView();
$propertyName = $type === 'partial'
? 'getPartialRootPaths'
: 'getLayoutRootPaths';
$reflectionClass = new \ReflectionClass($currentView);
$method = $reflectionClass->getMethod($propertyName);
$method->setAccessible(true);
$value = $method->invoke($currentView);
return array_unique(array_merge($paths, $value));
}
}
/**
* @param FormViewHelperService $service
*/
public function injectFormService(FormViewHelperService $service)
{
$this->formService = $service;
}
/**
* @param FieldViewHelperService $service
*/
public function injectFieldService(FieldViewHelperService $service)
{
$this->fieldService = $service;
}
/**
* @param SlotViewHelperService $slotService
*/
public function injectSlotService(SlotViewHelperService $slotService)
{
$this->slotService = $slotService;
}
}