6

I've tried to set a theme as default theme in drupal 8 using the following code

> $config = \Drupal::configFactory()->getEditable('system.theme');  
> $config->set('default', 'test_theme')->save();

On installing the module the code is not setting the theme as the one I've entered. The code is written in hook_install().

2 Answers 2

9

The current advice is to set this via config:

\Drupal::configFactory()
  ->getEditable('system.theme')
  ->set('default', $theme)
  ->save();

The previous method using ThemeHandlerInterface::setDefault() is now deprecated.

You might need to install it first too, I'm not sure. In case you do:

\Drupal::service('theme_installer')->install(['test_theme']);
1

I just want to contribute to this answer by adding that there's no need to worry about running both of these commands if the theme is already installed. It technically runs the theme_installer service underneath and checks beforehand for installed themes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.