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 DERHANSEN\SfEventMgt\Domain\Model\Registration\FieldValue; |
13
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
14
|
|
|
use TYPO3\CMS\Extbase\Property\PropertyMapper; |
15
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* PrefillMultiValueField ViewHelper for registration fields |
19
|
|
|
* |
20
|
|
|
* @author Torben Hansen <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class PrefillMultiValueFieldViewHelper extends AbstractViewHelper |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var \TYPO3\CMS\Extbase\Property\PropertyMapper |
26
|
|
|
*/ |
27
|
|
|
protected $propertyMapper = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param PropertyMapper $propertyMapper |
31
|
|
|
*/ |
32
|
|
|
public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper) |
33
|
|
|
{ |
34
|
|
|
$this->propertyMapper = $propertyMapper; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initialize arguments |
39
|
|
|
*/ |
40
|
|
|
public function initializeArguments() |
41
|
|
|
{ |
42
|
|
|
parent::initializeArguments(); |
43
|
|
|
$this->registerArgument('registrationField', 'object', 'RegistrationField', true); |
44
|
|
|
$this->registerArgument('currentValue', 'strong', 'Current value', true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns, if the given $currentValue is selected/checked for the given registration field |
49
|
|
|
* If no originalRequest exist (form is not submitted), true is returned if the given $currentValue |
50
|
|
|
* matches the default value of the field |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function render() |
55
|
|
|
{ |
56
|
|
|
/** @var Field $registrationField */ |
57
|
|
|
$registrationField = $this->arguments['registrationField']; |
58
|
|
|
$currentValue = $this->arguments['currentValue']; |
59
|
|
|
|
60
|
|
|
// If mapping errors occured for form, return value that has been submitted |
61
|
|
|
$originalRequest = $this->getRequest()->getOriginalRequest(); |
62
|
|
|
if ($originalRequest) { |
63
|
|
|
return $this->getFieldValueFromArguments( |
64
|
|
|
$originalRequest->getArguments(), |
65
|
|
|
$registrationField->getUid(), |
66
|
|
|
$currentValue |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->getFieldValueFromDefaultProperty($registrationField, $currentValue); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the submitted value for the given field uid |
75
|
|
|
* |
76
|
|
|
* @param array $submittedValues |
77
|
|
|
* @param int $fieldUid |
78
|
|
|
* @param string $currentValue |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
protected function getFieldValueFromArguments($submittedValues, $fieldUid, $currentValue) |
82
|
|
|
{ |
83
|
|
|
$result = false; |
84
|
|
|
foreach ($submittedValues['registration']['fieldValues'] as $fieldValueArray) { |
85
|
|
|
/** @var FieldValue $fieldValue */ |
86
|
|
|
$fieldValue = $this->propertyMapper->convert($fieldValueArray, FieldValue::class); |
87
|
|
|
if ($fieldValue->getField()->getUid() === $fieldUid) { |
88
|
|
|
$result = $this->isGivenValueSelected($fieldValue, $currentValue); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param FieldValue $fieldValue |
97
|
|
|
* @param string $currentValue |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
protected function isGivenValueSelected($fieldValue, $currentValue) |
101
|
|
|
{ |
102
|
|
|
$fieldValue = $fieldValue->getValue(); |
103
|
|
|
if (is_array($fieldValue)) { |
104
|
|
|
return in_array($currentValue, $fieldValue); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $currentValue === $fieldValue; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Registration\Field $registrationField |
112
|
|
|
* @param string $currentValue |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
protected function getFieldValueFromDefaultProperty($registrationField, $currentValue) |
116
|
|
|
{ |
117
|
|
|
$defaultValues = GeneralUtility::trimExplode(',', $registrationField->getDefaultValue()); |
118
|
|
|
|
119
|
|
|
return in_array($currentValue, $defaultValues); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Shortcut for retrieving the request from the controller context |
124
|
|
|
* |
125
|
|
|
* @return \TYPO3\CMS\Extbase\Mvc\Request |
126
|
|
|
*/ |
127
|
|
|
protected function getRequest() |
128
|
|
|
{ |
129
|
|
|
return $this->renderingContext->getControllerContext()->getRequest(); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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: