代码之家  ›  专栏  ›  技术社区  ›  Capi Etheriel

模块激活时加载视图模板

  •  1
  • Capi Etheriel  · 技术社区  · 15 年前

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

    3 回复  |  直到 10 年前
        1
  •  1
  •   mac    15 年前

    在一般层面上:你应该思考” 功能=模块 “把主题留给……主题!这并不意味着您不应该在特性中包含模板,而是应该 评估您所构建的模板是否适合您的功能的一般使用,或者它是否特定于当前使用的主题 .如果是后一种情况,则不应使用该功能打包模板文件,而是将其保留为主题。想想看 视图模块 工作,了解我的意思。

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

    也就是说,您通常告诉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类型)。

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

    希望这有帮助!

        2
  •  1
  •   Lourenzo Ferreira    15 年前

    尝试将此添加到您的feature.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
  •  0
  •   Sebastien Malot    10 年前

    下面是声明存储在“自定义模块”的“模板”文件夹中的视图模板的代码段:

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

    希望它能帮助别人。