[FEATURE] EXT:form - Add the "Confirmation" finisher to the form editor
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Finishers / ConfirmationFinisher.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Finishers;
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\GeneralUtility;
21 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
22 use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
23 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
24
25 /**
26 * A simple finisher that outputs a given text
27 *
28 * Options:
29 *
30 * - message: A hard-coded message to be rendered
31 *
32 * Usage:
33 * //...
34 * $confirmationFinisher = $this->objectManager->get(ConfirmationFinisher::class);
35 * $confirmationFinisher->setOptions(
36 * [
37 * 'message' => 'foo',
38 * ]
39 * );
40 * $formDefinition->addFinisher($confirmationFinisher);
41 * // ...
42 *
43 * Scope: frontend
44 */
45 class ConfirmationFinisher extends AbstractFinisher
46 {
47
48 /**
49 * @var array
50 */
51 protected $defaultOptions = [
52 'message' => 'The form has been submitted.',
53 'contentElementUid' => 0,
54 'typoscriptObjectPath' => 'lib.tx_form.contentElementRendering'
55 ];
56
57 /**
58 * @var array
59 */
60 protected $typoScriptSetup = [];
61
62 /**
63 * @var ConfigurationManagerInterface
64 */
65 protected $configurationManager;
66
67 /**
68 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
69 * @return void
70 */
71 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
72 {
73 $this->configurationManager = $configurationManager;
74 $this->typoScriptSetup = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
75 }
76
77 /**
78 * @param ContentObjectRenderer $contentObjectRenderer
79 */
80 public function injectContentObjectRenderer(ContentObjectRenderer $contentObjectRenderer)
81 {
82 $this->contentObjectRenderer = $contentObjectRenderer;
83 }
84
85 /**
86 * Executes this finisher
87 * @see AbstractFinisher::execute()
88 *
89 * @throws FinisherException
90 */
91 protected function executeInternal()
92 {
93 $formRuntime = $this->finisherContext->getFormRuntime();
94
95 $contentElementUid = (int)$this->parseOption('contentElementUid');
96 $typoscriptObjectPath = $this->parseOption('typoscriptObjectPath');
97 if ($contentElementUid > 0) {
98 $pathSegments = GeneralUtility::trimExplode('.', $typoscriptObjectPath);
99 $lastSegment = array_pop($pathSegments);
100 $setup = $this->typoScriptSetup;
101 foreach ($pathSegments as $segment) {
102 if (!array_key_exists(($segment . '.'), $setup)) {
103 throw new FinisherException(
104 sprintf('TypoScript object path "%s" does not exist', $typoscriptObjectPath),
105 1489238980
106 );
107 }
108 $setup = $setup[$segment . '.'];
109 }
110 $this->contentObjectRenderer->start([$contentElementUid], '');
111 $this->contentObjectRenderer->setCurrentVal((string)$contentElementUid);
112 $message = $this->contentObjectRenderer->cObjGetSingle($setup[$lastSegment], $setup[$lastSegment . '.']);
113 } else {
114 $message = $this->parseOption('message');
115 }
116
117 $formRuntime->getResponse()->setContent($message);
118 }
119 }