Problem/Motivation
SDC documentation is currently promoting using Twig's include
function and & embed
tag to call components from template. And it seems to be a popular way of using SDC components (for now, let's hope it will disappear as soon as SDC components are becoming usable directly from the render API and the display builders)
There is a big issue about include
and embed
: they don't pass through the SDC render element and don't trigger the Render API mechanisms.
So, SDC is currently doing a complex workaround.
ComponentsTwigExtension is adding 2 twig functions:
add_component_context
which is adding the attribute Attribute object if missing
validate_component_props
which is executing the JSON schema validator
They are not supposed to be used by templates owner, there exists because ComponentNodeVisitor is printing those Twig functions on every template, which will be executed on rendering in this order:
- attach_library
- add_component_context
- validate_component_props
In order to make the use of "template to template" Twig functions or tags (include
and embed
) more similar to the use of the Render API, leveraging the render element. For example, attach_library
is used here because #attached
is not executed.
This is a mess which can be fixed.
Proposed resolution
- Add asset library attachments (
#attached
) and prop validation to the render element itself
- Override Twig native
include
and embed
to return the SDC render element when the "template" is a full component plugin ID instead of a template path.
- Remove everything but
::getNodeVisitors()
in ComponentsTwigExtension and most of ComponentNodeVisitor
So, people using SDC from the Render API and people using SDC from templates will have , implemented in a cleaner way.
API changes
If done right, this may be free of breaking changes.