Skip to content

Commit

Permalink
[FEATURE] Introduce render parent viewhelper
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed Nov 12, 2024
1 parent ea0b95f commit 4f2396d
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Classes/ViewHelpers/Render/ParentViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php declare(strict_types=1);

/*
* This file is part of the package bk2k/bootstrap-package.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace BK2K\BootstrapPackage\ViewHelpers\Render;

use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

class ParentViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

Check failure on line 22 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Usage of deprecated trait TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic in class BK2K\BootstrapPackage\ViewHelpers\Render\ParentViewHelper: Will be removed in v5. The non-static render() method should be used instead

Check failure on line 22 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Usage of deprecated trait TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic in class BK2K\BootstrapPackage\ViewHelpers\Render\ParentViewHelper: Will be removed in v5. The non-static render() method should be used instead

Check failure on line 22 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Usage of deprecated trait TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic in class BK2K\BootstrapPackage\ViewHelpers\Render\ParentViewHelper: Will be removed in v5. The non-static render() method should be used instead

Check failure on line 22 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Usage of deprecated trait TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic in class BK2K\BootstrapPackage\ViewHelpers\Render\ParentViewHelper: Will be removed in v5. The non-static render() method should be used instead

/**
* @var bool
*/
protected $escapeOutput = false;

/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->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;

if ((new Typo3Version())->getMajorVersion() < 12) {
$view = GeneralUtility::makeInstance(StandaloneView::class);

Check failure on line 61 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Fetching class constant class of deprecated class TYPO3\CMS\Fluid\View\StandaloneView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

Check failure on line 61 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Fetching class constant class of deprecated class TYPO3\CMS\Fluid\View\StandaloneView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

Check failure on line 61 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Class TYPO3\CMS\Fluid\View\StandaloneView not found.

Check failure on line 61 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Class TYPO3\CMS\Fluid\View\StandaloneView not found.
} else {
$context = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
$context->setRequest($renderingContext->getRequest());

Check failure on line 64 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Call to an undefined method TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface::getRequest().

Check failure on line 64 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Call to deprecated method setRequest() of class TYPO3\CMS\Fluid\Core\Rendering\RenderingContext: since TYPO3 v13, will be removed in TYPO3 v14. Use RenderingContextFactory->create($pathArray, $request) instead.

Check failure on line 64 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Call to an undefined method TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface::getRequest().

Check failure on line 64 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Call to deprecated method setRequest() of class TYPO3\CMS\Fluid\Core\Rendering\RenderingContext: since TYPO3 v13, will be removed in TYPO3 v14. Use RenderingContextFactory->create($pathArray, $request) instead.

Check failure on line 64 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Call to an undefined method TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface::getRequest().

Check failure on line 64 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Call to an undefined method TYPO3\CMS\Fluid\Core\Rendering\RenderingContext::setRequest().

Check failure on line 64 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Call to an undefined method TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface::getRequest().

Check failure on line 64 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Call to an undefined method TYPO3\CMS\Fluid\Core\Rendering\RenderingContext::setRequest().
$view = GeneralUtility::makeInstance(StandaloneView::class, $context);

Check failure on line 65 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Fetching class constant class of deprecated class TYPO3\CMS\Fluid\View\StandaloneView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

Check failure on line 65 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Fetching class constant class of deprecated class TYPO3\CMS\Fluid\View\StandaloneView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

Check failure on line 65 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Class TYPO3\CMS\Fluid\View\StandaloneView not found.

Check failure on line 65 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Class TYPO3\CMS\Fluid\View\StandaloneView not found.
}
$view->assignMultiple($variables);

Check failure on line 67 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Call to method assignMultiple() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.

Check failure on line 67 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Call to method assignMultiple() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.
$view->assign('__unprocessedPartialRootPaths', $partialRootPaths);

Check failure on line 68 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Call to method assign() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.

Check failure on line 68 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Call to method assign() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.
$view->setLayoutRootPaths($renderingContext->getTemplatePaths()->getLayoutRootPaths());

Check failure on line 69 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Call to method setLayoutRootPaths() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.

Check failure on line 69 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Call to method setLayoutRootPaths() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.
$view->setPartialRootPaths($renderingContext->getTemplatePaths()->getPartialRootPaths());

Check failure on line 70 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Call to method setPartialRootPaths() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.

Check failure on line 70 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Call to method setPartialRootPaths() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.
$view->setTemplateRootPaths($renderingContext->getTemplatePaths()->getTemplateRootPaths());

Check failure on line 71 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.2)

Call to method setTemplateRootPaths() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.

Check failure on line 71 in Classes/ViewHelpers/Render/ParentViewHelper.php

View workflow job for this annotation

GitHub Actions / Build PHP (14.0.x-dev, 8.3)

Call to method setTemplateRootPaths() on an unknown class TYPO3\CMS\Fluid\View\StandaloneView.
$view->setTemplatePathAndFilename($currentTemplate);

return $view->render();
}
}
168 changes: 168 additions & 0 deletions Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php declare(strict_types=1);

/*
* This file is part of the package bk2k/bootstrap-package.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace T3G\AgencyPack\Blog\Tests\Functional\ViewHelpers;

use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
use TYPO3\CMS\Fluid\View\TemplateView;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use TYPO3Fluid\Fluid\View\Exception\InvalidTemplateResourceException;

final class ParentViewHelperTest extends FunctionalTestCase
{
protected array $testExtensionsToLoad = [
'typo3conf/ext/bootstrap_package',
'typo3conf/ext/demo_package'
];

/**
* @test
*/
public function renderExistAll(): void
{
$context = $this->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('<f:render partial="Partial" />');
$view = (new TemplateView($context));

Check failure on line 39 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

Check failure on line 39 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

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('<f:render partial="Partial" />');
$view = (new TemplateView($context));

Check failure on line 75 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

Check failure on line 75 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

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('<f:render partial="Partial" />');
$view = (new TemplateView($context));

Check failure on line 107 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

Check failure on line 107 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

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('<f:render partial="Partial" />');
$view = (new TemplateView($context));

Check failure on line 133 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

Check failure on line 133 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

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('<f:render partial="Partial" />');
$view = (new TemplateView($context));

Check failure on line 163 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.2)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

Check failure on line 163 in Tests/Functional/ViewHelpers/Render/ParentViewHelperTest.php

View workflow job for this annotation

GitHub Actions / Build PHP (^13, 8.3)

Instantiation of deprecated class TYPO3\CMS\Fluid\View\TemplateView: : since TYPO3 v13, will be removed in v14. Use ext:core ViewFactoryInterface instead.

$this->expectException(InvalidTemplateResourceException::class);
$view->render();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<bk2k:render.parent partial="Partial" />
BASIC - LEVEL1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<bk2k:render.parent partial="Partial" />
BASIC - LEVEL2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<bk2k:render.parent partial="Partial" />
BASIC - LEVEL3
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<bk2k:render.parent partial="Partial" />
BASIC - LEVEL4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<bk2k:render.parent partial="Partial" />
BASIC - LEVEL1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<bk2k:render.parent partial="Partial" />
BASIC - LEVEL4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<bk2k:render.parent partial="Partial" />
BASIC - LEVEL4
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<bk2k:render.parent partial="Partial" />
BASIC - LEVEL1

0 comments on commit 4f2396d

Please sign in to comment.