【问题标题】:Load view template on module activation在模块激活时加载视图模板
【发布时间】:2009-12-02 20:09:06
【问题描述】:

我开发了一个类似博客的存档功能(你知道,来自功能模块)。 我想编辑 .module 文件,以便将视图模板(捆绑在功能中)自动加载到主题中。有什么办法吗?

【问题讨论】:

  • 您能否阐明“自动将视图模板加载到主题中”的意思? Tnx!
  • 我实际上是指两件事,抱歉。首先,我希望 tpl.php 真正可供该功能使用(就像它在活动主题文件夹中一样)。其次,当您创建视图时--something.tpl.php 需要加载(在views_ui 上重新扫描)。我希望它自动加载。

标签: drupal drupal-views


【解决方案1】:

在一般层面上:您应该考虑“features = modules”并将主题留给...主题!这并不意味着您不应该在您的功能中包含模板,而是您应该评估您构建的模板是否适合您的功能的一般用途,还是特定于您当前使用的主题 .如果是后一种情况,则不应将模板文件与该功能一起打包,而应将其与主题一起保留。想想 views 模块 的工作原理,就明白我的意思了。

[也许您已经意识到这一点并对此进行了考虑,在这种情况下,只需忽略上述内容。我之所以考虑写它,是因为您的句子 “我希望 tpl.php 实际上可用于该功能以使用它(就像它在活动主题文件夹中一样)” 让我很惊讶-使用模板不在主题文件夹中,而是在其模块之一中,而且视图已经提供了“通用”模板。]

也就是说,您通常告诉drupal 使用给定模板的方式是通过在您的模块中实现hook_theme()。在这种情况下 - 不过 - 鉴于您要覆盖由视图定义的模板,您应该改为实现 hook_theme_registry_alter()

实际上有人already did it。这是链接页面中的代码 sn-p:

function MYMODULE_theme_registry_alter(&$theme_registry) {
  $my_path = drupal_get_path('module', 'MYMODULE');
  $hooks = array('node');  // you can do this to any number of template theme hooks
  // insert our module
  foreach ($hooks as $h) {
    _MYMODULE_insert_after_first_element($theme_registry[$h]['theme paths'], $my_path);
  }
}

function _MYMODULE_insert_after_first_element(&$a, $element) {
  $first_element = array_shift($a);
  array_unshift($a, $first_element, $element);
}

当然,您必须为视图而不是节点更改主题注册表(原始示例指的是 CCK 类型)。

与在 views_ui 中使用模板一样,我不确定在安装功能时功能模块是否已经清空了主题缓存(在这种情况下,您应该可以继续使用)。如果没有,您可以通过从安装文件中调用 cache_clear_all() 来手动触发它。如果清空整个缓存过多,您应该深入查看视图模块,了解如何相对于单个视图刷新缓存。

希望这会有所帮助!

【讨论】:

  • features 模块确实清空了主题缓存。非常感谢!
【解决方案2】:

尝试将此添加到您的功能 .module 文件中

/**
 * Implementation of hook_theme_registry_alter().
 */
function MYMODULE_theme_registry_alter(&$theme_registry) {
  $theme_registry['theme paths']['views'] = drupal_get_path('module', 'MYMODULE');
}

在 .install 文件上使用这个

/**
 * Implementation of hook_enable().
 */
function MYMODULE_enable() {
  drupal_rebuild_theme_registry();
}

【讨论】:

    【解决方案3】:

    这是我的 sn-p 声明存储在我的“custom_module”的“模板”文件夹中的视图模板:

    /**
     * Implements hook_theme_registry_alter().
     */
    function custom_module_theme_registry_alter(&$theme_registry) {
      $extension   = '.tpl.php';
      $module_path = drupal_get_path('module', 'custom_module');
      $files       = file_scan_directory($module_path . '/templates', '/' . preg_quote($extension) . '$/');
    
      foreach ($files as $file) {
        $template = drupal_basename($file->filename, $extension);
        $theme    = str_replace('-', '_', $template);
        list($base_theme, $specific) = explode('__', $theme, 2);
    
        // Don't override base theme.
        if (!empty($specific) && isset($theme_registry[$base_theme])) {
          $theme_info = array(
            'template'   => $template,
            'path'       => drupal_dirname($file->uri),
            'variables'  => $theme_registry[$base_theme]['variables'],
            'base hook'  => $base_theme,
            // Other available value: theme_engine.
            'type'       => 'module',
            'theme path' => $module_path,
          );
    
          $theme_registry[$theme] = $theme_info;
        }
      }
    }
    

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-11-21
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      相关资源
      最近更新 更多