Reputation: 1449
I built a module called ap_news and it creates a few blocks. I have my custom block template files in "sites/all/modules/custom/ap_news/theme/". It all works but I want the designers to be able to override these tpl files by putting a copy of them in "sites/mysite.com/themes/theme428/templates/block" or in "sites/mysite.com/themes/theme428/templates/ap_news" So I'd like Drupal to look in the sites theme folder first and if it doesn't find it, then look in my modules theme folder. I tried it but it only uses the one in my module. Here is my block and theme code:
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),
),
);
}
UPDATE: I have created a function for now to look for the files.
'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;
}
}
Upvotes: 1
Views: 1563
Reputation: 19441
On problem is that you prepend the modules 'theme' folder to the template name, which will mess up drupals template suggestion lookup functionality - you should use the 'path' element for that, e.g.:
function ap_news_theme() {
$path_to_templates = drupal_get_path('module', 'ap_news') . '/theme';
return array(
'block-ap_news-all_news' => array(
'template' => 'block-ap_news-all_news',
'arguments' => array('data' => NULL, 'path' => NULL, 'sectionName' => NULL, 'sectionId' => NULL),
'path' => $path_to_templates,
),
'block-ap_news_national_news' => array(
'template' => 'block-ap_news_national_news',
'arguments' => array('block' => NULL),
'path' => $path_to_templates,
),
);
}
With this, it should still find the template files in your module, but overriding them from the theme folder should work.
However, I'm not sure if it would also find them in your proposed additional theme subfolders ('templates/ap_news' and 'templates/block'), so you should first test with the overrides placed directly in the theme (and the themes 'templates') folder directly.
Upvotes: 2