|
1
|
|
|
<?php |
|
2
|
|
|
namespace DERHANSEN\SfEventMgt\ViewHelpers\Registration\Field; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please read the |
|
8
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field; |
|
12
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* PrefillField ViewHelper for registration fields |
|
16
|
|
|
* |
|
17
|
|
|
* @author Torben Hansen <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class PrefillFieldViewHelper extends AbstractViewHelper |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Initialize arguments |
|
23
|
|
|
*/ |
|
24
|
|
|
public function initializeArguments() |
|
25
|
|
|
{ |
|
26
|
|
|
parent::initializeArguments(); |
|
27
|
|
|
$this->registerArgument('registrationField', 'object', 'The registrationField object', true); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns a string to be used as prefill value for the given registration field (type=input). If the form |
|
32
|
|
|
* has already been submitted, the submitted value for the field is returned. |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function render() |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var Field $registrationField */ |
|
39
|
|
|
$registrationField = $this->arguments['registrationField']; |
|
40
|
|
|
// If mapping errors occured for form, return value that has been submitted |
|
41
|
|
|
$originalRequest = $this->getRequest()->getOriginalRequest(); |
|
42
|
|
|
if ($originalRequest) { |
|
43
|
|
|
return $this->getFieldValueFromArguments($originalRequest->getArguments(), $registrationField->getUid()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $registrationField->getDefaultValue(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the submitted value for the given field uid |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $submittedValues |
|
53
|
|
|
* @param int $fieldUid |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function getFieldValueFromArguments($submittedValues, $fieldUid) |
|
57
|
|
|
{ |
|
58
|
|
|
$result = ''; |
|
59
|
|
|
foreach ($submittedValues['registration']['fieldValues'] as $fieldValue) { |
|
60
|
|
|
if ((int)$fieldValue['field'] === $fieldUid) { |
|
61
|
|
|
$result = $fieldValue['value']; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
return $result; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Shortcut for retrieving the request from the controller context |
|
69
|
|
|
* |
|
70
|
|
|
* @return \TYPO3\CMS\Extbase\Mvc\Request |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getRequest() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->renderingContext->getControllerContext()->getRequest(); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: