2

I need to programmatically update my default theme and disable bartik on a live site. I know I need to update the config. In Drupal 7 I would have used theme_enable(), but I am not sure how to do this in Drupal 8. Below my function sets 'mycustomtheme' by default but it does not enable the theme. Thanks in advance.

  function mysite_deploy_update_8001() {
      \Drupal::configFactory()
      ->getEditable('system.theme')
      ->set('default', 'mycustomtheme')
      ->save();
    }
7

Use the theme installer service to enable and disable themes .

Install a list of themes:

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

Uninstall a list of themes:

\Drupal::service('theme_installer')->uninstall(['mytheme']);

Reference: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21ThemeInstaller.php/function/ThemeInstaller%3A%3Ainstall/8.2.x

Instead of writing to the config you can use the theme handler to set a default theme:

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

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.