2012-01-09 159 views
1

我构建了一个名为ap_news的模块,它创建了几个模块。我在“sites/all/modules/custom/ap_news/theme /”中有我的自定义块模板文件。这一切都有效,但我希望设计师能够通过将这些文件的副本放在“sites/mysite.com/themes/theme428/templates/block”或“sites/mysite.com/themes/theme428”中来覆盖这些tpl文件/ templates/ap_news“ 所以我想Drupal首先查看网站主题文件夹,如果找不到,请查看我的模块主题文件夹。我试过了,但它只使用我模块中的那个。 这里是我的块和主题代码:Drupal 6重写模块模板文件

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: 我创建了一个功能,现在寻找的文件。

 '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; 
    } 

} 
+0

你可以发布你的'hook_theme()'实现块主题吗?另外,哪个Drupal版本? – 2012-01-09 19:50:35

+0

我正在使用Drupal 6.20。我在原始文章中有hook_theme,除非我错过了一些东西。我不知道我需要一个单独的主题钩为我的块。我错过了什么?谢谢 – EricP 2012-01-09 19:59:02

+0

对不起,我一开始没注意到'hook_theme()'代码。 – 2012-01-09 20:02:41

回答

2

在的问题是,你在前面加上模块“主题”文件夹模板名称,这将陷入困境drupals模板建议查找功能 - 你应该使用“路径”元素为,例如:

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, 
    ), 
); 
} 

有了这个,它应该仍然可以在模块中找到模板文件,但是从主题文件夹覆盖它们应该可以工作。

然而,我不知道这是否也将发现他们对你提出的附加主题的子文件夹(“模板/ ap_news”和“模板/块”),所以你应该与覆盖初试直接放置在主题(和主题'模板')文件夹直接。

+0

Oups忘了在我的答案中实际删除'主题/'前缀 - 现在已经修复。 – 2012-01-09 21:13:36

+0

感谢Henrik。它现在是一种工作,但是当文件不存在时(在“path_to_theme()。/ templates/block”)中它会抛出关于文件不存在的错误。如果它在主站点主题文件夹中找不到,我需要它查看模块文件夹。 – EricP 2012-01-09 21:40:00

+2

@EricP:嗯,它应该只适用于上面的固定示例,但是您可能会遇到命名约定冲突的其他问题,因为您的模板名称与Drupal用作潜在模板覆盖的模板名称相匹配,以主题“视图”输出你的'hook_block'实现。因此,您可能需要再次尝试使用不同的命名方案来进行'内部'区块主题化,而不是当前的'block-δ'版本(与Drupal用于“外部”主题('block','')的一个版本相匹配。 ..)'呼叫)。 – 2012-01-10 14:34:15