- Issue created by @pdureau
- Assigned to mogtofu33
- Status changed to Postponed
6 months ago 10:09am 20 June 2024 - 🇫🇷France pdureau Paris
Postponed because there is no clear solution now.
Jean: "We can override
embed
andinclude
in Twig, but the props & slots are not easily available once they are transformed as variable in the Twig template." - Issue was unassigned.
- 🇫🇷France mogtofu33
Here is a starting point before reaching the problem of re-mapping props and slots from Twig Node to Drupal render array API.
And another issue is how to pass the render array to Twig for render throw `render_var`.
- Status changed to Active
5 months ago 4:23pm 31 July 2024 - 🇫🇷France pdureau Paris
In a way, it is more or less what SDC is doing:
ComponentsTwigExtension is adding 2 twig functions:
add_component_context
which is adding the attribute Attribute object if missingvalidate_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
andembed
) 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. - 🇫🇷France pdureau Paris
First proposal for
include
: https://git.drupalcode.org/project/ui_patterns/-/merge_requests/262/diffsIt was the easy one.
embed
will be an harder challenge - Merge request !267Issue #3449653 by pdureau: Execute normalization when Twig include and embed are used → (Merged) created by pdureau
- 🇫🇷France pdureau Paris
Another branch, so another MR, with another proposal: https://git.drupalcode.org/project/ui_patterns/-/merge_requests/267
Instead of calling the render element from
include
function,include
tag andembed
tag, we stay closer to the current SDC implementation:- reorganize our Twig modules visitors to be run before and after the SDC one
- move some logic from the render element to an internal Twig function run by one of the Twig modules visitors
- promote
include()
function as a replacement ofcomponent()
function
Why
include()
function? Because:include
tag is not recommended by the Twig documentation: bhttps://twig.symfony.com/doc/3.x/tags/include.html- as previously stated,
embed
tag is an overcomplicated, Twig specific, template altering mechanism which must be avoided at any cost
The crazy, ambitious, change is not forgotten and will be discusse din the Core issue: 📌 Trigger SDC render element when using Twig include or embed Active
- First commit to issue fork.
- 🇫🇷France just_like_good_vibes PARIS
I really like the new direction, because it will cope with current (bad) usages out in the wild and we will use a twig function instead of a custom twig function.
What i would expect in addition to this work, is a little bit of automatic testing. may i help with that maybe?
- 🇫🇷France just_like_good_vibes PARIS
if you agree, i can continue the work in the MR and add new kernel tests.
- Assigned to just_like_good_vibes
- Status changed to Needs work
29 days ago 11:04pm 19 November 2024 - 🇫🇷France pdureau Paris
yes, please, add the kernel tests because I never done such a thing
- 🇩🇪Germany Christian.wiedemann
Yes that is the way! Right now we "only" normalize the prop values. Shouldn't we support full UI Patterns support like.
{% embed "module:component" with { "ui_patterns": { props: { prop1: { value: value } } } }
So we move the preparing logic from the element to twig. Not sure if it goes too but from an architectural point of view it is straight forward and opens new possibilities.
- 🇫🇷France pdureau Paris
Hi Christian, it is great you like the change.
The data structure you are showing here is not compatible with the issue goal: being compatible with existing Twig mechanisms, to be compatible with the (kind of sad) state of existing SDC themes.
So, it must be this instead:
{% embed "module:component" with { prop1: value } %}
Also, in your example, you used
embed
which is a Twig tag we are not promoting. Our change is compatible with all Twig mechanisms but we are promoting onlyinclude()
function because:include
tag is not recommended by the Twig documentation: bhttps://twig.symfony.com/doc/3.x/tags/include.html- as previously stated,
embed
tag is an overcomplicated, Twig specific, template altering, mechanism which must be avoided at any cost
So, it must be this instead:
{{ include("module:component", { prop1: value }) %}
- 🇫🇷France pdureau Paris
Instead of adding this:
if (!isset($context['attributes'])) { $context['attributes'] = []; }
I beleive this would be better to move here a few lines from ComponentElementAlter::
// Attributes prop must never be empty, to avoid the processing of SDC's // ComponentsTwigExtension::mergeAdditionalRenderContext() which is adding // an Attribute PHP object before running the validator. $element["#props"]["attributes"]['data-component-id'] = $component->getPluginId();
- 🇫🇷France Grimreaper France 🇫🇷
Hi,
Possible to change a little bit the warning message to have "...function and will be removed before UI Patterns 2.0.0."?
The strong tag is not needed, only for this issue queue comment.