Skip to content

Commit b24369d

Browse files
authored
Merge pull request #66 from helhum/fix-widget-vhs
2 parents 5ee7764 + 367b9ee commit b24369d

5 files changed

Lines changed: 166 additions & 261 deletions

File tree

Classes/Configuration/RecordRenderingConfigurationBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(RenderingContext $renderingContext)
4141
* @return string[]
4242
*
4343
*/
44-
public function configurationFor(string $extensionName, string $pluginName, string $contextRecord = 'currentPage')
44+
public function configurationFor(string $extensionName, string $pluginName, string $contextRecord = 'currentPage'): array
4545
{
4646
list($tableName, $uid) = $this->resolveTableNameAndUidFromContextString($contextRecord);
4747
$pluginSignature = $this->buildPluginSignature($extensionName, $pluginName);
@@ -60,7 +60,7 @@ public function configurationFor(string $extensionName, string $pluginName, stri
6060
* @return string[]
6161
*
6262
*/
63-
public function configurationForPath(string $renderingPath, string $contextRecord = 'currentPage')
63+
public function configurationForPath(string $renderingPath, string $contextRecord = 'currentPage'): array
6464
{
6565
list($tableName, $uid) = $this->resolveTableNameAndUidFromContextString($contextRecord);
6666
return [
@@ -77,7 +77,7 @@ public function configurationForPath(string $renderingPath, string $contextRecor
7777
*
7878
* @return string[] table name as first and uid as second index of the array
7979
*/
80-
protected function resolveTableNameAndUidFromContextString(string $contextRecord)
80+
protected function resolveTableNameAndUidFromContextString(string $contextRecord): array
8181
{
8282
if ($contextRecord === 'currentPage') {
8383
$tableNameAndUid = ['pages', $this->renderingContext->getFrontendController()->id];

Classes/Uri/TyposcriptRenderingUri.php

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Helhum\TyposcriptRendering\Configuration\RecordRenderingConfigurationBuilder;
1919
use Helhum\TyposcriptRendering\Renderer\RenderingContext;
2020
use TYPO3\CMS\Core\Http\Uri;
21+
use TYPO3\CMS\Core\Utility\MathUtility;
22+
use TYPO3\CMS\Fluid\Core\Widget\WidgetRequest;
2123

2224
class TyposcriptRenderingUri extends Uri
2325
{
@@ -35,10 +37,20 @@ public function withViewHelperContext(ViewHelperContext $viewHelperContext): sel
3537
return $newUri;
3638
}
3739

40+
public function withWidgetContext(ViewHelperContext $viewHelperContext): self
41+
{
42+
$newUri = clone $this;
43+
$newUri->viewHelperContext = $viewHelperContext;
44+
$newUri->parseWidgetContext($viewHelperContext);
45+
46+
return $newUri;
47+
}
48+
3849
private function parseViewHelperContext(ViewHelperContext $viewHelperContext): void
3950
{
4051
$arguments = $viewHelperContext->getArguments();
4152
$controllerContext = $viewHelperContext->getControllerContext();
53+
$request = $controllerContext->getRequest();
4254

4355
$pluginName = $arguments['pluginName'] ?? null;
4456
$extensionName = $arguments['extensionName'] ?? null;
@@ -47,40 +59,42 @@ private function parseViewHelperContext(ViewHelperContext $viewHelperContext): v
4759
$renderingPath = $arguments['typoscriptObjectPath'] ?? null;
4860

4961
if ($pluginName === null) {
50-
$pluginName = $controllerContext->getRequest()->getPluginName();
62+
$pluginName = $request->getPluginName();
5163
}
5264
if ($extensionName === null) {
53-
$extensionName = $controllerContext->getRequest()->getControllerExtensionName();
65+
$extensionName = $request->getControllerExtensionName();
5466
}
5567
if ($contextRecord === 'current') {
5668
if (
57-
$pluginName !== $controllerContext->getRequest()->getPluginName()
58-
|| $extensionName !== $controllerContext->getRequest()->getControllerExtensionName()
69+
$pluginName !== $request->getPluginName()
70+
|| $extensionName !== $request->getControllerExtensionName()
5971
) {
6072
$contextRecord = 'currentPage';
6173
} else {
6274
$contextRecord = $viewHelperContext->getContentObject()->currentRecord;
6375
}
6476
}
65-
if ($renderingPath === null) {
66-
$renderingConfiguration = $this->buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord);
67-
} else {
77+
if (is_string($renderingPath)) {
6878
$renderingConfiguration = $this->buildConfigurationForPath($renderingPath, $contextRecord);
79+
} else {
80+
$renderingConfiguration = $this->buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord);
6981
}
7082
$additionalParams['tx_typoscriptrendering']['context'] = json_encode($renderingConfiguration);
7183

7284
$uriBuilder = $controllerContext->getUriBuilder();
7385
$uriBuilder->reset()
74-
->setTargetPageUid($arguments['pageUid'])
7586
->setUseCacheHash(true)
76-
->setSection($arguments['section'])
77-
->setFormat($arguments['format'])
78-
->setLinkAccessRestrictedPages($arguments['linkAccessRestrictedPages'])
87+
->setSection($arguments['section'] ?? '')
88+
->setFormat($arguments['format'] ?? 'html')
89+
->setLinkAccessRestrictedPages($arguments['linkAccessRestrictedPages'] ?? false)
7990
->setArguments($additionalParams)
80-
->setCreateAbsoluteUri($arguments['absolute'])
81-
->setAddQueryString($arguments['addQueryString'])
82-
->setAddQueryStringMethod($arguments['addQueryStringMethod'])
83-
->setArgumentsToBeExcludedFromQueryString($arguments['argumentsToBeExcludedFromQueryString']);
91+
->setCreateAbsoluteUri($arguments['absolute'] ?? false)
92+
->setAddQueryString($arguments['addQueryString'] ?? false)
93+
->setAddQueryStringMethod('GET')
94+
->setArgumentsToBeExcludedFromQueryString($arguments['argumentsToBeExcludedFromQueryString'] ?? []);
95+
if (MathUtility::canBeInterpretedAsInteger($arguments['pageUid'])) {
96+
$uriBuilder->setTargetPageUid((int)$arguments['pageUid']);
97+
}
8498

8599
$this->parseUri(
86100
$uriBuilder->uriFor(
@@ -94,6 +108,74 @@ private function parseViewHelperContext(ViewHelperContext $viewHelperContext): v
94108
);
95109
}
96110

111+
private function parseWidgetContext(ViewHelperContext $viewHelperContext): void
112+
{
113+
$arguments = $viewHelperContext->getArguments();
114+
$controllerContext = $viewHelperContext->getControllerContext();
115+
/** @var $request WidgetRequest $request */
116+
$request = $controllerContext->getRequest();
117+
if (!$request instanceof WidgetRequest) {
118+
throw new \RuntimeException('Called from wrong context', 1589401907);
119+
}
120+
121+
$pluginName = $arguments['pluginName'] ?? null;
122+
$extensionName = $arguments['extensionName'] ?? null;
123+
$contextRecord = $arguments['contextRecord'];
124+
$additionalParams = $arguments['additionalParams'];
125+
$renderingPath = $arguments['typoscriptObjectPath'] ?? null;
126+
127+
if ($pluginName === null) {
128+
$pluginName = $request->getWidgetContext()->getParentPluginName();
129+
}
130+
if ($extensionName === null) {
131+
$extensionName = $request->getWidgetContext()->getParentExtensionName();
132+
}
133+
if ($contextRecord === 'current') {
134+
if (
135+
$pluginName !== $request->getWidgetContext()->getParentPluginName()
136+
|| $extensionName !== $request->getWidgetContext()->getParentExtensionName()
137+
) {
138+
$contextRecord = 'currentPage';
139+
} else {
140+
$contextRecord = $viewHelperContext->getContentObject()->currentRecord;
141+
}
142+
}
143+
if (is_string($renderingPath)) {
144+
$renderingConfiguration = $this->buildConfigurationForPath($renderingPath, $contextRecord);
145+
} else {
146+
$renderingConfiguration = $this->buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord);
147+
}
148+
// @deprecated ajax set to false is deprecated
149+
if ($arguments['ajax']) {
150+
$additionalParams['tx_typoscriptrendering']['context'] = json_encode($renderingConfiguration);
151+
}
152+
153+
// adding the widget prefix for the arguments, use them together with the additionalParams
154+
$additionalParams[$request->getArgumentPrefix()] = $arguments['arguments'];
155+
156+
$uriBuilder = $controllerContext->getUriBuilder();
157+
$uriBuilder->reset()
158+
->setUseCacheHash(true)
159+
->setSection($arguments['section'] ?? '')
160+
->setFormat($arguments['format'] ?? 'html')
161+
->setLinkAccessRestrictedPages($arguments['linkAccessRestrictedPages'] ?? false)
162+
->setArguments($additionalParams)
163+
->setCreateAbsoluteUri($arguments['absolute'] ?? false)
164+
->setAddQueryString($arguments['addQueryString'] ?? false)
165+
->setAddQueryStringMethod('GET')
166+
->setArgumentsToBeExcludedFromQueryString($arguments['argumentsToBeExcludedFromQueryString'] ?? []);
167+
if (MathUtility::canBeInterpretedAsInteger($arguments['pageUid'])) {
168+
$uriBuilder->setTargetPageUid((int)$arguments['pageUid']);
169+
}
170+
171+
$uri = $uriBuilder->build();
172+
173+
$this->parseUri(
174+
$uri,
175+
$renderingPath !== null
176+
);
177+
}
178+
97179
/**
98180
* @param string $extensionName
99181
* @param string $pluginName

Classes/ViewHelpers/Uri/ActionViewHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
use Helhum\TyposcriptRendering\Uri\TyposcriptRenderingUri;
1818
use Helhum\TyposcriptRendering\Uri\ViewHelperContext;
19-
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
2019
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20+
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
2121
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
2222

2323
/**

Classes/ViewHelpers/Widget/LinkViewHelper.php

Lines changed: 32 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@
1414
*
1515
*/
1616

17-
use Helhum\TyposcriptRendering\Configuration\RecordRenderingConfigurationBuilder;
18-
use Helhum\TyposcriptRendering\Renderer\RenderingContext;
17+
use Helhum\TyposcriptRendering\Uri\TyposcriptRenderingUri;
18+
use Helhum\TyposcriptRendering\Uri\ViewHelperContext;
19+
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
1920

20-
class LinkViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
21+
class LinkViewHelper extends AbstractTagBasedViewHelper
2122
{
2223
/**
2324
* @var string
2425
*/
2526
protected $tagName = 'a';
2627

27-
/**
28-
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
29-
* @inject
30-
*/
31-
protected $configurationManager;
32-
3328
/**
3429
* Initialize arguments
3530
*
@@ -39,133 +34,50 @@ class LinkViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedVi
3934
*/
4035
public function initializeArguments()
4136
{
37+
parent::initializeArguments();
4238
$this->registerUniversalTagAttributes();
4339
$this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
4440
$this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
4541
$this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
4642
$this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
43+
// @deprecated
44+
$this->registerArgument('useCacheHash', 'bool', 'Deprecated: True whether the cache hash should be appended to the URL', false, null);
4745
$this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
48-
}
46+
$this->registerArgument('action', 'string', 'Target action');
47+
$this->registerArgument('arguments', 'array', 'Arguments', false, []);
48+
$this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
49+
$this->registerArgument('format', 'string', 'The requested format, e.g. ".html', false, '');
50+
$this->registerArgument('ajax', 'bool', 'TRUE if the URI should be to an AJAX widget, FALSE otherwise.', false, true);
4951

50-
/**
51-
* Render the Uri.
52-
*
53-
* @param string $pluginName
54-
* @param string $extensionName
55-
* @param string $action Target action
56-
* @param array $arguments Arguments
57-
* @param string $section The anchor to be added to the URI
58-
* @param string $format The requested format, e.g. ".html
59-
* @param bool $ajax true if the URI should be to an Ajax widget, false otherwise.
60-
* @param string $contextRecord The record that the rendering should depend upon. e.g. current (default: record is fetched from current Extbase plugin), tt_content:12 (tt_content record with uid 12), pages:15 (pages record with uid 15), 'currentPage' record of current page
61-
*
62-
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
63-
* @return string The rendered link
64-
*
65-
*/
66-
public function render($pluginName, $extensionName, $action = null, array $arguments = [], $section = '', $format = '', $ajax = true, $contextRecord = 'current')
67-
{
68-
if ($ajax === true) {
69-
$uri = $this->getAjaxUri();
70-
} else {
71-
$uri = $this->getWidgetUri();
72-
}
73-
$this->tag->addAttribute('href', $uri);
74-
$this->tag->setContent($this->renderChildren());
75-
return $this->tag->render();
52+
$this->registerArgument('extensionName', 'string', 'Target Extension Name (without "tx_" prefix and no underscores). If NULL the current extension name is used');
53+
$this->registerArgument('pluginName', 'string', 'Target plugin. If empty, the current plugin name is used');
54+
$this->registerArgument('contextRecord', 'string', 'The record that the rendering should depend upon. e.g. current (default: record is fetched from current Extbase plugin), tt_content:12 (tt_content record with uid 12), pages:15 (pages record with uid 15), \'currentPage\' record of current page', false, 'current');
55+
$this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = TRUE', false, []);
7656
}
7757

7858
/**
79-
* Gets the URI for an Ajax Request.
80-
*
81-
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
82-
* @return string the Ajax URI
59+
* Render the link.
8360
*
61+
* @return string The rendered link
8462
*/
85-
protected function getAjaxUri()
86-
{
87-
$pluginName = $this->arguments['pluginName'];
88-
$extensionName = $this->arguments['extensionName'];
89-
$contextRecord = $this->arguments['contextRecord'];
90-
$arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : [];
91-
if ($contextRecord === 'current') {
92-
if (
93-
$pluginName !== $this->controllerContext->getRequest()->getPluginName()
94-
|| $extensionName !== $this->controllerContext->getRequest()->getControllerExtensionName()
95-
) {
96-
$contextRecord = 'currentPage';
97-
} else {
98-
$contextRecord = $this->configurationManager->getContentObject()->currentRecord;
99-
}
100-
}
101-
$renderingConfiguration = $this->buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord);
102-
$additionalParams['tx_typoscriptrendering']['context'] = json_encode($renderingConfiguration);
103-
104-
$uriBuilder = $this->controllerContext->getUriBuilder();
105-
$argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
106-
107-
$uriBuilder->reset()
108-
->setArguments(array_merge([$argumentPrefix => $arguments], $additionalParams))
109-
->setSection($this->arguments['section'])
110-
->setAddQueryString(true)
111-
->setArgumentsToBeExcludedFromQueryString([$argumentPrefix, 'cHash'])
112-
->setFormat($this->arguments['format'])
113-
->setUseCacheHash(true);
114-
115-
// TYPO3 6.0 compatibility check:
116-
if (method_exists($uriBuilder, 'setAddQueryStringMethod')) {
117-
$uriBuilder->setAddQueryStringMethod($this->arguments['addQueryStringMethod']);
118-
}
119-
120-
return $uriBuilder->build();
121-
}
122-
123-
/**
124-
* Gets the URI for a non-Ajax Request.
125-
*
126-
* @return string the Widget URI
127-
*/
128-
protected function getWidgetUri()
63+
public function render()
12964
{
130-
$uriBuilder = $this->controllerContext->getUriBuilder();
131-
$argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
132-
$arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : [];
133-
if ($this->hasArgument('action')) {
134-
$arguments['action'] = $this->arguments['action'];
135-
}
136-
if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
137-
$arguments['format'] = $this->arguments['format'];
138-
}
139-
if ($this->hasArgument('addQueryStringMethod') && $this->arguments['addQueryStringMethod'] !== '') {
140-
$arguments['addQueryStringMethod'] = $this->arguments['addQueryStringMethod'];
141-
}
142-
$uriBuilder->reset()
143-
->setArguments([$argumentPrefix => $arguments])
144-
->setSection($this->arguments['section'])
145-
->setAddQueryString(true)
146-
->setArgumentsToBeExcludedFromQueryString([$argumentPrefix, 'cHash'])
147-
->setFormat($this->arguments['format']);
65+
$arguments = $this->arguments;
14866

149-
// TYPO3 6.0 compatibility check:
150-
if (method_exists($uriBuilder, 'setAddQueryStringMethod')) {
151-
$uriBuilder->setAddQueryStringMethod($this->arguments['addQueryStringMethod']);
67+
if ($arguments['ajax'] !== true) {
68+
// @deprecated
69+
trigger_error('Setting the argument "ajax" to false is deprecated. Use the TYPO3 widget view helper instead for such use case.', E_USER_DEPRECATED);
15270
}
71+
$uri = (new TyposcriptRenderingUri())->withWidgetContext(
72+
new ViewHelperContext(
73+
$this->renderingContext,
74+
$arguments
75+
)
76+
);
15377

154-
return $uriBuilder->build();
155-
}
78+
$this->tag->addAttribute('href', (string)$uri);
79+
$this->tag->setContent($this->renderChildren());
15680

157-
/**
158-
* @param string $extensionName
159-
* @param string $pluginName
160-
* @param string $contextRecord
161-
*
162-
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
163-
* @return string[]
164-
*
165-
*/
166-
public function buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord)
167-
{
168-
$configurationBuilder = new RecordRenderingConfigurationBuilder(new RenderingContext($GLOBALS['TSFE']));
169-
return $configurationBuilder->configurationFor($extensionName, $pluginName, $contextRecord);
81+
return $this->tag->render();
17082
}
17183
}

0 commit comments

Comments
 (0)