【发布时间】:2012-01-09 17:18:48
【问题描述】:
我构建了一个名为 ap_news 的模块,它创建了几个块。我在“sites/all/modules/custom/ap_news/theme/”中有我的自定义块模板文件。一切正常,但我希望设计人员能够通过将它们的副本放在“sites/mysite.com/themes/theme428/templates/block”或“sites/mysite.com/themes/theme428”中来覆盖这些 tpl 文件/模板/ap_news" 因此,我希望 Drupal 先查看站点主题文件夹,如果找不到,则查看我的模块主题文件夹。我试过了,但它只使用我模块中的那个。 这是我的块和主题代码:
function ap_news_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case "list":
// Generate listing of blocks from this module, for the admin/block page
$block = array();
$block['ap_news_national_news']['info'] = t('National news');
$block['ap_news_national_news']['cache']=BLOCK_NO_CACHE;
$block['ap_news_world_news']['info'] = t('World news');
$block['ap_news_world_news']['cache']=BLOCK_NO_CACHE;
return $block;
break;
case "view":
switch ($delta) {
case 'ap_news_national_news': // block-ap_news_national_news.tpl.php
// Generate our block content
$html = ap_news_nationalNews();
$block['subject'] = 'National News';
$block['content'] = $html;
$data = new stdClass(); $data->module = 'ap_news'; $data->content = $html; $data->delta = $delta; $data->subject = 'National News';
$block['content'] = theme('block-'.$delta, $data);
break;
//--------------------
case 'ap_news_world_news': // block-ap_news-world_news.tpl.php
$data = ap_news_allNews_block('WORLD', APNEWS_CID_WORLD_NEWS, 4);
$block['subject'] = 'World News';
$block['content'] = theme('block-ap_news-all_news', $data, base_path().APNEWS_PATH_WORLD_NEWS, 'World News', 'worldNews');
break;
}
}
return $block;
}
function ap_news_theme() {
return array(
'block-ap_news-all_news' => array(
'template' => 'theme/block-ap_news-all_news',
'arguments' => array('data' => NULL, 'path' => NULL, 'sectionName' => NULL, 'sectionId' => NULL),
),
'block-ap_news_national_news' => array(
'template' => 'theme/block-ap_news_national_news',
'arguments' => array('block' => NULL),
),
);
}
更新: 我现在已经创建了一个函数来查找文件。
'block-ap_news-national_news' => array(
'template' => 'block-ap_news-national-news',
'arguments' => array('block' => NULL),
'path' => ap_news_templatePath('block', 'block-ap_news-national-news', 'ap_news'),
),
/*
* Find the template paths.
* First look in the sites custom theme template folder (/themes/theme428/templates/ap_news),
* then in sites normal theme template folder,
* then in the modules folder
*/
function ap_news_templatePath($type, $template, $custom=''){
$siteThemePath = path_to_theme() . '/templates/' . $type. '/';
$siteCustomThemePath = path_to_theme() . '/templates/' . $custom. '/';
$moduleThemePath = drupal_get_path('module', 'ap_news') . '/theme/';
if(file_exists($siteCustomThemePath . $template . '.tpl.php')){
return $siteCustomThemePath;
}elseif(file_exists($siteThemePath . $template . '.tpl.php')){
return $siteThemePath;
}else{
return $moduleThemePath;
}
}
【问题讨论】:
-
你能发布你的
hook_theme()块主题的实现吗?另外,哪个 Drupal 版本? -
我正在使用 Drupal 6.20。我的原始帖子中有 hook_theme,除非我遗漏了什么。我不知道我的积木需要一个单独的主题挂钩。我错过了什么?谢谢
-
对不起,我不知何故第一眼就没有注意到
hook_theme()代码。
标签: templates drupal drupal-6 themes overriding