diff --git a/Classes/Fluid/ViewHelper/ComponentRenderer.php b/Classes/Fluid/ViewHelper/ComponentRenderer.php index 6635516..26b5ff9 100644 --- a/Classes/Fluid/ViewHelper/ComponentRenderer.php +++ b/Classes/Fluid/ViewHelper/ComponentRenderer.php @@ -20,6 +20,7 @@ use SMS\FluidComponents\ViewHelpers\ContentViewHelper; use SMS\FluidComponents\ViewHelpers\ParamViewHelper; use TYPO3\CMS\Core\Configuration\Features; +use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext; use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory; @@ -121,22 +122,24 @@ public function getComponentPrefix(): string */ public function render(): string { - // Create a new rendering context for the component file - $renderingContext = $this->getRenderingContext(); - - // set the original request to preserve the request attributes + // use the original request to preserve the request attributes // some ViewHelpers expect a ServerRequestInterface or other attributes inside the request // e.g. f:uri.action, f:page.action + if (method_exists($this->renderingContext, 'getAttribute') && + method_exists($this->renderingContext, 'hasAttribute') && + $this->renderingContext->hasAttribute(ServerRequestInterface::class) + ) { + $request = $this->renderingContext->getAttribute(ServerRequestInterface::class); + } else { + $request = $this->renderingContext->getRequest(); + } - if (method_exists($this->renderingContext, 'hasAttribute')) { - if ($this->renderingContext->hasAttribute(ServerRequestInterface::class)) { - $renderingContext->setAttribute( - ServerRequestInterface::class, - $this->renderingContext->getAttribute(ServerRequestInterface::class) - ); - } + // Create a new rendering context for the component file + if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) { + $renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create(); + $renderingContext->setRequest($request); } else { - $renderingContext->setRequest($this->renderingContext->getRequest()); + $renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create([], $request); } $renderingContext->setViewHelperVariableContainer($this->renderingContext->getViewHelperVariableContainer()); @@ -398,8 +401,7 @@ public function validateArguments(): void */ protected function initializeComponentParams(): void { - $renderingContext = $this->getRenderingContext(); - + $renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create(); $componentFile = $this->componentLoader->findComponent($this->componentNamespace); // Parse component template without using the cache @@ -578,15 +580,6 @@ protected function getComponentPrefixer(): ComponentPrefixerInterface return self::$componentPrefixerCache[$this->componentNamespace]; } - protected function getRenderingContext(): RenderingContext - { - if ($this->container->has(RenderingContextFactory::class)) { - return $this->container->get(RenderingContextFactory::class)->create(); - } else { - return GeneralUtility::makeInstance(RenderingContext::class); - } - } - protected static function shouldUseTemplatePaths(): bool { static $assertion = null; diff --git a/Classes/ViewHelpers/Form/TranslatedValidationResultsViewHelper.php b/Classes/ViewHelpers/Form/TranslatedValidationResultsViewHelper.php index 217977d..e2c197e 100644 --- a/Classes/ViewHelpers/Form/TranslatedValidationResultsViewHelper.php +++ b/Classes/ViewHelpers/Form/TranslatedValidationResultsViewHelper.php @@ -281,7 +281,7 @@ public static function translateFormElementError( ); } - private function getRequest(): RequestInterface + private function getRequest():? RequestInterface { if (method_exists($this->renderingContext, 'getAttribute') && method_exists($this->renderingContext, 'hasAttribute') && diff --git a/Classes/ViewHelpers/Translate/LabelsViewHelper.php b/Classes/ViewHelpers/Translate/LabelsViewHelper.php index 58803ce..1a1a78e 100644 --- a/Classes/ViewHelpers/Translate/LabelsViewHelper.php +++ b/Classes/ViewHelpers/Translate/LabelsViewHelper.php @@ -68,7 +68,7 @@ public function render(): array return $labels; } - private function getRequest(): RequestInterface + private function getRequest():? RequestInterface { if (method_exists($this->renderingContext, 'getAttribute') && method_exists($this->renderingContext, 'hasAttribute') && diff --git a/Tests/Functional/ComponentRendererTest.php b/Tests/Functional/ComponentRendererTest.php index a6a4c42..350370a 100644 --- a/Tests/Functional/ComponentRendererTest.php +++ b/Tests/Functional/ComponentRendererTest.php @@ -9,6 +9,7 @@ use SMS\FluidComponents\Fluid\ViewHelper\ComponentRenderer; use TYPO3\CMS\Core\Cache\Backend\NullBackend; use TYPO3\CMS\Core\Http\ServerRequest; +use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters; use TYPO3\CMS\Extbase\Mvc\Request; @@ -79,15 +80,25 @@ public function renderComponent($component, $arguments, $content, $expected): vo /** @var ViewHelperInvoker $invoker */ $invoker = GeneralUtility::makeInstance(ViewHelperInvoker::class); - $renderingContext = $container->get(RenderingContextFactory::class)->create( - [], - new Request( + if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) { + $renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create(); + $renderingContext->setRequest(new Request( (new ServerRequest)->withAttribute( 'extbase', new ExtbaseRequestParameters ) - ) - ); + )); + } else { + $renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create( + [], + new Request( + (new ServerRequest)->withAttribute( + 'extbase', + new ExtbaseRequestParameters + ) + ) + ); + } $output = $invoker->invoke( $renderer, diff --git a/Tests/Functional/ParameterEscapingTest.php b/Tests/Functional/ParameterEscapingTest.php index 19a9d7c..0a0bdc1 100644 --- a/Tests/Functional/ParameterEscapingTest.php +++ b/Tests/Functional/ParameterEscapingTest.php @@ -9,6 +9,7 @@ use PHPUnit\Framework\Attributes\Test; use SMS\FluidComponents\Utility\ComponentLoader; use TYPO3\CMS\Core\Http\ServerRequest; +use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters; use TYPO3\CMS\Extbase\Mvc\Request; @@ -196,17 +197,28 @@ public static function renderDataProvider(): Generator public function render(string $template, string $expected): void { $view = new TemplateView(); - $view->setRenderingContext( - GeneralUtility::makeInstance(RenderingContextFactory::class)->create( - [], - new Request( - (new ServerRequest)->withAttribute( - 'extbase', - new ExtbaseRequestParameters + if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) { + $renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create(); + $renderingContext->setRequest(new Request( + (new ServerRequest)->withAttribute( + 'extbase', + new ExtbaseRequestParameters + ) + )); + $view->setRenderingContext($renderingContext); + } else { + $view->setRenderingContext( + GeneralUtility::makeInstance(RenderingContextFactory::class)->create( + [], + new Request( + (new ServerRequest)->withAttribute( + 'extbase', + new ExtbaseRequestParameters + ) ) ) - ) - ); + ); + } $view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers'); $view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components'); diff --git a/Tests/Functional/SlotParameterTest.php b/Tests/Functional/SlotParameterTest.php index 2d32bac..09372f7 100644 --- a/Tests/Functional/SlotParameterTest.php +++ b/Tests/Functional/SlotParameterTest.php @@ -10,6 +10,7 @@ use PHPUnit\Framework\Attributes\Test; use SMS\FluidComponents\Utility\ComponentLoader; use TYPO3\CMS\Core\Http\ServerRequest; +use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters; use TYPO3\CMS\Extbase\Mvc\Request; @@ -120,22 +121,7 @@ public static function renderDataProvider(): Generator #[DataProvider('renderDataProvider')] public function render(string $template, string $expected): void { - $view = new TemplateView(); - - $view->setRenderingContext( - GeneralUtility::makeInstance(RenderingContextFactory::class)->create( - [], - new Request( - (new ServerRequest)->withAttribute( - 'extbase', - new ExtbaseRequestParameters - ) - ) - ) - ); - - $view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers'); - $view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components'); + $view = $this->getView(); $view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template); // Test without cache @@ -149,23 +135,7 @@ public function render(string $template, string $expected): void public function unspecifiedRequiredSlot(): void { $template = ''; - - $view = new TemplateView(); - - $view->setRenderingContext( - GeneralUtility::makeInstance(RenderingContextFactory::class)->create( - [], - new Request( - (new ServerRequest)->withAttribute( - 'extbase', - new ExtbaseRequestParameters - ) - ) - ) - ); - - $view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers'); - $view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components'); + $view = $this->getView(); $view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template); // Test without cache @@ -183,23 +153,7 @@ public function unspecifiedRequiredSlot(): void public function undefinedSlot(): void { $template = 'contentmore content'; - - $view = new TemplateView(); - - $view->setRenderingContext( - GeneralUtility::makeInstance(RenderingContextFactory::class)->create( - [], - new Request( - (new ServerRequest)->withAttribute( - 'extbase', - new ExtbaseRequestParameters - ) - ) - ) - ); - - $view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers'); - $view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components'); + $view = $this->getView(); $view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template); // Test without cache @@ -212,4 +166,37 @@ public function undefinedSlot(): void self::expectExceptionCode(1681832624); $view->render(); } + + protected function getView(): TemplateView + { + $view = new TemplateView(); + + if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) { + $renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create(); + $renderingContext->setRequest(new Request( + (new ServerRequest)->withAttribute( + 'extbase', + new ExtbaseRequestParameters + ) + )); + $view->setRenderingContext($renderingContext); + } else { + $view->setRenderingContext( + GeneralUtility::makeInstance(RenderingContextFactory::class)->create( + [], + new Request( + (new ServerRequest)->withAttribute( + 'extbase', + new ExtbaseRequestParameters + ) + ) + ) + ); + } + + $view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers'); + $view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components'); + + return $view; + } }