From 8c56e8b0031ffd0e0243c26d837e5b7d8010bda1 Mon Sep 17 00:00:00 2001 From: Benjamin Kott Date: Fri, 25 Aug 2023 09:48:11 +0200 Subject: [PATCH] [FEATURE] Introduce render parent viewhelper --- .../ViewHelpers/Render/ParentViewHelper.php | 71 ++++++++ .../Render/ParentViewHelperTest.php | 168 ++++++++++++++++++ .../Partials/ExistAll/Level1/Partial.html | 2 + .../Partials/ExistAll/Level2/Partial.html | 2 + .../Partials/ExistAll/Level3/Partial.html | 2 + .../Partials/ExistAll/Level4/Partial.html | 2 + .../Partials/FirstAndLast/Level1/Partial.html | 2 + .../Partials/FirstAndLast/Level4/Partial.html | 2 + .../Partials/NoParent/Level4/Partial.html | 2 + .../OnlyOnePartialPath/Level1/Partial.html | 2 + 10 files changed, 255 insertions(+) create mode 100644 Classes/ViewHelpers/Render/ParentViewHelper.php create mode 100644 Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php create mode 100644 Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level1/Partial.html create mode 100644 Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level2/Partial.html create mode 100644 Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level3/Partial.html create mode 100644 Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level4/Partial.html create mode 100644 Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level1/Partial.html create mode 100644 Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level4/Partial.html create mode 100644 Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/NoParent/Level4/Partial.html create mode 100644 Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/OnlyOnePartialPath/Level1/Partial.html diff --git a/Classes/ViewHelpers/Render/ParentViewHelper.php b/Classes/ViewHelpers/Render/ParentViewHelper.php new file mode 100644 index 000000000..899eec60c --- /dev/null +++ b/Classes/ViewHelpers/Render/ParentViewHelper.php @@ -0,0 +1,71 @@ +registerArgument('partial', 'string', 'Partial to render'); + } + + /** + * @return string + */ + public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) + { + $partial = $arguments['partial'] ?? null; + if ($partial === null) { + return ''; + } + + $format = 'html'; + $variables = (array) $renderingContext->getVariableProvider()->getAll(); + $partialRootPaths = $variables['__unprocessedPartialRootPaths'] ?? $renderingContext->getTemplatePaths()->getPartialRootPaths(); + $partialRootPaths = array_filter($partialRootPaths, function ($path) use ($partial, $format) { + return file_exists($path . $partial . '.' . $format); + }); + if (count($partialRootPaths) === 1) { + return ''; + } + array_pop($partialRootPaths); + $currentTemplate = end($partialRootPaths) . $partial . '.' . $format; + + $context = GeneralUtility::makeInstance(RenderingContextFactory::class)->create(); + $context->setRequest($renderingContext->getRequest()); + $view = GeneralUtility::makeInstance(StandaloneView::class, $context); + $view->assignMultiple($variables); + $view->assign('__unprocessedPartialRootPaths', $partialRootPaths); + $view->setLayoutRootPaths($renderingContext->getTemplatePaths()->getLayoutRootPaths()); + $view->setPartialRootPaths($renderingContext->getTemplatePaths()->getPartialRootPaths()); + $view->setTemplateRootPaths($renderingContext->getTemplatePaths()->getTemplateRootPaths()); + $view->setTemplatePathAndFilename($currentTemplate); + + return $view->render(); + } +} diff --git a/Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php b/Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php new file mode 100644 index 000000000..cc038ea04 --- /dev/null +++ b/Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php @@ -0,0 +1,168 @@ +get(RenderingContextFactory::class)->create([ + 'partialRootPaths' => [ + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level1/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level2/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level3/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level4/', + ] + ]); + + $context->getTemplatePaths()->setTemplateSource(''); + $view = (new TemplateView($context)); + + self::assertSame( + implode( + PHP_EOL, + [ + '', + 'BASIC - LEVEL1', + '', + 'BASIC - LEVEL2', + '', + 'BASIC - LEVEL3', + '', + 'BASIC - LEVEL4', + '', + ] + ), + $view->render() + ); + } + + /** + * @test + */ + public function renderFirstAndLast(): void + { + $context = $this->get(RenderingContextFactory::class)->create([ + 'partialRootPaths' => [ + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level1/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level2/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level3/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level4/', + ] + ]); + + $context->getTemplatePaths()->setTemplateSource(''); + $view = (new TemplateView($context)); + + self::assertSame( + implode( + PHP_EOL, + [ + '', + 'BASIC - LEVEL1', + '', + 'BASIC - LEVEL4', + '', + ] + ), + $view->render() + ); + } + + /** + * @test + */ + public function renderNoParent(): void + { + $context = $this->get(RenderingContextFactory::class)->create([ + 'partialRootPaths' => [ + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/NoParent/Level1/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/NoParent/Level2/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/NoParent/Level3/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/NoParent/Level4/', + ] + ]); + + $context->getTemplatePaths()->setTemplateSource(''); + $view = (new TemplateView($context)); + + self::assertSame( + implode( + PHP_EOL, + [ + '', + 'BASIC - LEVEL4', + '', + ] + ), + $view->render() + ); + } + + /** + * @test + */ + public function renderOnlyOnePartialPath(): void + { + $context = $this->get(RenderingContextFactory::class)->create([ + 'partialRootPaths' => [ + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/OnlyOnePartialPath/Level1/', + ] + ]); + $context->getTemplatePaths()->setTemplateSource(''); + $view = (new TemplateView($context)); + + self::assertSame( + implode( + PHP_EOL, + [ + '', + 'BASIC - LEVEL1', + '', + ] + ), + $view->render() + ); + } + + /** + * @test + */ + public function renderNoneExist(): void + { + $context = $this->get(RenderingContextFactory::class)->create([ + 'partialRootPaths' => [ + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/NoneExist/Level1/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/NoneExist/Level2/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/NoneExist/Level3/', + 'EXT:demo_package/Resources/Private/ParentViewHelperTest/Partials/NoneExist/Level4/', + ] + ]); + + $context->getTemplatePaths()->setTemplateSource(''); + $view = (new TemplateView($context)); + + $this->expectException(InvalidTemplateResourceException::class); + $view->render(); + } +} diff --git a/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level1/Partial.html b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level1/Partial.html new file mode 100644 index 000000000..7a2c27823 --- /dev/null +++ b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level1/Partial.html @@ -0,0 +1,2 @@ + +BASIC - LEVEL1 diff --git a/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level2/Partial.html b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level2/Partial.html new file mode 100644 index 000000000..3a5baaf10 --- /dev/null +++ b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level2/Partial.html @@ -0,0 +1,2 @@ + +BASIC - LEVEL2 diff --git a/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level3/Partial.html b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level3/Partial.html new file mode 100644 index 000000000..5bfc2e5d5 --- /dev/null +++ b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level3/Partial.html @@ -0,0 +1,2 @@ + +BASIC - LEVEL3 diff --git a/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level4/Partial.html b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level4/Partial.html new file mode 100644 index 000000000..17dd9947a --- /dev/null +++ b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/ExistAll/Level4/Partial.html @@ -0,0 +1,2 @@ + +BASIC - LEVEL4 diff --git a/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level1/Partial.html b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level1/Partial.html new file mode 100644 index 000000000..7a2c27823 --- /dev/null +++ b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level1/Partial.html @@ -0,0 +1,2 @@ + +BASIC - LEVEL1 diff --git a/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level4/Partial.html b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level4/Partial.html new file mode 100644 index 000000000..17dd9947a --- /dev/null +++ b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/FirstAndLast/Level4/Partial.html @@ -0,0 +1,2 @@ + +BASIC - LEVEL4 diff --git a/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/NoParent/Level4/Partial.html b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/NoParent/Level4/Partial.html new file mode 100644 index 000000000..17dd9947a --- /dev/null +++ b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/NoParent/Level4/Partial.html @@ -0,0 +1,2 @@ + +BASIC - LEVEL4 diff --git a/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/OnlyOnePartialPath/Level1/Partial.html b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/OnlyOnePartialPath/Level1/Partial.html new file mode 100644 index 000000000..7a2c27823 --- /dev/null +++ b/Tests/Packages/demo_package/Resources/Private/ParentViewHelperTest/Partials/OnlyOnePartialPath/Level1/Partial.html @@ -0,0 +1,2 @@ + +BASIC - LEVEL1