1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace HDNET\Calendarize\ViewHelpers\Link; |
6
|
|
|
|
7
|
|
|
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder; |
8
|
|
|
|
9
|
|
|
class AbstractActionViewHelper extends AbstractLinkViewHelper |
10
|
|
|
{ |
11
|
|
|
protected $extensionName = 'Calendarize'; |
12
|
|
|
protected $pluginName = 'Calendar'; |
13
|
|
|
protected $controllerName = 'Calendar'; |
14
|
|
|
protected $actionName; |
15
|
|
|
|
16
|
|
|
public function initializeArguments() |
17
|
|
|
{ |
18
|
|
|
parent::initializeArguments(); |
19
|
|
|
|
20
|
|
|
$this->registerArgument('section', 'string', 'The anchor to be added to the URI', false); |
21
|
|
|
$this->registerArgument('pageUid', 'int', 'Target page', false); |
22
|
|
|
$this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute', false); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Render a link with action and controller. |
27
|
|
|
* |
28
|
|
|
* @param array $controllerArguments |
29
|
|
|
* @param null $pageUid |
30
|
|
|
* |
31
|
|
|
* @return mixed|string |
32
|
|
|
*/ |
33
|
|
|
public function renderExtbaseLink(array $controllerArguments = [], $pageUid = null) |
34
|
|
|
{ |
35
|
|
|
$absolute = $this->arguments['absolute'] ?? false; |
36
|
|
|
$pageUid = $pageUid ?? $this->getPageUid($this->arguments['pageUid'] ?? ''); |
37
|
|
|
|
38
|
|
|
$section = $this->arguments['section'] ?? ''; |
39
|
|
|
|
40
|
|
|
/** @var UriBuilder $uriBuilder */ |
41
|
|
|
$uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder(); |
|
|
|
|
42
|
|
|
// $uriBuilder = $this->renderingContext->getUriBuilder(); // Typo3 11 and later |
43
|
|
|
$this->lastHref = $uriBuilder->reset() |
44
|
|
|
->setTargetPageUid($pageUid) |
45
|
|
|
->setSection($section) |
46
|
|
|
->setCreateAbsoluteUri($absolute) |
47
|
|
|
->uriFor( |
48
|
|
|
$this->actionName, |
49
|
|
|
$controllerArguments, |
50
|
|
|
$this->controllerName, |
51
|
|
|
$this->extensionName, |
52
|
|
|
$this->pluginName |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
if ('' !== $this->lastHref) { |
56
|
|
|
$this->tag->addAttribute('href', $this->lastHref); |
57
|
|
|
$this->tag->setContent($this->renderChildren()); |
58
|
|
|
$this->tag->forceClosingTag(true); |
59
|
|
|
$result = $this->tag->render(); |
60
|
|
|
} else { |
61
|
|
|
$result = $this->renderChildren(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
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: