2010-02-18 91 views
8

我有一個返回附件text/plain的一個功能一個Drupal模塊,返回untemplated輸出

function mymodule_menu() { 
$items = array(); 
$items[MY_PATH] = array(
'title' => 'some page', 
'page callback' => 'myfunction', 
'type' => MENU_CALLBACK, 
); 
} 

function myfunction() 
{ 
drupal_set_header('Content-Type: text/plain'); 
return "some text"; 
} 

但它返回在page.tpl.php中模板的頁面,我希望它沒有模糊,我如何超越主題使其返回純文本?

感謝,

湯姆

回答

0

你的模塊可以定義模板文件(reference):

<?php 
function mymodul_preprocess_page(&$variables) { 
    foreach ($variables['template_files'] as $file) { 
    $template_files[] = $file; 
    if ($file == 'page-node') { 
     $template_files[] = 'page-'. $variables['node']->type; 
    } 
    } 
    $variables['template_files'] = $template_files; 
} 
?> 

通過創建問題的網頁新的.tpl.php文件。例如。

頁module.tpl.php

頁module.tpl.php將只需要一個簡單的網頁,例如:

<?php 
print $content; 
?> 
+0

您好,感謝! 該文本文件不是內容類型,它的報告生成從PHP /數據庫,所以我不知道我明白你的意思。 但是我剛剛意識到,如果我做 打印「whateever」; 而不是 返回「無論」; drupal不顯示頁面模板。我仍然有興趣知道如何讓drupal將page-plain.tpl.php模板應用於任意內容... – Tom 2010-02-18 03:09:31

9

這將返回純文本

function myfunction() { 
    drupal_set_header('Content-Type: text/plain'); 
    print "some text"; 
    exit(0); 
} 
+1

+1 - 這是Drupal本身返回'unthemed'內容的方式,例如,回答js回調或文件(啓用私人文件時)。有關核心的示例,請參閱http://api.drupal.org/api/function/file_transfer/6。 – 2010-02-19 08:18:09

+1

警告一句話適用:當通過exit()/ die()結束正常的Drupal請求處理時,* hook_exit將不會被調用*,所以請確保在這些情況下不要依賴它。 – 2010-02-19 08:20:28

+0

也想知道這是如何工作的。 – 2011-06-22 10:06:46

1

如果您想要創建一個類似HTML模板 - barebones.tpl.php,只包含

<?php 
    drupal_set_header('Content-Type: text/plain'); 
    print $barebones; 
?> 

,你可以鉤住模板到YOURTHEME_preprocess_html(),如下所示:

function YOURTHEME_preprocess_html(&$variables) { 
    if (array_key_exists('barebones',$_REQUEST)) { 
     $variables['barebones'] = $variables['page']['foo']['bar']; 
     $variables['theme_hook_suggestions'][] = 'html__barebones'; 
    } 
} 

現在,如果您使用附加查詢?準系統呼叫您的頁面,如drupal/foo/bar?barebones,它將返回準系統版本。

這是一個棘手的問題。 var_dump($variables['page'])查看drupal離開您的文本的位置。它被塞入渲染數組中,被所有用於渲染文本的信息所包圍,而這些信息並不是你使用的。讓我想知道是否它不會更有效率只打印它並退出()內myfunction :-)

6

或者,您可以使用您的菜單回調定義中的'傳遞迴調'設置。現在你的頁面回調函數將通過剛剛打印後退出,而不是調用drupal_deliver_html_page(),這是所有輸出典型的主題標記等

function mymodule_menu() { 
    $items = array(); 
    $items['MY_PATH'] = array(
    'title' => 'some page', 
    'page callback' => 'myfunction', 
    'type' => MENU_CALLBACK, 
    'delivery callback' => 'mymodule_deliver_page', 
); 
    return $items; 
} 

function mymodule_deliver_page($page_callback_result) { 
    print $page_callback_result; 
    exit(0); 
} 
+0

我認爲'delivery callback'是在drupal 7中處理這個問題的最好方法,但不是退出;一般drupal_exit();應該在Drupal 7中使用:https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_exit/7 這會調用hook_exit()等等。 – Phizes 2014-07-07 12:17:33

2

最好的和最簡單的解決方案是一個自定義函數運行只是讓你的回調打印你的HTML並且什麼都不返回。

例如,

// Hooks menu to insert new url for drupal 
 
function MYMODULE_menu() { 
 
    $items = array(); 
 
    $items['member-stats.php'] = array(
 
    'page callback' => '_MYMODULE_menu_callback', 
 
    'access callback' => TRUE, 
 
); 
 
    return $items; 
 
} 
 

 
// Callback prints and doesn't return anything 
 
function _MYMODULE_menu_callback() { 
 
    print "Hello, world"; 
 
}