Discern between additional and defined arguments
Hello,
currently it is not possible to do something like this: https://fluid-primitives.com/docs/viewhelpers/attributes
https://github.com/TYPO3/Fluid/blob/18048d9e26375c114cc1a59d47f0ceef186194b2/src/Core/Component/ComponentAdapter.php#L78C1-L87C6
Arguments are just joined but ideally we should be able to discern between defined arguments and additional arguments
Hi! I think it should be possible to implement this with the current APIs:
- set
additionalArgumentsAllowed()totruein your component collection (I assume you already do that) - use a custom component renderer (you probably also do that)
- in your
renderComponent()implementation, you can get the component's definition with$this->componentResolver->getComponentDefinition($viewHelperName)(which should already be cached) and then compare the component definition with the provided arguments. I would then consider only passing the defined arguments to the template and assign the additional arguments to theViewHelperVariableContainer, so that they can only be accessed through your specific ViewHelper.
Does that make sense?
Hi! Thanks for the quick reply. We were not yet using a Custom Component Renderer because we thought that might block or complicate future updates.
We now added a getAdditionalVariables function to our ComponentCollection
public function getAdditionalVariables(string $viewHelperName): array
{
return [
'definedArguments' => array_keys($this->getComponentDefinition($viewHelperName)->getArgumentDefinitions()),
'flattenedArguments' => implode(',', array_keys($this->getComponentDefinition($viewHelperName)->getArgumentDefinitions()))
];
}
that gives us the arguments that are defined using f:argument and then we have a ViewHelper that prints out the additional arguments based on the defined arguments.
This would still be a great feature to have directly inside fluid as rendering additional arguments / props is really useful when using components.
We were not yet using a Custom Component Renderer because we thought that might block or complicate future updates.
Not the ComponentAdapter (which shouldn't be replaced), but the ComponentRenderer (which you already use, at least I see it in your repository). It's safe to create your own renderer, in fact it's encouraged, as noted in the documentation:
https://docs.typo3.org/other/typo3fluid/fluid/main/en-us/Extending/ComponentProviders.html#custom-componentrenderer
But I see your point: It should be built-in to Fluid to render arbitrary HTML attributes with a built-in ViewHelper.
Related: #1135
Hey @mkroener im creating fluid-primitives and if i understand you correctly the custom component renderer from fluid-primitives is already doing what you described like simon explained it (just not yet with the ViewHelperVariableContainer 😬).
During the getComponentDefinition from the component collection we check if the ui:attributes viewhelper is used in the component and set additionalArgumentsAllowed to true. They are then filtered against the defined arguments during rendering https://github.com/jramke/fluid-primitives/blob/c2032496ac2ffd9473f5788a885de2d9fd20ad6c/Classes/Component/ComponentRenderer.php#L78-L79
Hi @jramke, thanks for the clarification. Just to avoid further confusion: I did not mean to imply that this is my library or that Fluid has any kind of issue with it. I simply wanted to point out that this external library already implements the requested behaviour and that something similar might be useful in the core.
@s2b By the way, I want to add something positive here. The current evolution of TYPO3 Fluid as a library is really impressive. Libraries like fluid-primitives show how powerful the new versions already are and how much potential there is in the direction you are taking. I think this is a very strong path forward. You and the other maintainers are doing excellent work.