DigiComp.FluidRenderFunctions/Classes/FormExtensions/SelectAspect.php

69 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace DigiComp\FluidRenderFunctions\FormExtensions;
use DigiComp\FluidRenderFunctions\InvokeRenderFunctionInterface;
use DigiComp\FluidRenderFunctions\Utils\GeneratorClosureIterator;
use DigiComp\FluidRenderFunctions\Utils\RenderableProxy;
use DigiComp\FluidRenderFunctions\ViewHelpers\RegisterRenderFunctionViewHelper;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\FluidAdaptor\ViewHelpers\Form\SelectViewHelper;
/**
* @Flow\Aspect
*/
class SelectAspect extends SelectViewHelper
{
/**
* @Flow\After("setting(DigiComp.FluidRenderFunctions.enableAspects.select) && method(Neos\FluidAdaptor\ViewHelpers\Form\SelectViewHelper->initializeArguments())")
*/
public function introduceRenderFuncArgument(JoinPointInterface $joinPoint): void
{
$proxy = $joinPoint->getProxy();
if (!($proxy instanceof SelectViewHelper)) {
return;
}
$proxy->registerArgument('renderFunction', 'string', 'callabe to use to render single object');
}
/**
* @Flow\After("setting(DigiComp.FluidRenderFunctions.enableAspects.select) && method(Neos\FluidAdaptor\ViewHelpers\Form\SelectViewHelper->validateArguments())")
*/
public function validateRenderFunction(JoinPointInterface $joinPoint): void
{
$proxy = $joinPoint->getProxy();
if (!($proxy instanceof SelectViewHelper)) {
return;
}
if (!isset($proxy->arguments['renderFunction'])) {
return;
}
$renderFunction = $proxy->viewHelperVariableContainer->get(
RegisterRenderFunctionViewHelper::class,
$proxy->arguments['renderFunction']
);
if (!($renderFunction instanceof InvokeRenderFunctionInterface)) {
throw new \InvalidArgumentException(
'render function with name "' . $proxy->arguments['renderFunction'] . '" has not been registered.',
1717293038
);
}
$proxy->arguments['renderFunction'] = $renderFunction;
$originalOptions = $proxy->arguments['options'];
if (!\is_iterable($originalOptions)) {
// Validation is left to the original view helper
return;
}
$proxy->arguments['options'] = new GeneratorClosureIterator(
static function () use ($originalOptions, $renderFunction) {
foreach ($originalOptions as $option) {
yield new RenderableProxy($renderFunction, $option);
}
}
);
}
}