Skip to content

Commit 9eab459

Browse files
committed
Refactor URI generation and apply it to view helper
1 parent 0827abc commit 9eab459

3 files changed

Lines changed: 183 additions & 66 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace Helhum\TyposcriptRendering\Uri;
4+
5+
/*
6+
* This file is part of the TypoScript Rendering TYPO3 extension.
7+
*
8+
* It is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU General Public License, either version 2
10+
* of the License, or any later version.
11+
*
12+
* For the full copyright and license information, please read
13+
* LICENSE file that was distributed with this source code.
14+
*
15+
*/
16+
17+
use Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException;
18+
use Helhum\TyposcriptRendering\Configuration\RecordRenderingConfigurationBuilder;
19+
use Helhum\TyposcriptRendering\Renderer\RenderingContext;
20+
use TYPO3\CMS\Core\Http\Uri;
21+
22+
class TyposcriptRenderingUri extends Uri
23+
{
24+
/**
25+
* @var ViewHelperContext
26+
*/
27+
private $viewHelperContext;
28+
29+
public function withViewHelperContext(ViewHelperContext $viewHelperContext): self
30+
{
31+
$newUri = clone $this;
32+
$newUri->viewHelperContext = $viewHelperContext;
33+
$newUri->parseViewHelperContext($viewHelperContext);
34+
35+
return $newUri;
36+
}
37+
38+
private function parseViewHelperContext(ViewHelperContext $viewHelperContext): void
39+
{
40+
$arguments = $viewHelperContext->getArguments();
41+
$controllerContext = $viewHelperContext->getControllerContext();
42+
43+
$pluginName = $arguments['pluginName'];
44+
$extensionName = $arguments['extensionName'];
45+
$contextRecord = $arguments['contextRecord'];
46+
47+
if ($pluginName === null) {
48+
$pluginName = $controllerContext->getRequest()->getPluginName();
49+
}
50+
if ($extensionName === null) {
51+
$extensionName = $controllerContext->getRequest()->getControllerExtensionName();
52+
}
53+
if ($contextRecord === 'current') {
54+
if (
55+
$pluginName !== $controllerContext->getRequest()->getPluginName()
56+
|| $extensionName !== $controllerContext->getRequest()->getControllerExtensionName()
57+
) {
58+
$contextRecord = 'currentPage';
59+
} else {
60+
$contextRecord = $viewHelperContext->getContentObject()->currentRecord;
61+
}
62+
}
63+
$renderingConfiguration = $this->buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord);
64+
$additionalParams['tx_typoscriptrendering']['context'] = json_encode($renderingConfiguration);
65+
66+
$uriBuilder = $controllerContext->getUriBuilder();
67+
$uriBuilder->reset()
68+
->setTargetPageUid($arguments['pageUid'])
69+
->setUseCacheHash(true)
70+
->setSection($arguments['section'])
71+
->setFormat($arguments['format'])
72+
->setLinkAccessRestrictedPages($arguments['linkAccessRestrictedPages'])
73+
->setArguments($additionalParams)
74+
->setCreateAbsoluteUri($arguments['absolute'])
75+
->setAddQueryString($arguments['addQueryString'])
76+
->setAddQueryStringMethod($arguments['addQueryStringMethod'])
77+
->setArgumentsToBeExcludedFromQueryString($arguments['argumentsToBeExcludedFromQueryString']);
78+
79+
$this->parseUri($uriBuilder->uriFor($arguments['action'], $arguments['arguments'], $arguments['controller'], $extensionName, $pluginName));
80+
}
81+
82+
/**
83+
* @param string $extensionName
84+
* @param string $pluginName
85+
* @param string $contextRecordId
86+
*
87+
* @throws ConfigurationBuildingException
88+
* @return string[]
89+
*
90+
*/
91+
public function buildTypoScriptRenderingConfiguration(string $extensionName, string $pluginName, string $contextRecordId): array
92+
{
93+
$configurationBuilder = new RecordRenderingConfigurationBuilder(new RenderingContext($GLOBALS['TSFE']));
94+
95+
return $configurationBuilder->configurationFor($extensionName, $pluginName, $contextRecordId);
96+
}
97+
}

Classes/Uri/ViewHelperContext.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace Helhum\TyposcriptRendering\Uri;
4+
5+
/*
6+
* This file is part of the TypoScript Rendering TYPO3 extension.
7+
*
8+
* It is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU General Public License, either version 2
10+
* of the License, or any later version.
11+
*
12+
* For the full copyright and license information, please read
13+
* LICENSE file that was distributed with this source code.
14+
*
15+
*/
16+
17+
use TYPO3\CMS\Core\Utility\GeneralUtility;
18+
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
19+
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
20+
use TYPO3\CMS\Extbase\Object\ObjectManager;
21+
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22+
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23+
24+
class ViewHelperContext
25+
{
26+
/**
27+
* @var RenderingContextInterface
28+
*/
29+
private $renderingContext;
30+
31+
/**
32+
* @var array
33+
*/
34+
private $arguments;
35+
36+
/**
37+
* @var ConfigurationManager
38+
*/
39+
private $configurationManager;
40+
41+
public function __construct(RenderingContextInterface $renderingContext, array $arguments, ConfigurationManager $configurationManager = null)
42+
{
43+
$this->renderingContext = $renderingContext;
44+
$this->arguments = $arguments;
45+
$this->configurationManager = $configurationManager;
46+
}
47+
48+
public function getControllerContext(): ControllerContext
49+
{
50+
if ($this->renderingContext instanceof \TYPO3\CMS\Fluid\Core\Rendering\RenderingContext) {
51+
return $this->renderingContext->getControllerContext();
52+
}
53+
54+
// Let PHP deal with the error, we don't operate in other contexts anyway
55+
return null;
56+
}
57+
58+
public function getArguments(): array
59+
{
60+
return $this->arguments;
61+
}
62+
63+
public function getContentObject(): ContentObjectRenderer
64+
{
65+
$configurationManager = $this->configurationManager ?? GeneralUtility::makeInstance(ObjectManager::class)->get(ConfigurationManager::class);
66+
$contentObject = $configurationManager->getContentObject();
67+
68+
return $contentObject ?? GeneralUtility::makeInstance(ContentObjectRenderer::class);
69+
}
70+
}

