代码之家  ›  专栏  ›  技术社区  ›  Kevin

检索当前的Drupal主题?

  •  3
  • Kevin  · 技术社区  · 14 年前

    在模块的上下文中,如何确定为当前用户加载的主题?

    drupal_get_path
    path_to_theme
    

    这两种方法都不好,因为它们似乎只在template.php主题中工作。

    2 回复  |  直到 12 年前
        1
  •  3
  •   avpaderno    14 年前

    如果允许用户为自己选择主题,则他们选择的主题将保存在 $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');
    }
    
        2
  •  1
  •   googletorp    14 年前

    path_to_theme 应该工作得很好,我在两个Drupal安装上测试了它,两个都工作了。如果主题尚未初始化, 路径主题 会这样做的,这是Drupal内部用来设置不同的全局主题变量的方法,比如 $theme_path ,这是您要查找的变量。