Drupal API 提供了 drupal_get_path($type, $name)
函数,用于获取特定主题或模块的路径。如果我想获取当前主题的路径怎么办?
Drupal API 提供了 drupal_get_path($type, $name)
函数,用于获取特定主题或模块的路径。如果我想获取当前主题的路径怎么办?
path_to_theme
函数。这应该可以正常工作 (文档):
global $theme;
$path = drupal_get_path('theme', $theme);
// there's also a $theme_path global
global $theme_path;
path_to_theme()
,而不是使用$theme_path
。 - apaderno$themeHandler = \Drupal::service('theme_handler');
$themePath = $themeHandler->getTheme($themeHandler->getDefault())->getPath();
global $base_url;
$theme = \Drupal::theme()->getActiveTheme();
$image_url = $base_url.'/'. $theme->getPath() .'/images/image.jpg';
$themePath = \Drupal::service('extension.list.theme')->getPath('my_theme_name');
或者
$themePath = \Drupal::service('extension.path.resolver')->getPath('theme', 'my_theme_name');
在Drupal 5中,您可以简单地使用:path_to_theme()
这将为您提供从Drupal根目录到特定主题目录的完整路径。请注意,它不包括尾随斜杠。
在Drupal 6中,它的行为略有不同。如果您在页面内调用它,它将调用当前正在进行主题设置的任何内容...无论是您的主题、模块等等。以下是API文档中的关键引用:
它可以指向活动主题或处理主题实现的模块。例如,在主题调用范围内调用时,它将取决于主题函数的处理位置。如果从模块中实现,则会指向该模块。如果从活动主题中实现,则会指向活动主题。当在主题调用范围之外调用时,它将始终指向活动主题。
对于D8,主题文件夹在预处理函数中是可用的:
function hook_preprocess_page(&$variables) {
$variables['some_logo_file'] = "/{$variables['theme']['path']}/images/logo.png";
}
page.html.twig:
<img src="{{ logo_src }}">