2011-11-16 297 views

回答

8

您可以使用hook_custom_theme()

function mymodule_custom_theme() { 
    if ($some_condition_is_true) { 
    return 'my_theme'; 
    } 
} 

如果需要立足選擇的路徑,然後去最好的辦法是重寫特定菜單路由器項目的theme callbackSee here for an example

1

儘管我不確定當您想要更改主題時需要什麼條件,但是如果您想根據url,節點類型,分類術語,視圖頁等來更改主題,那麼您可以使用Context模塊,它會爲你做這件事,你甚至不需要編寫一行代碼。檢查了這一點:http://drupal.org/project/context

這是一個非常有用的模塊,並與幾乎所有著名的模塊,如面板,歐米茄主題,三角洲不錯整合等

0

Drupal的變量theme_default是你必須設置切換主題之一使用variable_set函數。

variable_set('theme_default', 'your_theme_name'); 

如果您已經安裝了自定義模塊,則可以通過hook_update_N更改默認主題。此外,請確保您在hook_install中調用代碼以在安裝期間運行它,以防您想與其他人共享您的模塊並想要在安裝期間更改活動主題。

/** 
* Implements hook_update_N(). 
*/ 
function mymodule_update_7000() { 
    $theme_list = array(
    'bootstrap', 
    'mytheme', 
    'shiny', 
); 
    theme_enable($theme_list); 
    $theme_default = 'mytheme'; 
    // The below code would change the default active theme to 'mytheme' 
    variable_set('theme_default', $theme_default); 
    $admin_theme = 'shiny'; 
    variable_set('admin_theme', $admin_theme); 
} 
0

雖然variable_set()將爲hook_install()hook_update_N()工作,你不應該在一個模塊內使用它。調用variable_set()會清空cache_bootstrap表,這在繁忙網站上會嚴重影響性能。如果你不需要Context的全部功能,我會推薦ThemeKey module。但是,上下文很容易導出用於版本控制,而據我所知,無法導出ThemeKey規則。