2014-02-27 139 views
1

我開發一個Drupal模塊的內容。它的一部分打開一個彈出窗口,顯示一些元素,並使用JavaScript將輸入傳回主頁面。應用自定義頁面模板,從Drupal 7的模塊

由於這是一個小窗口,我不希望它從網站的主題顯示完整的主題邊界。

在Drupal的6我能夠用以下來實現:

function MyPopupPageHandler() { 
    @content = "Content..."; 
    //Add additional content to @content; 
    print theme("page", @content); 
} 

然而,Drupal 7的期望的主題()函數的第二個參數是一個數組,和I」的實施例中無已經見過顯示我如何設置頁面的主要內容。

我想是在模塊中的自定義頁面模板,可以通過該網站的主題,如果它提供了一個覆蓋。

我想知道:

  1. 我需要什麼元素來把我傳遞給主題()函數在數組中?
  2. 我應該怎麼稱呼我的模板文件?
  3. 我如何告訴Drupal的在哪裏可以找到我的模板,因爲它需要由默認的模塊?

欣賞你可以提供任何幫助。

詹姆斯

+0

我想我已經想通了這一點。 –

+0

我昨晚在學習如何爲Drupal編程,我需要放棄基於D6的方法並重新開始。 頁面處理程序需要返回一個表單元素數組,並且它們應該返回到調用函數而不是直接打印,因此不需要直接調用theme()函數。在hook_theme函數中,我可以設置要使用的模板。 我會稍後發佈更詳細的解釋,以幫助任何人。 –

回答

0

1)theme()函數的第二個參數必須是關聯數組。你的函數應該是這樣的:

function MYMODULE_custom_function() { 
    $content = "Some stuff"; 
    return theme('MYMODULE_custom_output', array('content' => $content)); 
} 

2)我猜你的意思是「我應該在哪裏打電話給我的模板文件?」在hook_theme()功能在同一.module文件:

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)告知哪裏可以找到您的自定義模板文件,如果你決定使用一個,你可以看到這一點:https://drupal.org/node/715160,我希望這將有助於。

請繼續放縱,因爲我還是新使用Drupal,我也盡我的最佳位置:O)

2

試試這個 首先創建.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 
    ), 
); 

} 

後,你有創造這樣的網站演示page.tpl.php中的文件名/所有/模塊/ MYMODULENAME /模板/

和清除緩存