Hello guys,

In Drupal 8 I am trying to install a theme programmatically.

I try to do this via a custom module and my ***.info.yml file looks like:

name: Setup site
type: module
core: 8.x
dependencies:
  - ctools
themes:
  - adminimal_theme

But when I install this module, my theme doesn't get installed.

What am I missing here ?
It works perfectly for modules (ctools in this example.)

Kind regards
Michael

Comments

sqndr’s picture

See also this StackOverflow question.

The goal for a theme is to be module independent - and this also goes the other way around. Modules can't have dependencies on themes and themes can't have dependencies on modules.

Maybe try working with an install profile?

Jeff Burnz’s picture

Hopefully this will change and themes can have dependancies (as they should be able to have) https://www.drupal.org/node/474684

dxvargas’s picture

You can use ***.install file with this:

/**
 * Implements hook_install().
 */
function ***_install() {
  \Drupal::service('theme_installer')->install(['adminimal_theme']);
}

Jeff Burnz’s picture

And to set as default:

\Drupal::service('theme_handler')->setDefault('mytheme');

jaapjan’s picture

If you get this exception RuntimeException: Recursive router rebuild detected. in Drupal\Core\Routing\RouteBuilder->rebuild() (line 121 of core/lib/Drupal/Core/Routing/RouteBuilder.php) you might want to consider moving the code to hook_modules_installed(). Like this:

/**
 * Implements hook_modules_installed().
 *
 * @param $modules
 */
function MYMODULE_modules_installed($modules) {
  if (in_array('MYMODULE', $modules)) {
    $theme = 'MYTHEME';
    \Drupal::service('theme_installer')->install([$theme]);
    \Drupal::service('theme_handler')->setDefault($theme);
  }
}