在模块激活时加载视图模板

时间:2009-12-02 20:09:06

标签: drupal drupal-views

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

3 个答案:

答案 0 :(得分:1)

总的来说:你应该想到“ features = modules ”并留下主题为......主题!这并不意味着您不应该在模板中添加您的功能,但是您应该评估您构建的模板是否适合您的功能的一般用途,或者它是否特定于您当前使用的主题 。如果是后一种情况,则不应使用该功能打包模板文件,而应将其保留为主题。只要想想 views模块的工作原理,就可以了解我的意思。

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

那就是说,通常告诉drupal使用给定模板的方法是在模块中实现hook_theme()。在这种情况下 - 尽管如此 - 鉴于您要覆盖视图定义的模板,您应该实现hook_theme_registry_alter()

实际上有人already did it。以下是链接页面的代码段:

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()来手动触发它。如果清空整个缓存太多,你应该深入了解视图模块,了解如何相对于单个视图刷新缓存。

希望这有帮助!

答案 1 :(得分:1)

尝试将此添加到您的功能.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();
}

答案 2 :(得分:0)

以下是我的片段,用于声明存储在“custom_module”的“template”文件夹中的视图模板:

/**
 * 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;
    }
  }
}

希望它有所帮助。

相关问题