- Issue created by @herved
- 🇧🇪Belgium herved
I found out about 🌱 [meta] Themes improperly check renderable arrays when determining visibility Needs work and I'm trying to wrap my head around it.
FWIW here are 2 patches as mentioned in the description.
With this approach preprocess hooks are responsible to bubble up cache metadata.
Are there any downsides to doing this? - last update
over 1 year ago 29,958 pass - last update
over 1 year ago Custom Commands Failed - last update
over 1 year ago 29,933 pass, 7 fail - 🇧🇪Belgium herved
+++ b/core/themes/engines/twig/twig.engine @@ -48,14 +42,27 @@ function twig_render_template($template_file, array $variables) { + if (empty(trim($rendered_markup))) {
Duh,
empty()
is the wrong condition... let's trytrim($rendered_markup) === ''
- last update
over 1 year ago 29,958 pass - 🇩🇪Germany donquixote
So the origin for this hack was, if I remember correctly, the observation that we cannot reliably determine if something is "empty" before all preprocess functions have run.
I think we would want to implement this hack in the last of all preprocess functions.
Therefore, would it make sense to introduce another callback that runs _after_ preprocess, that is specifically made for emptiness checks?
Also, perhaps we need a convincing test case to get a better grip on what we are trying to accomplish.
- 🇩🇪Germany donquixote
Another interesting solution could be if a template could abort early and force empty output.
This would probably need to be done in twig directly. - 🇧🇪Belgium herved
Hi donquixote, thanks for jumping in :)
Indeed, we are currently adding our preprocess last using
hook_theme_registry_alter()
and I remember we also had to usehook_module_implements_alter()
to place our theme_registry_alter in last position because of the field_group module also implementing it. What a mess ^^
So yes, that would make total sense. Perhaps apostprocess
hook ?Also, perhaps we need a convincing test case to get a better grip on what we are trying to accomplish.
True, I will try to write some scenarios tomorrow.
- 🇧🇪Belgium herved
The process layer would have been a good candidate to achieve this but it was removed in D8 #1843650: Remove the process layer (hook_process and hook_process_HOOK) →
So I have no idea what we could do here. Pretty sure we don't want to reinstate it...In terms of scenarios: region (content var), field (items var), layout (content var) and paragraph (content var) are some examples of theme hooks where those variables can contain a complex render array which turns out to be completely empty after rendering.
By complex render array I mean not just#cache
and#access
but nested render arrays. - 🇺🇸United States euk
Here is a simpler path with a config option available - https://www.drupal.org/project/drupal/issues/3400912 ✨ Make Twig debug comments optional for empty markup. Active
- 🇧🇪Belgium herved
Thanks @euk
I thought about creating a specific issue for it but I see you did it already, I'll have a look at it, thanks.
It looks like a better approach than #6 here (BTW don't use that one it's messing the order of debug markup).
The approach in #3400912 makes it non disruptive by using a switch, which is interesting but could also make it harder to solve the other issue I'm trying to solve here.
I'll leave this open for now. - 🇺🇸United States euk
@herved, thanks in advance! Would be lovely to get the config option incorporated.
- 🇧🇪Belgium herved
Hiding patch: prevent_twig_debug_only-3380299-6.patch (covered by ✨ Make Twig debug comments optional for empty markup. Active )
The other patch (allow_preprocess_abort_rendering-3380299-4.patch) is still relevant though. - 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦
In a similar vein, it would be useful if a preprocess function could change which template is actually used.
Instead of
#abort_render
, there could be a way to set a different template name or set it to FALSE to abort rendering.