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.

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chx created an issue. See original summary.

darol100’s picture

1+, 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.

dawehner’s picture

which make contrib theme to have module and/or profile as dependency.

If 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.

dawehner’s picture

Assigned: Unassigned » dawehner

chx explained me a bit more about it, sorry!

dcrocks’s picture

hook_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.

dawehner’s picture

Assigned: dawehner » Unassigned
Status: Active » Needs review
FileSize
3.97 KB

Let's see how it goes

darol100’s picture

@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

darol100’s picture

Status: Needs review » Reviewed & tested by the community

I was able to run hook_install/hook_uninstall inside of a theme.

best_theme_ever.install

<?php
function best_theme_ever_install(){
  drupal_set_message(t('hook_install work =))'), 'status');
}

function best_theme_ever_uninstall(){
  drupal_set_message(t('hook_uninstall work =))'), 'status');
}

Moving it to RTBC.

star-szr’s picture

+++ b/core/lib/Drupal/Core/Render/theme.api.php
@@ -1273,5 +1273,21 @@ function hook_template_preprocess_default_variables_alter(&$variables) {
+ * Perform setup tasks when the theme is uninstalled.
+ *
+ * @see \Drupal\Core\Extension\ModuleInstaller::uninstall
+ */
+function hook_uninstall() {

For the uninstall could the docblock say "tear down" or something instead of "setup"?

Otherwise looks good, thanks!

darol100’s picture

catch’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Extension/ThemeInstaller.php
@@ -213,6 +219,13 @@ public function install(array $theme_list, $install_dependencies = TRUE) {
+  protected function loadThemeInstallInc(Extension $theme) {

Needs documentation.

Moving to 8.1.x since it's an API addition.

Looks great apart from that though.

joelpittet’s picture

Relating a couple issues that have the general direction of making Themes have more features that modules have.

hass’s picture

What about updates? We need this to run version updates.

joelpittet’s picture

Oh 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?

catch’s picture

I 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.

joelpittet’s picture

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

SKAUGHT’s picture

SKAUGHT’s picture

Version: 8.2.x-dev » 8.1.x-dev
Issue summary: View changes
Status: Needs work » Needs review
cilefen’s picture

@SKAUGHT Is this an RC target for 8.1.x?

SKAUGHT’s picture

Status: Needs review » Needs work

I 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().

cilefen’s picture

It may be too late for RC for this one.

SKAUGHT’s picture

i'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.

SKAUGHT’s picture

Version: 8.1.x-dev » 8.2.x-dev
SKAUGHT’s picture

Title: Add (back) hook_install for themes » Add .install file abilities for Themes

updating issue title.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

The last submitted patch, 10: 2652542-Add-back-hook_install-for-themes-10.patch, failed testing.

The last submitted patch, 10: 2652542-Add-back-hook_install-for-themes-10.patch, failed testing.

killua99’s picture

Here 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.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tobiasb’s picture

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

markhalliwell’s picture

Status: Needs review » Closed (won't fix)
Related issues: +#2206347: Use event system in ModuleHandler

I 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.

bkosborne’s picture

I'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?