Drupal - How can I programmatically get the theme name?

Using the theme manager is the proper Drupal 8 way of getting information about your theme.

\Drupal::service('theme.manager')->getActiveTheme()

A general rule in drupal 8 is look for the manager (/handler) service.

** Note: as Neograph734 pointed out, \Drupal::service('theme.manager')->getActiveTheme() will return the active theme object. If you want to get the theme machine-name then use \Drupal::service('theme.manager')->getActiveTheme()->getName()


This will do it:

$config = \Drupal::config('system.theme');    
print $config->get('default');

You can always use drush to explore your available configs:

drush config-list

and

drush config-list system

gave me a list:

...
system.rss
system.site
system.theme.global
system.theme
...

and then I could check with the following:

drush cget system.theme.global

and

drush cget system.theme

to finally find out that it holds a default property that was what you asked for.


  1. if you want to get actual active theme name administration theme included Use :

 $activeThemeName = \Drupal::service('theme.manager')->getActiveTheme(); 

  1. if you want your default selected theme theme used in front not admistartion theme Use :

 $defaultThemeName = \Drupal::config('system.theme')->get('default');    

Tags:

8