【问题标题】:Drupal 6 overriding a modules Block template filesDrupal 6覆盖模块块模板文件
【发布时间】: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


【解决方案1】:

问题是您将模块“主题”文件夹添加到模板名称之前,这会破坏 drupal 模板建议查找功能 - 您应该为此使用“路径”元素,例如:

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”)中找到它们,因此您应该首先使用直接覆盖直接放置在主题(和主题“模板”)文件夹中。

【讨论】:

  • Oups,忘记在我的答案示例中实际删除“主题/”前缀 - 现在已修复。
  • 谢谢亨利克。它现在可以工作了,但是当文件不存在时(在“path_to_theme().'/templates/block”中)它会抛出一个关于文件不存在的错误。如果在主站点主题文件夹中找不到它,我需要它在模块文件夹中查找。
  • @EricP:嗯,它应该只适用于上面的固定示例,但是您可能面临命名约定冲突的额外问题,因为您的模板名称与 Drupal 用作潜在模板覆盖的模板名称匹配,以主题化您的 hook_block 实现的“视图”输出。因此,您可能想再次尝试对“内部”块主题使用与当前“块-[delta]”版本不同的命名方案(与 Drupal 用于“外部”theme('block', ...) 调用的命名方案相匹配)。跨度>
  • 我更新了我原来的帖子。我现在创建了一个函数来查找文件。我稍后会尝试用不同的命名约定重命名它们,但现在这对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 2011-01-25
相关资源
最近更新 更多