2013-01-10 59 views
0

在cron運行期間,我有一個模塊可以緩存許多節點的標記。我的問題是,在此cron運行期間,渲染函數中的任何標記都不會通過我的主題鉤子或模板。如何在cron運行期間爲我的模塊代碼設置主題Drupal 7

從我的模塊代碼中,我該如何選擇主題?有沒有鉤子?有沒有可以指定它的功能?

最後,我希望能夠做到這一點,就好像我是一個page_build掛機運行此得到相同的結果:

render(node_view($node, 'teaser')); 
render(node_view($node, 'mini_teaser')); 
+1

嘗試添加'global $ custom_theme; $ custom_theme ='theme_name';'到你的'hook_cron()'實現的頂部 – Clive

+0

這似乎沒有進入Drupal 7,但是在從Drupal 6中找到API入口時,我發現一個Drupal 7掛鉤用於設置主題:http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_custom_theme/7 – curiouser

回答

0

的Drupal 7有一個鉤子,允許模塊目前正在改變啓用主題:hook_custom_theme()

請注意,用於調用該鉤子的代碼如下所示。 (見menu_get_custom_theme()

// First allow modules to dynamically set a custom theme for the current 
// page. Since we can only have one, the last module to return a valid 
// theme takes precedence. 
$custom_themes = array_filter(module_invoke_all('custom_theme'), 'drupal_theme_access'); 
if (!empty($custom_themes)) { 
    $custom_theme = array_pop($custom_themes); 
} 
// If there is a theme callback function for the current page, execute it. 
// If this returns a valid theme, it will override any theme that was set 
// by a hook_custom_theme() implementation above. 
$router_item = menu_get_item(); 
if (!empty($router_item['access']) && !empty($router_item['theme_callback']) && function_exists($router_item['theme_callback'])) { 
    $theme_name = call_user_func_array($router_item['theme_callback'], $router_item['theme_arguments']); 
    if (drupal_theme_access($theme_name)) { 
    $custom_theme = $theme_name; 
    } 
} 

由於系統模塊實現了鉤子,如果你的情況下鉤首先執行一個模塊中實現hook_custom_theme()(例如,模塊的簡稱是custom_module),那麼系統模塊可以覆蓋您的模塊設置的主題。

通常,設置全局$custom_theme應該會得到相同的效果。確保已設置的主題已啓用。