Problem/Motivation
Most of the effort in 8.x to "update the theme system" was focused primarily around converting everything to Twig.
While this is all well and good, Twig is ultimately the last step in the "theming" process.
We really didn't touch a lot of the theme system internals, just moved them around a bit into other OO classes/services.
We're still using antiquated hooks to define the theme registry and templates it may provide.
It's time we modernize it using existing systems.
See #2863819-22: Convert theme hooks (defined by hook_theme()) to be objects for more discussion regarding this topic.
Proposed resolution
Modernize the internals of the theme system.
History
There have been quite a few issues/discussions around this topic over the years. The following are a list of related issues for posterity sake:
- #2004872: [meta] Theme system architecture changes
- #1382350: [discussion] Theme/render system problems
Plan
Note: some issues can be worked on or implemented independently despite where it lives in its given "phase". This is, currently, just a rough estimation of the current scope of work and its logical progression.
Phase I
Convert current APIs (theme hooks) internals into proper OO code for continued BC support and reliable testability:
- #2954402: Refactor ThemeManager::render(), split into smaller immutable objects.
- #2863819: Convert theme hooks (defined by hook_theme()) to be objects
- #2957440: Refactor ThemeRegistry and Theme\Registry
- #2953921: Refactor out theme hook suggestion building from ThemeManager::render() into a separate function.
Phase II
Replace current APIs (theme hooks) with a modernized theme system:
- #2869859: [PP-1] Refactor theme hooks/registry into plugin managers
- #2809689: Move hook_theme implementations into yml
- #2809683: Make it required to specify variables passed to templates
- #2821376: Allow extending theme implementations
- #2809691: Make it possible to add libraries inside hook_theme implementation
- #2714509: Remove usages of #theme_wrappers
- #3016948: Type check theme variables
Phase III
Adding better DX/FX enhancements and utilities:
- #2316941: Use the builder pattern to make it easier to create render arrays
- #2972143: Create \Drupal\Component\Utility\ArrayObject
- #1804488: [meta] Introduce a Theme Component Library
- #2702061: Unify & simplify render & theme system: component-based rendering (enables pattern library, style guides, interface previews, client-side re-rendering)
- #2818121: Create alternative to Twig include function to improve Drupal integration
- #2809679: Add versions for render arrays
- #1804488: [meta] Introduce a Theme Component Library
Phase IV
Finally fixing render arrays of doom:
Comments
Comment #2
lauriiiThis issue was created after a discussion with the @joelpittet and @Cottser in BadCamp.
Comment #3
pixelmord commentedThis is great! I really appreciate this pragmatic approach to work towards the #components goal in increments, the beauty is that even without that, these steps totally make sense.
Comment #4
cyb_tachyon commentedSo it looks to me that the next real step for those of us currently using a components theming approach* depends on the following two issues:
But I don't think there's an easy way to get #2809689 into the components module, so we're stuck until 8.3. Still, exciting stuff. It'd be nice to simply add components with a drupal-specific yaml file in their directory and call them in a presenter template with a theme call.
Thanks again @lauriii for putting this together!
* Either using twig include + blocks or twig include + some sort of pre-render
Comment #5
fabianx commented#4: #2809689: Move hook_theme implementations into yml while you can't _move_ things, we could as first step _allow_ to define hook_theme implementations in YML and yes that could start easily within the components module.
So there is not a need to wait for core on that.
Comment #6
dawehnerIs there a reason this is not in the idea issue queue? Just curious.
Comment #7
lauriii@dawehner: Not sure if this belongs there since this is not an idea but instead just a meta issue that explains a vision for this subsystem.
Just quoting the drupal core governance docs:
Comment #8
fabianx commented#6: It is not an idea, but several small concrete actionable issues we have been talking about for years.
At best it belongs to the components initiative (in a way) as it lays some groundwork for it. But components is frozen right now. Also some of this issues are implemented and prototyped in contrib first.
But all the changes / tasks are way within the scope of the theme subsystem, so this should indeed be sufficient.
Also there is no sign-off needed for the whole concept as again this is not a whole initiative, but rather some tasks that make each in themselves sense and are independent.
I feel that is the biggest difference.
Comment #10
pwolanin commentedComing here from another issue - creating render arrays in the Twig template seems like an anti-pattern and I don't even see why we'd support it. Perhaps I'm not understanding the intended feature.
This is less problematic if it's only for specific render elements and the allowed keys can be validated.
What I don't want to support is actually writing render arrays directly as arrays in Twig since we may need to block that to resolve #2860607: Code execution via Twig templates (including inline)
Comment #11
cyb_tachyon commented@pwolanin I think you may be intending to comment on #2818121: Create render array generator that can be used in Twig.
This work is being informed by 2702061: Unify & simplify render & theme system: component-based rendering (enables pattern library, style guides, interface previews, client-side re-rendering) and is attempting to solve the pipeline issues currently present when attempting to integrate a Twig Component library with a Drupal theme and backend.
As I understand it, the plan is to eventually move away from render arrays, and instead focus on experimenting with the "best" ways to get Drupal backend data (which may be organized completely differently from the frontend structure) into a component frontend. For now though, we are building these experiments in Contrib modules on Drupal.org with the idea that a solid implementation will eventually evolve and be ready to include in Drupal 8 Core.
Comment #12
manuel garcia commentedAdding related issue for visibility. I a bit confused by the different efforts going on in this area of Drupal...
Comment #15
markhalliwellThis has stagnated, tremendously.
The IS needs to be updated and reorganized.
Comment #16
markhalliwellComment #17
markhalliwellComment #18
markhalliwellThe IS needs to ultimately be reformatted matching: https://www.drupal.org/core/initiative-proposal-template
I would like to make this an official initiative.
Comment #19
borisson_Should we move this issue to the ideas queue to make for easier to coordinate this initiative?
Comment #21
lauriiiComment #22
lauriii