Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
In order for themes to do things including using hook_requirements() to verify various components (installation of 3rd party js/css libraries, that other dependent modules containing JS/CSS lib's that would be expected by said theme) are installed, active and ready to be accessed, themes need a .install file.
original related issue: #1067408 wanted to add this and up until #119 (!) it was there. Then it wasn't.
Proposed resolution
Add it back.
Remaining tasks
User interface changes
none. D/X enhancement.
API changes
Themes can implement hook_install. No BC break.
Comments
Comment #2
darol100 CreditAttribution: darol100 as a volunteer and commented1+, For this feature. We definitely need this to modify configuration when the theme is been installed. There is not a way to do this right now, which make contrib theme to have module and/or profile as dependency.
Comment #3
dawehnerIf we want that, I would suggest that themes can have dependencies to modules ... hacking that into install would make the experience in drush/console/composer worse than it would have to be.
Comment #4
dawehnerchx explained me a bit more about it, sorry!
Comment #5
dcrocks CreditAttribution: dcrocks as a volunteer commentedhook_theme_installed and hook_theme_enabled are mentioned as a @todo from #122 thru #145. After that hook_theme_enabled is implemented but there is no further mention of hook_theme_install. There are several comments as to the need for hook_theme_installed though.
Comment #6
dawehnerLet's see how it goes
Comment #7
darol100 CreditAttribution: darol100 as a volunteer and commented@dawehner,
#3
I'm not hacking the install with Drush/DrupalConsole instead after installing the theme we will recommend to run a script to import the configuration instead of doing manual work and/or having a module as dependency to import configuration.
Here is an example of what I was talking about - https://www.drupal.org/node/2652562
Comment #8
darol100 CreditAttribution: darol100 as a volunteer and commentedI was able to run hook_install/hook_uninstall inside of a theme.
best_theme_ever.install
Moving it to RTBC.
Comment #9
star-szrFor the uninstall could the docblock say "tear down" or something instead of "setup"?
Otherwise looks good, thanks!
Comment #10
darol100 CreditAttribution: darol100 as a volunteer and commentedRe-roll patch base on #9.
Comment #11
catchNeeds documentation.
Moving to 8.1.x since it's an API addition.
Looks great apart from that though.
Comment #12
joelpittetRelating a couple issues that have the general direction of making Themes have more features that modules have.
Comment #13
hass CreditAttribution: hass commentedWhat about updates? We need this to run version updates.
Comment #14
joelpittetOh yeah, maybe we can actually move this to #187289: Support .install files for themes and get
hook_update_N()
and hook_install in one go at it?Comment #15
catchI think this should be a duplicate of #474684: Allow themes to declare dependencies on modules - because theme project folders can already contain modules. Once you add dependencies, you get hook_install() (and updates, and everything else) for free.
See discussion in #2002606: Allow themes to provide services.yml also.
Comment #16
joelpittetComment #18
SKAUGHT+1
Comment #19
SKAUGHTComment #20
cilefen CreditAttribution: cilefen commented@SKAUGHT Is this an RC target for 8.1.x?
Comment #21
SKAUGHTI would hope so.
I currently am talking it back to needs work, as i've just touched up the summary, but realize the current patch doesn't directly include a routine for hook_requirements().
Comment #22
cilefen CreditAttribution: cilefen commentedIt may be too late for RC for this one.
Comment #23
SKAUGHTi've been testing this patch. although is does currently allow for a theme to have a .install file and hook_install/uninstall it certainly does not magically pickup a hook_requirements, as is.
following the basic pattern of this patch i can get it to begin a _requirements callback. but then is it failing to properly respond to and $phase switch. Sadly, at this time, I can not see way to properly continue with this patch myself.
Comment #24
SKAUGHTComment #25
SKAUGHTupdating issue title.
Comment #29
killua99 CreditAttribution: killua99 commentedHere is a patch rerolling.
I add an extra function inside theme.inc to provide better the require_once
Also I might consider this issue as a bug and not as a task. This features was wrongly removed and add the bug to don't support .install for themes.
I couldn't add a interdiff since the changes are from the 8.1.x branch and quite old.
Comment #32
tobiasbReroll.
Comment #34
markhalliwellI think it'd be more appropriate to simply proxy theme events via a
ThemePluginManager
(#2869859: [PP-1] Refactor theme hooks/registry into plugin managers) since themes aren't allowed to define services for events, see: https://cgit.drupalcode.org/plus/tree/src/ThemePluginManager.php?h=8.x-4....Similar to #2206347: Use event system in ModuleHandler.
As a last resort, I agree with @catch in that #474684: Allow themes to declare dependencies on modules could assist with this... albeit, in a very indirect way.
Regardless, introducing .install files for themes now feels like a step backwards, not forwards.
Comment #35
bkosborneI'm here as I was surprised to find that while themes can define default configuration, they cannot define update hooks to modify that configuration or install new configuration if needed. For example, I have a Drupal 9 platform with themes that provide default config entities for the Layout Builder Styles and CKEditor Standlone Styles modules. If I want to make changes or add new config entities for sites that already exist and use those themes, it seems my only hope is to create a module, make that module a dependency of the theme, and put the config and update hooks in there?