Drupal API有 drupal_get_path($type, $name) ,它将提供任何路径特定的主题或模块。如果我想要当前主题的路径怎么办?

有帮助吗?

解决方案

使用 path_to_theme 功能。

其他提示

这应该有用( doc ):

global $theme;
$path = drupal_get_path('theme', $theme);

// there's also a $theme_path global

global $theme_path;

在D6中,path_to_theme()可能不会以您期望的方式运行,具体取决于您使用它的方式。如果你在任何主题预处理函数之外使用它,那么它可能会给你你想要的东西,但是如果在模块的主题/预处理钩子函数的上下文中调用它...它将指向模块路径宣布主题。

实施例。如果我有一个主题<!>“my_theme <!>”;和我的模块<!> quot; my_module <!> quot;使用预处理挂钩覆盖论坛主题,在我的模块中调用path_to_theme():例如my_module_preprocess_forums()...将返回<!> quot; forums <!> quot;,而不是<!>“my_theme <!>”;正如人们所预料的那样。

如果你问我,果味很浓。

在Drupal 7中,为获取当前主题的路径,我们可以使用: path_to_theme()功能。

在Drupal 8中

global $base_url;
$theme = \Drupal::theme()->getActiveTheme();
$image_url = $base_url.'/'. $theme->getPath() .'/images/image.jpg';

在Drupal 5中,您只需使用:path_to_theme()

这将为您提供从Drupal根目录到特定主题目录的完整路径。请注意,它不包括尾部斜杠。

在Drupal 6中,这种行为略有不同。如果你从你的页面中调用它,它将调用当前正在进行主题的任何内容...这是你的主题,模块等。以下是API文档的关键引用:

  

它可以指向活动主题或   处理主题的模块   实现。例如,何时   在主题范围内调用   打电话取决于哪里   主题功能被处理。如果   从模块实现,它将   指向模块。如果实施   从活跃的主题来看,它会指出   活跃的主题。什么时候叫   在主题通话范围之外,   它总是指向活跃的   主题。

来源: http://api.drupal.org/api/function/path_to_theme

对于D8,主题文件夹在预处理功能中可用:

function hook_preprocess_page(&$variables) {
  $variables['some_logo_file'] = "/{$variables['theme']['path']}/images/logo.png";
}

page.html.twig:

<img src="{{ logo_src }}">
scroll top