2010-07-19 41 views
3

在模塊的上下文中,如何確定爲當前用戶加載哪個主題?檢索當前Drupal主題?

drupal_get_path 
path_to_theme 

這些都不是什麼好事,因爲它們似乎只適用於主題的template.php。

回答

3

如果允許用戶爲自己選擇主題,則他們選擇的主題將保存在$user->theme中,其中$user是用戶對象。 如果模塊設置了自定義主題,則全局變量$custom_theme包含當前設置的主題的名稱。

下面的代碼片段在$current_theme保存主題名稱當前活動:

global $custom_theme, $theme, $user; 

if (!empty($user->theme)) { 
    $current_theme = $user->theme; 
} 
elseif (!empty($custom_theme)) { 
    $current_theme = $custom_theme; 
} 
else { 
    $current_theme = $theme ? $theme : variable_get('theme_default', 'garland'); 
} 
1

path_to_theme應該工作得很好,我測試了兩個Drupal安裝,並且都工作。如果主題還沒有初始化,path_to_theme會這樣做,這是Drupal內部用來設置不同的全局主題變量,如$theme_path,這是您正在尋找的變量。

+0

嗯,這給了我模塊路徑時,我前幾天試了一下 – Kevin 2010-07-19 15:08:39

+0

@googletorp,都能跟得上 - 這取決於當調用'path_to_theme()'時的作用域。從文檔:>當在主題調用範圍內調用時,它將取決於處理主題函數的地方。如果從一個模塊實現,它將指向該模塊。如果從活動主題實施,它將指向活動主題>來源:[Drupal API](http://api.drupal.org/api/drupal/includes!theme.inc/function/path_to_theme/6) – 2012-03-27 23:42:53