Drupal 7 将自定义页面模板应用于Drupal 7模块的内容

Drupal 7 将自定义页面模板应用于Drupal 7模块的内容,drupal-7,drupal-modules,drupal-theming,drupal-fapi,Drupal 7,Drupal Modules,Drupal Theming,Drupal Fapi,我正在开发一个Drupal模块。它的一部分打开一个弹出窗口,显示其中的一些元素,并使用JavaScript将输入传输回主页 由于这是一个小窗口,我不希望它显示站点主题的完整主题边框 在Drupal 6中,我通过以下几点实现了这一点: function MyPopupPageHandler() { @content = "Content..."; //Add additional content to @content; print theme("page", @content); }

我正在开发一个Drupal模块。它的一部分打开一个弹出窗口,显示其中的一些元素,并使用JavaScript将输入传输回主页

由于这是一个小窗口,我不希望它显示站点主题的完整主题边框

在Drupal 6中,我通过以下几点实现了这一点:

function MyPopupPageHandler() {
  @content = "Content...";
  //Add additional content to @content;
  print theme("page", @content);
}
但是,Drupal7希望theme()函数的第二个参数是数组,我所看到的示例都没有显示如何设置页面的主要内容

我想要的是模块中的自定义页面模板,如果站点主题提供了模板,则可以覆盖该模板

我想知道:

  • 我需要在传递给theme()函数的数组中放入哪些元素
  • 我应该怎样称呼我的模板文件
  • 我如何告诉Drupal在哪里找到我的模板,因为默认情况下它需要在模块中
  • 感谢你能提供的任何帮助

    James

    1)函数
    theme()
    的第二个参数必须是关联数组。您的函数应该如下所示:

    function MYMODULE_custom_function() {
      $content = "Some stuff";
      return theme('MYMODULE_custom_output', array('content' => $content));
    }
    
    2) 我猜您在相同的
    模块
    文件中的
    hook\u theme()
    函数中的意思是“我应该在哪里调用我的模板文件”:

    function MYMODULE_theme() {
      return array(
        'MYMODULE_custom_output' => array(
          'variables' => array('content' => array()),
          // You can also use template file here : 'template' => 'MYMODULE-template'
          // OR use the following theme_function() if you don't want to create a new file
        ),
      );
    }
    
    // If you don't use template file
    function theme_MYMODULE_custom_output($variables) {
      $output = // Arrange your html output here
      return $output;
    }
    
    3) 如果您决定使用自定义模板文件,您可以阅读以下内容:我希望这会有所帮助

    请保持宽容,因为我对Drupal还是新手,我在这里也尽了最大努力:o)

    试试这个 首先,在.module文件中创建一个菜单

    function MYMODULE_menu()
    {
        $items['Demo'] =array(
                'title' => 'Demo Page',
                'page callback' => 'demo_page', // call a function
                'access arguments' => array('access content'),
        );
         return $items;
    
    }
    
    创建函数之后

    function demo_page()
    {
        $select = db_select('node', 'n');
        $select = $select->fields('n', array('id'))
        ->extend('PagerDefault');
    
        $queried_nodes = $select->execute()
        ->fetchAllAssoc('id');
        $pager = theme('pager');
    
        return  theme('demo_template', array('nodes' => $queried_nodes , 'pager' => $pager)); // call a theme or you have no pass any argument in theme to change a 'nodes'=> NULL or 'pager'=>NULL 
    }
    
    在我创建了一个主题函数之后

    function MYMODULE_theme()
    {
        return array(
          'demo_template' => array(
            'template' => 'demo-page',//this is file name of template file
            'variables' => array('nodes' => NULL,'pager' => NULL), //this is pass avarible of templates file
            'path' => drupal_get_path('module', 'MYMODULE_NAME').'/template' // set a path of file 
        ),
     );
    
    }
    
    在sites/all/modules/MYMODULENAME/template中创建类似demo-page.tpl.php的文件名后/


    清除缓存

    我想我已经解决了这个问题。我昨晚花了很多时间重新学习如何为Drupal编程,我需要放弃基于D6的方法,重新开始。页面处理程序需要返回一个表单元素数组,它们应该返回给调用函数,而不是直接打印,因此不需要直接调用theme()函数。在hook_主题函数中,我可以设置要使用的模板。稍后我会发布更详细的解释,以防对任何人有所帮助。