Drupal 6覆盖模块块模板文件

时间:2012-01-09 17:18:49

标签: templates drupal drupal-6 themes override

我构建了一个名为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;
    }

}

1 个答案:

答案 0 :(得分:2)

问题在于你将模块'theme'文件夹添加到模板名称中,这将破坏drupals模板建议查找功能 - 你应该使用'path'元素,例如:

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

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

然而,我不确定它是否也会在你提议的其他主题子文件夹中找到它们('templates / ap_news'和'templates / block'),所以你应该先测试一下覆盖直接放在主题(和主题'模板')文件夹中。