Problem/Motivation
#2228093: Modernize theme initialization removed the ability for themes to implement system-wide alter hooks and instead limits to a certain "whitelist" due to the nature of the caching system in 8.x. This "whitelist" does not contain hook_registry_theme_alter().
This is a critical hook that should allow themes to alter the theme registry before any rendering happens.
This blocks the port of Bootstrap which uses this hook to implement it's dynamic file system for splitting out complex code as well as altering existing theme hooks to counter many core theme system issues that have not been addressed. Bootstrap currently has > 50k installs in 7.x.
Proposed resolution
Add this alter hook back.
Sadly its a bit tricky because of the following reasons:
- The ThemeManager depends on the registry (for theme()), but the Registry now depends on the ThemeManager for executing the alter "hook",
so we need setter injection in order to resolve the circular dependency - The theme registry is built by theme, so we want to alter by the theme of the registry object. Therefore we need
ThemeManager::alterByTheme($theme, ...)
Remaining tasks
User interface changes
None.
API changes
Allows themes to implement hook_registry_theme_alter() again.
| Comment | File | Size | Author |
|---|---|---|---|
| #31 | 2448847-31.patch | 12.14 KB | arlinsandbulte |
| #17 | interdiff.txt | 599 bytes | dawehner |
| #17 | 2448847-17.patch | 11.79 KB | dawehner |
| #15 | interdiff.txt | 969 bytes | dawehner |
| #15 | 2448847-15.patch | 11.85 KB | dawehner |
Comments
Comment #1
fabianx commented#2228093: Modernize theme initialization said that hook_theme_registry_alter() was allowed, is that a bug in the code?
Comment #2
dawehnerNumber two.
Comment #4
fabianx commentedComment #5
webchickHm. Can you explain how this meets the definition of critical? https://www.drupal.org/node/45111
Note especially this part:
I find it a bit difficult to believe that it's impossible to theme at all without this being fixed, but it sounds solidly major since it falls under "Issues that have significant repercussions but do not render the whole system unusable (or have a known workaround) are marked major."
Comment #6
markhalliwellComment #7
markhalliwellComment #8
dawehnerLet's try that.
Comment #10
dawehnerAnother one.
Comment #11
fabianx commentedThis looks duplicated.
Missing doxy
This todo should be at alterForTheme.
Comment #12
dawehnerAnd here is a test.
Comment #13
fabianx commentedThese lines are still duplicated - unless I am missing something.
Comment #15
dawehnerYou are absolute right.
Comment #17
dawehnerMaybe this helps a lot.
Comment #18
dawehnerAdapted the issue summary.
Comment #19
kim.pepperDocs missing
Docs missing
Should not contain trailing text.
Formatting was mixed up here.
Needs full stop.
Also, needs an update to the change notice because we are changing the API.
(Reviewed with @jibran & @dashaforbes) during critical office hours)
Comment #20
jibran@dawehner thanks for the patch. During our review we discussed why this is breaking a circular dependency.
theme manager is only using $themeRegistry in ThemeManager::theme() function. So for all normal workflow we can safely assume that theme registry will be available for theme function.
I can only come up with one scenario in which theme registry will not be available for theme function.
When we are rebuilding the container and before building theme registry we hit some actual circular dependency in that case we try to render the theme for displaying error/exception when there is no theme registry. It'd be good if we can add test for this and handle this boundary case.
Comment #21
dawehnerOh, its not that hard as you think:
The
@theme.managerservice needs the@theme.registryservice, and the other way round.Comment #22
dawehnerDo we? We have an alter hook, which is still documented in
theme.api.php, yes, theThemeManagerInterfacechanged, but IMHO this is not something we need a dedicated change record for
Comment #23
dawehner.
Comment #24
larowlanAll bets are off when we get an error that early, an invalid constraint plugin will manifest as a twig error, but actually, its unrelated.
Not sure we should babysit this.
Boldy setting to rtbc
Comment #25
fabianx commentedRTBC + 1
Comment #26
alexpottI think it is worth documenting that the theme's base themes hooks will be invoked as well. Because that is the first thing I wondered when thinking about the patch.
Comment #27
dawehnerThat is a good idea!
Comment #28
jibranBack to RTBC.
Comment #29
arlinsandbulte commentedSimple grammar edit to #27:
- * Similar to ::alter it invokes also the alter hooks for the base themes.
+ * Similar to ::alter it also invokes the alter hooks for the base themes.
Comment #30
tim.plunkettThanks, that reads better.
Comment #31
arlinsandbulte commentedI just can't leave well enough alone. Added a comma to my edit for even better, proper grammar:
- * Similar to ::alter it also invokes the alter hooks for the base themes.
+ * Similar to ::alter, it also invokes the alter hooks for the base themes.
Leaving at RTBC.
Comment #32
alexpottThis issue addresses a critical bug and is allowed per https://www.drupal.org/core/beta-changes. Committed 08ce5a7 and pushed to 8.0.x. Thanks!
Comment #35
xjm