Last Updated: November 24, 2016
·
1.627K
· webinity

Enable a Drupal theme from a custom module

Sometimes you are required to enable a theme programmatically from a module without needing to do it manually all the time, so to do this I have used the hook_init() function to get a list of all the themes in Drupal which contains all the themes info.

Then I am using the name of the theme (the one in the .info file) and after checking if the status is disabled I call themeenable function and pass an array with the theme name I want to enable. The themeenable function accepts an array of theme names, so you are able to enable multiple themes at once.

function mymodule_init()
{
    $themes = list_themes(); //Get all the available themes
    if($themes['mythemename']->status == 0)
    {
            //if mythemename status is 0 i.e. disabled
        theme_enable(array('mythemename')); //enable theme
    }
}

1 Response
Add your response

I would recommend to not use the init hook for that. If you look at https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_init/7.x you can see that this hook is called for every uncached page request. This logic should just be called on installing your module, so hookenable() or hookinstall() would probably be a better choice.

over 1 year ago ·