Classes/ViewHelpers/Uri/AjaxActionViewHelper.php

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
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\Rendering\RenderingContextInterface;
20+
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
1921

2022
/**
2123
* A view helper for creating Ajax URIs to extbase actions.
2224
*
2325
* = Examples =
2426
*
2527
* <code title="URI to the show-action of the current controller">
26-
* <h:uri.ajaxAction action="show" />
28+
* <t:uri.ajaxAction action="show" />
2729
* </code>
2830
* <output>
2931
* index.php?id=123&tx_typoscriptrendering[context]={"record":"tt_content_123","path":"tt_content.list.20.myextension_plugin"}&tx_myextension_plugin[action]=show&tx_myextension_plugin[controller]=Standard&cHash=xyz
@@ -32,11 +34,7 @@
3234
*/
3335
class AjaxActionViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
3436
{
35-
/**
36-
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
37-
* @inject
38-
*/
39-
protected $configurationManager;
37+
use CompileWithRenderStatic;
4038

4139
/**
4240
* Initialize arguments
@@ -51,6 +49,8 @@ public function initializeArguments()
5149
$this->registerArgument('extensionName', 'string', 'Target Extension Name (without "tx_" prefix and no underscores). If NULL the current extension name is used');
5250
$this->registerArgument('pluginName', 'string', 'Target plugin. If empty, the current plugin name is used');
5351
$this->registerArgument('pageUid', 'int', 'Target page. See TypoLink destination');
52+
$this->registerArgument('pageType', 'int', 'Type of the target page. See typolink.parameter', false, 0);
53+
$this->registerArgument('noCache', 'bool', 'Set this to disable caching for the target page. You should not need this.', false, false);
5454
$this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
5555
$this->registerArgument('format', 'string', 'The requested format, e.g. ".html', false, '');
5656
$this->registerArgument('linkAccessRestrictedPages', 'bool', 'If set, links pointing to access restricted pages will still link to the page even though the page cannot be accessed.', false, false);
@@ -62,65 +62,15 @@ public function initializeArguments()
6262
$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');
6363
}
6464

65-
/**
66-
*
67-
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
68-
* @return string Rendered link
69-
*
70-
*/
71-
public function render()
65+
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
7266
{
73-
$pluginName = $this->arguments['pluginName'];
74-
$extensionName = $this->arguments['extensionName'];
75-
$contextRecord = $this->arguments['contextRecord'];
76-
77-
if ($pluginName === null) {
78-
$pluginName = $this->renderingContext->getControllerContext()->getRequest()->getPluginName();
79-
}
80-
if ($extensionName === null) {
81-
$extensionName = $this->renderingContext->getControllerContext()->getRequest()->getControllerExtensionName();
82-
}
83-
if ($contextRecord === 'current') {
84-
if (
85-
$pluginName !== $this->renderingContext->getControllerContext()->getRequest()->getPluginName()
86-
|| $extensionName !== $this->renderingContext->getControllerContext()->getRequest()->getControllerExtensionName()
87-
) {
88-
$contextRecord = 'currentPage';
89-
} else {
90-
$contextRecord = $this->configurationManager->getContentObject()->currentRecord;
91-
}
92-
}
93-
$renderingConfiguration = $this->buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord);
94-
$additionalParams['tx_typoscriptrendering']['context'] = json_encode($renderingConfiguration);
95-
96-
$uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();
97-
$uriBuilder->reset()
98-
->setTargetPageUid($this->arguments['pageUid'])
99-
->setUseCacheHash(true)
100-
->setSection($this->arguments['section'])
101-
->setFormat($this->arguments['format'])
102-
->setLinkAccessRestrictedPages($this->arguments['linkAccessRestrictedPages'])
103-
->setArguments($additionalParams)
104-
->setCreateAbsoluteUri($this->arguments['absolute'])
105-
->setAddQueryString($this->arguments['addQueryString'])
106-
->setAddQueryStringMethod($this->arguments['addQueryStringMethod'])
107-
->setArgumentsToBeExcludedFromQueryString($this->arguments['argumentsToBeExcludedFromQueryString']);
67+
$uri = (new TyposcriptRenderingUri())->withViewHelperContext(
68+
new ViewHelperContext(
69+
$renderingContext,
70+
$arguments
71+
)
72+
);
10873

109-
return $uriBuilder->uriFor($this->arguments['action'], $this->arguments['arguments'], $this->arguments['controller'], $extensionName, $pluginName);
110-
}
111-
112-
/**
113-
* @param string $extensionName
114-
* @param string $pluginName
115-
* @param string $contextRecord
116-
*
117-
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
118-
* @return string[]
119-
*
120-
*/
121-
public function buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord)
122-
{
123-
$configurationBuilder = new RecordRenderingConfigurationBuilder(new RenderingContext($GLOBALS['TSFE']));
124-
return $configurationBuilder->configurationFor($extensionName, $pluginName, $contextRecord);
74+
return (string)$uri;
12575
}
12676
}

0 commit comments

Comments
 (0)