[BUGFIX] Finishers with output must return the output
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Runtime / FormRuntime.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Runtime;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It originated from the Neos.Form package (www.neos.io)
9 *
10 * It is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, either version 2
12 * of the License, or any later version.
13 *
14 * For the full copyright and license information, please read the
15 * LICENSE.txt file that was distributed with this source code.
16 *
17 * The TYPO3 project - inspiring people to share!
18 */
19
20 use TYPO3\CMS\Core\Utility\ArrayUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Extbase\Error\Result;
23 use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
24 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
25 use TYPO3\CMS\Extbase\Mvc\Web\Request;
26 use TYPO3\CMS\Extbase\Mvc\Web\Response;
27 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
28 use TYPO3\CMS\Extbase\Property\Exception as PropertyException;
29 use TYPO3\CMS\Extbase\Reflection\PropertyReflection;
30 use TYPO3\CMS\Form\Domain\Exception\RenderingException;
31 use TYPO3\CMS\Form\Domain\Finishers\FinisherContext;
32 use TYPO3\CMS\Form\Domain\Model\FormDefinition;
33 use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
34 use TYPO3\CMS\Form\Domain\Model\FormElements\Page;
35 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
36 use TYPO3\CMS\Form\Domain\Renderer\RendererInterface;
37 use TYPO3\CMS\Form\Domain\Runtime\Exception\PropertyMappingException;
38 use TYPO3\CMS\Form\Mvc\Validation\EmptyValidator;
39 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
40
41 /**
42 * This class implements the *runtime logic* of a form, i.e. deciding which
43 * page is shown currently, what the current values of the form are, trigger
44 * validation and property mapping.
45 *
46 * You generally receive an instance of this class by calling {@link \TYPO3\CMS\Form\Domain\Model\FormDefinition::bind}.
47 *
48 * Rendering a Form
49 * ================
50 *
51 * That's easy, just call render() on the FormRuntime:
52 *
53 * /---code php
54 * $form = $formDefinition->bind($request, $response);
55 * $renderedForm = $form->render();
56 * \---
57 *
58 * Accessing Form Values
59 * =====================
60 *
61 * In order to get the values the user has entered into the form, you can access
62 * this object like an array: If a form field with the identifier *firstName*
63 * exists, you can do **$form['firstName']** to retrieve its current value.
64 *
65 * You can also set values in the same way.
66 *
67 * Rendering Internals
68 * ===================
69 *
70 * The FormRuntime asks the FormDefinition about the configured Renderer
71 * which should be used ({@link \TYPO3\CMS\Form\Domain\Model\FormDefinition::getRendererClassName}),
72 * and then trigger render() on this Renderer.
73 *
74 * This makes it possible to declaratively define how a form should be rendered.
75 *
76 * Scope: frontend
77 * **This class is NOT meant to be sub classed by developers.**
78 * @api
79 */
80 class FormRuntime implements RootRenderableInterface, \ArrayAccess
81 {
82 const HONEYPOT_NAME_SESSION_IDENTIFIER = 'tx_form_honeypot_name_';
83
84 /**
85 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
86 */
87 protected $objectManager;
88
89 /**
90 * @var \TYPO3\CMS\Form\Domain\Model\FormDefinition
91 */
92 protected $formDefinition;
93
94 /**
95 * @var \TYPO3\CMS\Extbase\Mvc\Web\Request
96 */
97 protected $request;
98
99 /**
100 * @var \TYPO3\CMS\Extbase\Mvc\Web\Response
101 */
102 protected $response;
103
104 /**
105 * @var \TYPO3\CMS\Form\Domain\Runtime\FormState
106 */
107 protected $formState;
108
109 /**
110 * The current page is the page which will be displayed to the user
111 * during rendering.
112 *
113 * If $currentPage is NULL, the *last* page has been submitted and
114 * finishing actions need to take place. You should use $this->isAfterLastPage()
115 * instead of explicitely checking for NULL.
116 *
117 * @var \TYPO3\CMS\Form\Domain\Model\FormElements\Page
118 */
119 protected $currentPage = null;
120
121 /**
122 * Reference to the page which has been shown on the last request (i.e.
123 * we have to handle the submitted data from lastDisplayedPage)
124 *
125 * @var \TYPO3\CMS\Form\Domain\Model\FormElements\Page
126 */
127 protected $lastDisplayedPage = null;
128
129 /**
130 * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
131 */
132 protected $hashService;
133
134 /**
135 * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService
136 * @internal
137 */
138 public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
139 {
140 $this->hashService = $hashService;
141 }
142
143 /**
144 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
145 * @internal
146 */
147 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
148 {
149 $this->objectManager = $objectManager;
150 }
151
152 /**
153 * @param FormDefinition $formDefinition
154 * @param Request $request
155 * @param Response $response
156 * @api
157 */
158 public function __construct(FormDefinition $formDefinition, Request $request, Response $response)
159 {
160 $this->formDefinition = $formDefinition;
161 $arguments = $request->getArguments();
162 $this->request = clone $request;
163 $formIdentifier = $this->formDefinition->getIdentifier();
164 if (isset($arguments[$formIdentifier])) {
165 $this->request->setArguments($arguments[$formIdentifier]);
166 }
167
168 $this->response = $response;
169 }
170
171 /**
172 * @internal
173 */
174 public function initializeObject()
175 {
176 $this->initializeFormStateFromRequest();
177 $this->initializeCurrentPageFromRequest();
178 $this->initializeHoneypotFromRequest();
179
180 if (!$this->isFirstRequest() && $this->getRequest()->getMethod() === 'POST') {
181 $this->processSubmittedFormValues();
182 }
183
184 $this->renderHoneypot();
185 }
186
187 /**
188 * Initializes the current state of the form, based on the request
189 */
190 protected function initializeFormStateFromRequest()
191 {
192 $serializedFormStateWithHmac = $this->request->getInternalArgument('__state');
193 if ($serializedFormStateWithHmac === null) {
194 $this->formState = GeneralUtility::makeInstance(FormState::class);
195 } else {
196 $serializedFormState = $this->hashService->validateAndStripHmac($serializedFormStateWithHmac);
197 $this->formState = unserialize(base64_decode($serializedFormState));
198 }
199 }
200
201 /**
202 * Initializes the current page data based on the current request, also modifiable by a hook
203 */
204 protected function initializeCurrentPageFromRequest()
205 {
206 if (!$this->formState->isFormSubmitted()) {
207 $this->currentPage = $this->formDefinition->getPageByIndex(0);
208 if (
209 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterInitializeCurrentPage'])
210 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterInitializeCurrentPage'])
211 ) {
212 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterInitializeCurrentPage'] as $className) {
213 $hookObj = GeneralUtility::makeInstance($className);
214 if (method_exists($hookObj, 'afterInitializeCurrentPage')) {
215 $this->currentPage = $hookObj->afterInitializeCurrentPage(
216 $this,
217 $this->currentPage,
218 null,
219 $this->request->getArguments()
220 );
221 }
222 }
223 }
224 return;
225 }
226 $this->lastDisplayedPage = $this->formDefinition->getPageByIndex($this->formState->getLastDisplayedPageIndex());
227
228 // We know now that lastDisplayedPage is filled
229 $currentPageIndex = (int)$this->request->getInternalArgument('__currentPage');
230 if ($currentPageIndex > $this->lastDisplayedPage->getIndex() + 1) {
231 // We only allow jumps to following pages
232 $currentPageIndex = $this->lastDisplayedPage->getIndex() + 1;
233 }
234
235 // We now know that the user did not try to skip a page
236 if ($currentPageIndex === count($this->formDefinition->getPages())) {
237 // Last Page
238 $this->currentPage = null;
239 } else {
240 $this->currentPage = $this->formDefinition->getPageByIndex($currentPageIndex);
241 }
242
243 if (
244 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterInitializeCurrentPage'])
245 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterInitializeCurrentPage'])
246 ) {
247 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterInitializeCurrentPage'] as $className) {
248 $hookObj = GeneralUtility::makeInstance($className);
249 if (method_exists($hookObj, 'afterInitializeCurrentPage')) {
250 $this->currentPage = $hookObj->afterInitializeCurrentPage(
251 $this,
252 $this->currentPage,
253 $this->lastDisplayedPage,
254 $this->request->getArguments()
255 );
256 }
257 }
258 }
259 }
260
261 /**
262 * Checks if the honey pot is active, and adds a validator if so.
263 */
264 protected function initializeHoneypotFromRequest()
265 {
266 $renderingOptions = $this->formDefinition->getRenderingOptions();
267 if (!isset($renderingOptions['honeypot']['enable']) || $renderingOptions['honeypot']['enable'] === false || TYPO3_MODE === 'BE') {
268 return;
269 }
270
271 ArrayUtility::assertAllArrayKeysAreValid($renderingOptions['honeypot'], ['enable', 'formElementToUse']);
272
273 if (!$this->isFirstRequest()) {
274 $elementsCount = count($this->lastDisplayedPage->getElements());
275 if ($elementsCount === 0) {
276 return;
277 }
278
279 $honeypotNameFromSession = $this->getHoneypotNameFromSession($this->lastDisplayedPage);
280 if ($honeypotNameFromSession) {
281 $honeypotElement = $this->lastDisplayedPage->createElement($honeypotNameFromSession, $renderingOptions['honeypot']['formElementToUse']);
282 $validator = $this->objectManager->get(EmptyValidator::class);
283 $honeypotElement->addValidator($validator);
284 }
285 }
286 }
287
288 /**
289 * Renders a hidden field if the honey pot is active.
290 */
291 protected function renderHoneypot()
292 {
293 $renderingOptions = $this->formDefinition->getRenderingOptions();
294 if (!isset($renderingOptions['honeypot']['enable']) || $renderingOptions['honeypot']['enable'] === false || TYPO3_MODE === 'BE') {
295 return;
296 }
297
298 ArrayUtility::assertAllArrayKeysAreValid($renderingOptions['honeypot'], ['enable', 'formElementToUse']);
299
300 if (!$this->isAfterLastPage()) {
301 $elementsCount = count($this->currentPage->getElements());
302 if ($elementsCount === 0) {
303 return;
304 }
305
306 if (!$this->isFirstRequest()) {
307 $honeypotNameFromSession = $this->getHoneypotNameFromSession($this->lastDisplayedPage);
308 if ($honeypotNameFromSession) {
309 $honeypotElement = $this->formDefinition->getElementByIdentifier($honeypotNameFromSession);
310 if ($honeypotElement instanceof FormElementInterface) {
311 $this->lastDisplayedPage->removeElement($honeypotElement);
312 }
313 }
314 }
315
316 $elementsCount = count($this->currentPage->getElements());
317 $randomElementNumber = mt_rand(0, ($elementsCount - 1));
318 $honeypotName = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, mt_rand(5, 26));
319
320 $referenceElement = $this->currentPage->getElements()[$randomElementNumber];
321 $honeypotElement = $this->currentPage->createElement($honeypotName, $renderingOptions['honeypot']['formElementToUse']);
322 $validator = $this->objectManager->get(EmptyValidator::class);
323
324 $honeypotElement->addValidator($validator);
325 if (mt_rand(0, 1) === 1) {
326 $this->currentPage->moveElementAfter($honeypotElement, $referenceElement);
327 } else {
328 $this->currentPage->moveElementBefore($honeypotElement, $referenceElement);
329 }
330 $this->setHoneypotNameInSession($this->currentPage, $honeypotName);
331 }
332 }
333
334 /**
335 * @param Page $page
336 * return null|string
337 */
338 protected function getHoneypotNameFromSession(Page $page)
339 {
340 if ($this->getTypoScriptFrontendController()->loginUser) {
341 $honeypotNameFromSession = $this->getTypoScriptFrontendController()->fe_user->getKey(
342 'user',
343 self::HONEYPOT_NAME_SESSION_IDENTIFIER . $this->getIdentifier() . $page->getIdentifier()
344 );
345 } else {
346 $honeypotNameFromSession = $this->getTypoScriptFrontendController()->fe_user->getKey(
347 'ses',
348 self::HONEYPOT_NAME_SESSION_IDENTIFIER . $this->getIdentifier() . $page->getIdentifier()
349 );
350 }
351 return $honeypotNameFromSession;
352 }
353
354 /**
355 * @param Page $page
356 * @param string $honeypotName
357 */
358 protected function setHoneypotNameInSession(Page $page, string $honeypotName)
359 {
360 if ($this->getTypoScriptFrontendController()->loginUser) {
361 $this->getTypoScriptFrontendController()->fe_user->setKey(
362 'user',
363 self::HONEYPOT_NAME_SESSION_IDENTIFIER . $this->getIdentifier() . $page->getIdentifier(),
364 $honeypotName
365 );
366 } else {
367 $this->getTypoScriptFrontendController()->fe_user->setKey(
368 'ses',
369 self::HONEYPOT_NAME_SESSION_IDENTIFIER . $this->getIdentifier() . $page->getIdentifier(),
370 $honeypotName
371 );
372 }
373 }
374
375 /**
376 * Returns TRUE if the last page of the form has been submitted, otherwise FALSE
377 *
378 * @return bool
379 */
380 protected function isAfterLastPage(): bool
381 {
382 return $this->currentPage === null;
383 }
384
385 /**
386 * Returns TRUE if no previous page is stored in the FormState, otherwise FALSE
387 *
388 * @return bool
389 */
390 protected function isFirstRequest(): bool
391 {
392 return $this->lastDisplayedPage === null;
393 }
394
395 /**
396 * Runs throuh all validations
397 */
398 protected function processSubmittedFormValues()
399 {
400 $result = $this->mapAndValidatePage($this->lastDisplayedPage);
401 if ($result->hasErrors() && !$this->userWentBackToPreviousStep()) {
402 $this->currentPage = $this->lastDisplayedPage;
403 $this->request->setOriginalRequestMappingResults($result);
404 }
405 }
406
407 /**
408 * returns TRUE if the user went back to any previous step in the form.
409 *
410 * @return bool
411 */
412 protected function userWentBackToPreviousStep(): bool
413 {
414 return !$this->isAfterLastPage() && !$this->isFirstRequest() && $this->currentPage->getIndex() < $this->lastDisplayedPage->getIndex();
415 }
416
417 /**
418 * @param Page $page
419 * @return Result
420 * @throws PropertyMappingException
421 */
422 protected function mapAndValidatePage(Page $page): Result
423 {
424 $result = $this->objectManager->get(Result::class);
425 $requestArguments = $this->request->getArguments();
426
427 $propertyPathsForWhichPropertyMappingShouldHappen = [];
428 $registerPropertyPaths = function ($propertyPath) use (&$propertyPathsForWhichPropertyMappingShouldHappen) {
429 $propertyPathParts = explode('.', $propertyPath);
430 $accumulatedPropertyPathParts = [];
431 foreach ($propertyPathParts as $propertyPathPart) {
432 $accumulatedPropertyPathParts[] = $propertyPathPart;
433 $temporaryPropertyPath = implode('.', $accumulatedPropertyPathParts);
434 $propertyPathsForWhichPropertyMappingShouldHappen[$temporaryPropertyPath] = $temporaryPropertyPath;
435 }
436 };
437
438 $value = null;
439
440 GeneralUtility::deprecationLog('EXT:form - calls for "onSubmit" are deprecated since TYPO3 v8 and will be removed in TYPO3 v9');
441 $page->onSubmit($this, $value, $requestArguments);
442
443 if (
444 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterSubmit'])
445 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterSubmit'])
446 ) {
447 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterSubmit'] as $className) {
448 $hookObj = GeneralUtility::makeInstance($className);
449 if (method_exists($hookObj, 'afterSubmit')) {
450 $value = $hookObj->afterSubmit(
451 $this,
452 $page,
453 $value,
454 $requestArguments
455 );
456 }
457 }
458 }
459
460 foreach ($page->getElementsRecursively() as $element) {
461 try {
462 $value = ArrayUtility::getValueByPath($requestArguments, $element->getIdentifier(), '.');
463 } catch (\RuntimeException $exception) {
464 $value = null;
465 }
466
467 GeneralUtility::deprecationLog('EXT:form - calls for "onSubmit" are deprecated since TYPO3 v8 and will be removed in TYPO3 v9');
468 $element->onSubmit($this, $value, $requestArguments);
469
470 if (
471 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterSubmit'])
472 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterSubmit'])
473 ) {
474 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterSubmit'] as $className) {
475 $hookObj = GeneralUtility::makeInstance($className);
476 if (method_exists($hookObj, 'afterSubmit')) {
477 $value = $hookObj->afterSubmit(
478 $this,
479 $element,
480 $value,
481 $requestArguments
482 );
483 }
484 }
485 }
486
487 $this->formState->setFormValue($element->getIdentifier(), $value);
488 $registerPropertyPaths($element->getIdentifier());
489 }
490
491 // The more parts the path has, the more early it is processed
492 usort($propertyPathsForWhichPropertyMappingShouldHappen, function ($a, $b) {
493 return substr_count($b, '.') - substr_count($a, '.');
494 });
495
496 $processingRules = $this->formDefinition->getProcessingRules();
497
498 foreach ($propertyPathsForWhichPropertyMappingShouldHappen as $propertyPath) {
499 if (isset($processingRules[$propertyPath])) {
500 $processingRule = $processingRules[$propertyPath];
501 $value = $this->formState->getFormValue($propertyPath);
502 try {
503 $value = $processingRule->process($value);
504 } catch (PropertyException $exception) {
505 throw new PropertyMappingException(
506 'Failed to process FormValue at "' . $propertyPath . '" from "' . gettype($value) . '" to "' . $processingRule->getDataType() . '"',
507 1480024933,
508 $exception
509 );
510 }
511 $result->forProperty($this->getIdentifier() . '.' . $propertyPath)->merge($processingRule->getProcessingMessages());
512 $this->formState->setFormValue($propertyPath, $value);
513 }
514 }
515
516 return $result;
517 }
518
519 /**
520 * Override the current page taken from the request, rendering the page with index $pageIndex instead.
521 *
522 * This is typically not needed in production code, but it is very helpful when displaying
523 * some kind of "preview" of the form.
524 *
525 * @param int $pageIndex
526 * @api
527 */
528 public function overrideCurrentPage(int $pageIndex)
529 {
530 $this->currentPage = $this->formDefinition->getPageByIndex($pageIndex);
531 }
532
533 /**
534 * Render this form.
535 *
536 * @return string|null rendered form
537 * @throws RenderingException
538 * @api
539 */
540 public function render()
541 {
542 if ($this->isAfterLastPage()) {
543 return $this->invokeFinishers();
544 }
545
546 $this->formState->setLastDisplayedPageIndex($this->currentPage->getIndex());
547
548 if ($this->formDefinition->getRendererClassName() === '') {
549 throw new RenderingException(sprintf('The form definition "%s" does not have a rendererClassName set.', $this->formDefinition->getIdentifier()), 1326095912);
550 }
551 $rendererClassName = $this->formDefinition->getRendererClassName();
552 $renderer = $this->objectManager->get($rendererClassName);
553 if (!($renderer instanceof RendererInterface)) {
554 throw new RenderingException(sprintf('The renderer "%s" des not implement RendererInterface', $rendererClassName), 1326096024);
555 }
556
557 $controllerContext = $this->getControllerContext();
558
559 $renderer->setControllerContext($controllerContext);
560 $renderer->setFormRuntime($this);
561 return $renderer->render($this);
562 }
563
564 /**
565 * Executes all finishers of this form
566 *
567 * @return string
568 */
569 protected function invokeFinishers(): string
570 {
571 $finisherContext = $this->objectManager->get(
572 FinisherContext::class,
573 $this,
574 $this->getControllerContext()
575 );
576
577 $output = '';
578 foreach ($this->formDefinition->getFinishers() as $finisher) {
579 $finisherOutput = $finisher->execute($finisherContext);
580 if (is_string($finisherOutput) && !empty($finisherOutput)) {
581 $output .= $finisherOutput;
582 }
583 if ($finisherContext->isCancelled()) {
584 break;
585 }
586 }
587
588 return $output;
589 }
590
591 /**
592 * @return string The identifier of underlying form
593 * @api
594 */
595 public function getIdentifier(): string
596 {
597 return $this->formDefinition->getIdentifier();
598 }
599
600 /**
601 * Get the request this object is bound to.
602 *
603 * This is mostly relevant inside Finishers, where you f.e. want to redirect
604 * the user to another page.
605 *
606 * @return Request the request this object is bound to
607 * @api
608 */
609 public function getRequest(): Request
610 {
611 return $this->request;
612 }
613
614 /**
615 * Get the response this object is bound to.
616 *
617 * This is mostly relevant inside Finishers, where you f.e. want to set response
618 * headers or output content.
619 *
620 * @return Response the response this object is bound to
621 * @api
622 */
623 public function getResponse(): Response
624 {
625 return $this->response;
626 }
627
628 /**
629 * Returns the currently selected page
630 *
631 * @return Page
632 * @api
633 */
634 public function getCurrentPage(): Page
635 {
636 return $this->currentPage;
637 }
638
639 /**
640 * Returns the previous page of the currently selected one or NULL if there is no previous page
641 *
642 * @return Page|null
643 * @api
644 */
645 public function getPreviousPage()
646 {
647 $previousPageIndex = $this->currentPage->getIndex() - 1;
648 if ($this->formDefinition->hasPageWithIndex($previousPageIndex)) {
649 return $this->formDefinition->getPageByIndex($previousPageIndex);
650 }
651 return null;
652 }
653
654 /**
655 * Returns the next page of the currently selected one or NULL if there is no next page
656 *
657 * @return Page|null
658 * @api
659 */
660 public function getNextPage()
661 {
662 $nextPageIndex = $this->currentPage->getIndex() + 1;
663 if ($this->formDefinition->hasPageWithIndex($nextPageIndex)) {
664 return $this->formDefinition->getPageByIndex($nextPageIndex);
665 }
666 return null;
667 }
668
669 /**
670 * @return ControllerContext
671 */
672 protected function getControllerContext(): ControllerContext
673 {
674 $uriBuilder = $this->objectManager->get(UriBuilder::class);
675 $uriBuilder->setRequest($this->request);
676 $controllerContext = $this->objectManager->get(ControllerContext::class);
677 $controllerContext->setRequest($this->request);
678 $controllerContext->setResponse($this->response);
679 $controllerContext->setArguments($this->objectManager->get(Arguments::class, []));
680 $controllerContext->setUriBuilder($uriBuilder);
681 return $controllerContext;
682 }
683
684 /**
685 * Abstract "type" of this Renderable. Is used during the rendering process
686 * to determine the template file or the View PHP class being used to render
687 * the particular element.
688 *
689 * @return string
690 * @api
691 */
692 public function getType(): string
693 {
694 return $this->formDefinition->getType();
695 }
696
697 /**
698 * @param string $identifier
699 * @return bool
700 * @internal
701 */
702 public function offsetExists($identifier)
703 {
704 if ($this->getElementValue($identifier) !== null) {
705 return true;
706 }
707
708 if (is_callable([$this, 'get' . ucfirst($identifier)])) {
709 return true;
710 }
711 if (is_callable([$this, 'has' . ucfirst($identifier)])) {
712 return true;
713 }
714 if (is_callable([$this, 'is' . ucfirst($identifier)])) {
715 return true;
716 }
717 if (property_exists($this, $identifier)) {
718 $propertyReflection = new PropertyReflection($this, $identifier);
719 return $propertyReflection->isPublic();
720 }
721
722 return false;
723 }
724
725 /**
726 * @param string $identifier
727 * @return mixed
728 * @internal
729 */
730 public function offsetGet($identifier)
731 {
732 if ($this->getElementValue($identifier) !== null) {
733 return $this->getElementValue($identifier);
734 }
735 $getterMethodName = 'get' . ucfirst($identifier);
736 if (is_callable([$this, $getterMethodName])) {
737 return $this->{$getterMethodName}();
738 }
739 return null;
740 }
741
742 /**
743 * @param string $identifier
744 * @param mixed $value
745 * @internal
746 */
747 public function offsetSet($identifier, $value)
748 {
749 $this->formState->setFormValue($identifier, $value);
750 }
751
752 /**
753 * @param string $identifier
754 * @internal
755 */
756 public function offsetUnset($identifier)
757 {
758 $this->formState->setFormValue($identifier, null);
759 }
760
761 /**
762 * Returns the value of the specified element
763 *
764 * @param string $identifier
765 * @return mixed
766 * @api
767 */
768 public function getElementValue(string $identifier)
769 {
770 $formValue = $this->formState->getFormValue($identifier);
771 if ($formValue !== null) {
772 return $formValue;
773 }
774 return $this->formDefinition->getElementDefaultValueByIdentifier($identifier);
775 }
776
777 /**
778 * @return array<Page> The Form's pages in the correct order
779 * @api
780 */
781 public function getPages(): array
782 {
783 return $this->formDefinition->getPages();
784 }
785
786 /**
787 * @return FormState
788 * @internal
789 */
790 public function getFormState(): FormState
791 {
792 return $this->formState;
793 }
794
795 /**
796 * Get all rendering options
797 *
798 * @return array associative array of rendering options
799 * @api
800 */
801 public function getRenderingOptions(): array
802 {
803 return $this->formDefinition->getRenderingOptions();
804 }
805
806 /**
807 * Get the renderer class name to be used to display this renderable;
808 * must implement RendererInterface
809 *
810 * @return string the renderer class name
811 * @api
812 */
813 public function getRendererClassName(): string
814 {
815 return $this->formDefinition->getRendererClassName();
816 }
817
818 /**
819 * Get the label which shall be displayed next to the form element
820 *
821 * @return string
822 * @api
823 */
824 public function getLabel(): string
825 {
826 return $this->formDefinition->getLabel();
827 }
828
829 /**
830 * Get the template name of the renderable
831 *
832 * @return string
833 * @api
834 */
835 public function getTemplateName(): string
836 {
837 return $this->formDefinition->getTemplateName();
838 }
839
840 /**
841 * Get the underlying form definition from the runtime
842 *
843 * @return FormDefinition
844 * @api
845 */
846 public function getFormDefinition(): FormDefinition
847 {
848 return $this->formDefinition;
849 }
850
851 /**
852 * This is a callback that is invoked by the Renderer before the corresponding element is rendered.
853 * Use this to access previously submitted values and/or modify the $formRuntime before an element
854 * is outputted to the browser.
855 *
856 * @param FormRuntime $formRuntime
857 * @api
858 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
859 */
860 public function beforeRendering(FormRuntime $formRuntime)
861 {
862 GeneralUtility::logDeprecatedFunction();
863 }
864
865 /**
866 * @return TypoScriptFrontendController
867 */
868 protected function getTypoScriptFrontendController()
869 {
870 return $GLOBALS['TSFE'];
871 }
872 }