Passed
Push — typo3_11 ( 05e136 )
by Torben
43:39
created

PrefillViewHelper::render()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 3
nop 0
dl 0
loc 18
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.txt file that was distributed with this source code.
8
 */
9
10
namespace DERHANSEN\SfEventMgt\ViewHelpers;
11
12
/**
13
 * Prefill ViewHelper
14
 */
15
class PrefillViewHelper extends AbstractPrefillViewHelper
16
{
17
    /**
18
     * Initialize arguments
19
     */
20
    public function initializeArguments()
21
    {
22
        parent::initializeArguments();
23
        $this->registerArgument('fieldname', 'string', 'FieldName', true);
24
        $this->registerArgument('prefillSettings', 'array', 'PrefillSettings', false, []);
25
    }
26
27
    /**
28
     * If the current field is available in POST data of the current request, return the value, otherwise
29
     * a property from fe_user (if logged in and if the given field is configured to be prefilled) is returned.
30
     *
31
     * @return string
32
     */
33
    public function render()
34
    {
35
        $fieldname = $this->arguments['fieldname'];
36
        $prefillSettings = $this->arguments['prefillSettings'];
37
38
        $request = $this->renderingContext->getRequest();
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on TYPO3Fluid\Fluid\Core\Re...nderingContextInterface. It seems like you code against a sub-type of TYPO3Fluid\Fluid\Core\Re...nderingContextInterface such as TYPO3\CMS\Fluid\Core\Rendering\RenderingContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        /** @scrutinizer ignore-call */ 
39
        $request = $this->renderingContext->getRequest();
Loading history...
39
        $registrationData = $request->getParsedBody()[$this->getPluginNamespace($request)] ?? [];
40
        if (isset($registrationData['registration'][$fieldname])) {
41
            return $registrationData['registration'][$fieldname];
42
        }
43
44
        if (!isset($GLOBALS['TSFE']) || !$GLOBALS['TSFE']->fe_user->user || empty($prefillSettings) ||
45
            !array_key_exists($fieldname, $prefillSettings)
46
        ) {
47
            return '';
48
        }
49
50
        return (string)($GLOBALS['TSFE']->fe_user->user[$prefillSettings[$fieldname]]);
51
    }
52
}
53