1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\ViewHelpers\Step; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService; |
17
|
|
|
use Romm\Formz\Service\ViewHelper\Form\FormViewHelperService; |
18
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext; |
19
|
|
|
use TYPO3\CMS\Extbase\Mvc\Web\Request; |
20
|
|
|
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper; |
21
|
|
|
|
22
|
|
|
class PreviousLinkViewHelper extends AbstractFormFieldViewHelper |
23
|
|
|
{ |
24
|
|
|
const PREVIOUS_LINK_PARAMETER = 'substepsPrevious'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $tagName = 'input'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FormViewHelperService |
33
|
|
|
*/ |
34
|
|
|
protected $formService; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ControllerContext |
38
|
|
|
*/ |
39
|
|
|
protected $controllerContext; |
40
|
|
|
|
41
|
|
|
public function initializeArguments() |
42
|
|
|
{ |
43
|
|
|
parent::initializeArguments(); |
44
|
|
|
$this->registerUniversalTagAttributes(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function render() |
48
|
|
|
{ |
49
|
|
|
if (!$this->controllerContext) { |
50
|
|
|
$this->controllerContext = $this->renderingContext->getControllerContext(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/* |
54
|
|
|
* First, we check if this view helper is called from within the |
55
|
|
|
* `FormViewHelper`, because it would not make sense anywhere else. |
56
|
|
|
*/ |
57
|
|
|
if (false === $this->formService->formContextExists()) { |
58
|
|
|
// @todo |
59
|
|
|
// throw ContextNotFoundException::substepViewHelperFormContextNotFound(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$formObject = $this->formService->getFormObject(); |
63
|
|
|
$formDefinition = $formObject->getDefinition(); |
64
|
|
|
|
65
|
|
|
if (false === $formDefinition->hasSteps()) { |
66
|
|
|
throw new \Exception('todo'); // @todo |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @var Request $request */ |
70
|
|
|
$request = $this->controllerContext->getRequest(); |
71
|
|
|
$currentStep = $formObject->fetchCurrentStep($request)->getCurrentStep(); |
72
|
|
|
$stepDefinition = StepMiddlewareService::get()->getStepDefinition($currentStep); |
73
|
|
|
|
74
|
|
|
// @todo handle previous steps depth |
75
|
|
|
if (!$stepDefinition->hasPreviousDefinition() |
76
|
|
|
&& !$currentStep->hasSubsteps() |
77
|
|
|
) { |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->tag->addAttribute('type', 'submit'); |
82
|
|
|
$this->tag->addAttribute('value', $this->getValueAttribute()); |
83
|
|
|
$this->tag->addAttribute('name', $this->prefixFieldName(PreviousLinkViewHelper::PREVIOUS_LINK_PARAMETER)); |
84
|
|
|
$this->tag->addAttribute('fz-previous-link', true); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return $this->tag->render(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param FormViewHelperService $service |
91
|
|
|
*/ |
92
|
|
|
public function injectFormService(FormViewHelperService $service) |
93
|
|
|
{ |
94
|
|
|
$this->formService = $service; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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: