-
Notifications
You must be signed in to change notification settings - Fork 638
/
ConfirmationFinisher.php
159 lines (140 loc) · 5.21 KB
/
ConfirmationFinisher.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
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Form\Domain\Finishers;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
use TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
/**
* A finisher that outputs a given text
*
* Options:
*
* - message: A hard-coded message to be rendered
* - contentElementUid: A content element uid to be rendered
*
* Usage:
* //...
* $confirmationFinisher = $this->objectManager->get(ConfirmationFinisher::class);
* $confirmationFinisher->setOptions(
* [
* 'message' => 'foo',
* ]
* );
* $formDefinition->addFinisher($confirmationFinisher);
* // ...
*
* Scope: frontend
*/
class ConfirmationFinisher extends AbstractFinisher
{
/**
* @var array
*/
protected $defaultOptions = [
'message' => 'The form has been submitted.',
'contentElementUid' => 0,
'typoscriptObjectPath' => 'lib.tx_form.contentElementRendering'
];
/**
* @var array
*/
protected $typoScriptSetup = [];
/**
* @var ConfigurationManagerInterface
*/
protected $configurationManager;
/**
* @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
*/
public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
{
$this->configurationManager = $configurationManager;
$this->typoScriptSetup = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
}
/**
* @param ContentObjectRenderer $contentObjectRenderer
*/
public function injectContentObjectRenderer(ContentObjectRenderer $contentObjectRenderer)
{
$this->contentObjectRenderer = $contentObjectRenderer;
}
/**
* Executes this finisher
*
* @see AbstractFinisher::execute()
* @return string
*
* @throws FinisherException
*/
protected function executeInternal()
{
$contentElementUid = $this->parseOption('contentElementUid');
$typoscriptObjectPath = $this->parseOption('typoscriptObjectPath');
if (!empty($contentElementUid)) {
$pathSegments = GeneralUtility::trimExplode('.', $typoscriptObjectPath);
$lastSegment = array_pop($pathSegments);
$setup = $this->typoScriptSetup;
foreach ($pathSegments as $segment) {
if (!array_key_exists($segment . '.', $setup)) {
throw new FinisherException(
sprintf('TypoScript object path "%s" does not exist', $typoscriptObjectPath),
1489238980
);
}
$setup = $setup[$segment . '.'];
}
$this->contentObjectRenderer->start([$contentElementUid], '');
$this->contentObjectRenderer->setCurrentVal((string)$contentElementUid);
$message = $this->contentObjectRenderer->cObjGetSingle($setup[$lastSegment], $setup[$lastSegment . '.']);
} else {
$message = $this->parseOption('message');
}
$standaloneView = $this->initializeStandaloneView(
$this->finisherContext->getFormRuntime()
);
$standaloneView->assign('message', $message);
return $standaloneView->render();
}
/**
* @param FormRuntime $formRuntime
* @return StandaloneView
* @throws FinisherException
*/
protected function initializeStandaloneView(FormRuntime $formRuntime): StandaloneView
{
$standaloneView = $this->objectManager->get(StandaloneView::class);
if (!isset($this->options['templateName'])) {
throw new FinisherException(
'The option "templateName" must be set for the ConfirmationFinisher.',
1521573955
);
}
$standaloneView->setTemplate($this->options['templateName']);
$standaloneView->getTemplatePaths()->fillFromConfigurationArray($this->options);
if (isset($this->options['variables']) && is_array($this->options['variables'])) {
$standaloneView->assignMultiple($this->options['variables']);
}
$standaloneView->assign('form', $formRuntime);
$standaloneView->assign('finisherVariableProvider', $this->finisherContext->getFinisherVariableProvider());
$standaloneView->getRenderingContext()
->getViewHelperVariableContainer()
->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime);
return $standaloneView;
}
}