|
1
|
|
|
<?php |
|
2
|
|
|
namespace DERHANSEN\SfEventMgt\ViewHelpers; |
|
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 TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Prefill ViewHelper |
|
15
|
|
|
* |
|
16
|
|
|
* @author Torben Hansen <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class PrefillViewHelper extends AbstractViewHelper |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Initialize arguments |
|
22
|
|
|
*/ |
|
23
|
|
|
public function initializeArguments() |
|
24
|
|
|
{ |
|
25
|
|
|
parent::initializeArguments(); |
|
26
|
|
|
$this->registerArgument('fieldname', 'string', 'FieldName', true); |
|
27
|
|
|
$this->registerArgument('prefillSettings', 'array', 'PrefillSettings', false, []); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns a property from fe_user (if logged in and if the given field is |
|
32
|
|
|
* configured to be prefilled) |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
8 |
|
public function render() |
|
37
|
|
|
{ |
|
38
|
8 |
|
$fieldname = $this->arguments['fieldname']; |
|
39
|
8 |
|
$prefillSettings = $this->arguments['prefillSettings']; |
|
40
|
1 |
|
$piVars = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('tx_sfeventmgt_pievent'); |
|
41
|
|
|
if (isset($piVars['registration'][$fieldname]) && $piVars['registration'][$fieldname] !== '') { |
|
42
|
7 |
|
return $piVars['registration'][$fieldname]; |
|
43
|
4 |
|
} |
|
44
|
7 |
|
if (!isset($GLOBALS['TSFE']) || !$GLOBALS['TSFE']->fe_user->user || empty($prefillSettings) || |
|
45
|
4 |
|
!array_key_exists($fieldname, $prefillSettings) |
|
46
|
|
|
) { |
|
47
|
|
|
return ''; |
|
48
|
3 |
|
} |
|
49
|
3 |
|
// If mapping errors occured for form, return value that has been submitted |
|
50
|
1 |
|
$originalRequest = $this->getRequest()->getOriginalRequest(); |
|
51
|
1 |
|
if ($originalRequest) { |
|
52
|
|
|
$submittedValues = $originalRequest->getArguments(); |
|
53
|
2 |
|
return $submittedValues['registration'][$fieldname]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return strval($GLOBALS['TSFE']->fe_user->user[$prefillSettings[$fieldname]]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Shortcut for retrieving the request from the controller context |
|
61
|
|
|
* |
|
62
|
|
|
* @return \TYPO3\CMS\Extbase\Mvc\Request |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getRequest() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->renderingContext->getControllerContext()->getRequest(); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
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: