storing the render function in ViewHelperVariableContainer
All checks were successful
ci/woodpecker/push/code-style Pipeline was successful
ci/woodpecker/push/functional-tests/1 Pipeline was successful
ci/woodpecker/push/functional-tests/2 Pipeline was successful
ci/woodpecker/push/functional-tests/3 Pipeline was successful
ci/woodpecker/push/functional-tests/4 Pipeline was successful

This commit is contained in:
Ferdinand Kuhl 2024-06-02 04:04:48 +02:00
parent 9f3488f84e
commit 050e6e119f
6 changed files with 42 additions and 6 deletions

View file

@ -7,15 +7,18 @@ namespace DigiComp\FluidRenderFunctions\ViewHelpers;
use DigiComp\FluidRenderFunctions\InvokeRenderFunctionInterface; use DigiComp\FluidRenderFunctions\InvokeRenderFunctionInterface;
use DigiComp\FluidRenderFunctions\Utils\GeneratorClosureIterator; use DigiComp\FluidRenderFunctions\Utils\GeneratorClosureIterator;
use DigiComp\FluidRenderFunctions\Utils\RenderableProxy; use DigiComp\FluidRenderFunctions\Utils\RenderableProxy;
use DigiComp\FluidRenderFunctions\ViewHelpers\Traits\ValidateRenderFunctionTrait;
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper; use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
class ApplyRenderFunctionViewHelper extends AbstractViewHelper class ApplyRenderFunctionViewHelper extends AbstractViewHelper
{ {
use ValidateRenderFunctionTrait;
public function initializeArguments(): void public function initializeArguments(): void
{ {
parent::initializeArguments(); parent::initializeArguments();
$this->registerArgument('in', 'mixed', 'subject to apply the render function to'); $this->registerArgument('in', 'mixed', 'subject to apply the render function to');
$this->registerArgument('function', InvokeRenderFunctionInterface::class, 'render function to use', true); $this->registerArgument('function', 'string', 'render function to use', true);
$this->registerArgument( $this->registerArgument(
'force', 'force',
'bool', 'bool',
@ -26,6 +29,12 @@ class ApplyRenderFunctionViewHelper extends AbstractViewHelper
); );
} }
public function validateArguments()
{
parent::validateArguments();
$this->validateRenderFunctionArgument('function');
}
public function render() public function render()
{ {
$in = $this->arguments['in']; $in = $this->arguments['in'];

View file

@ -47,8 +47,9 @@ class RegisterRenderFunctionViewHelper extends AbstractViewHelper
foreach ($this->childNodes as $childNode) { foreach ($this->childNodes as $childNode) {
$transferNode->addChildNode($childNode); $transferNode->addChildNode($childNode);
} }
$this->renderingContext->getVariableProvider() $renderer = new NodeRenderTransfer($transferNode, $this->arguments['subjectName']);
->add($this->arguments['as'], new NodeRenderTransfer($transferNode, $this->arguments['subjectName'])); $this->renderingContext->getViewHelperVariableContainer()
->add(static::class, $this->arguments['as'], $renderer);
return ''; return '';
} }
} }

View file

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace DigiComp\FluidRenderFunctions\ViewHelpers\Traits;
use DigiComp\FluidRenderFunctions\InvokeRenderFunctionInterface;
use DigiComp\FluidRenderFunctions\ViewHelpers\RegisterRenderFunctionViewHelper;
trait ValidateRenderFunctionTrait
{
public function validateRenderFunctionArgument(string $argumentName)
{
$renderFunction = $this->viewHelperVariableContainer->get(
RegisterRenderFunctionViewHelper::class,
$this->arguments[$argumentName]
);
if (!($renderFunction instanceof InvokeRenderFunctionInterface)) {
throw new \InvalidArgumentException(
'render function with name "' . $this->arguments[$argumentName] . '" has not been registered.',
1717293038
);
}
$this->arguments[$argumentName] = $renderFunction;
}
}

View file

@ -13,5 +13,5 @@ FluidRenderFunctions to the rescue:
<rf:registerRenderFunction as="renderBook" subjectName="myBook"> <rf:registerRenderFunction as="renderBook" subjectName="myBook">
{myBook.name} from {myBook.author.name} {myBook.name} from {myBook.author.name}
</rf:registerRenderFunction> </rf:registerRenderFunction>
<f:form.select options="{books -> rf:applyRenderFunction(function: renderBook)}" /> <f:form.select options="{books -> rf:applyRenderFunction(function: 'renderBook')}" />
``` ```

View file

@ -4,5 +4,5 @@
<rf:registerRenderFunction as="testFunc"> <rf:registerRenderFunction as="testFunc">
{subject.name} is cool {subject.name} is cool
</rf:registerRenderFunction> </rf:registerRenderFunction>
{test -> rf:applyRenderFunction(function: testFunc, force: true)} {test -> rf:applyRenderFunction(function: 'testFunc', force: true)}
</f:spaceless></f:section> </f:spaceless></f:section>

View file

@ -4,5 +4,5 @@
<rf:registerRenderFunction as="testFunc"> <rf:registerRenderFunction as="testFunc">
{subject.name} is cool {subject.name} is cool
</rf:registerRenderFunction> </rf:registerRenderFunction>
<f:form.select options="{testEntities -> rf:applyRenderFunction(function: testFunc)}" /> <f:form.select options="{testEntities -> rf:applyRenderFunction(function: 'testFunc')}" />
</f:spaceless></f:section> </f:spaceless></f:section>