开发者

returning untemplated output in drupal from menu callback function

开发者 https://www.devze.com 2022-12-20 23:38 出处:网络
I have a drupal module with a function that returns an attachment text/plain, function mymodule_menu() {

I have a drupal module with a function that returns an attachment text/plain,

function mymodule_menu() {
$item开发者_JAVA百科s = 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";
}

But it returns the page in the page.tpl.php template, however I want it untemplated, how do I over-ride the theme to make it return plain text?

Thanks,

Tom


This will return plain text

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


Alternatively you can use the 'delivery callback' setting in your menu callback definition. Now your page callback function will be run through a custom function that just prints and exits, rather than calling drupal_deliver_html_page(), which is what outputs all the typical theme markup, etc.

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);
}


The best and simplest solution is to just have your callback print your html and return nothing.

For example,

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


if you'd create a template like html--barebones.tpl.php, containing just

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

you could hook that template to YOURTHEME_preprocess_html(), like so:

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

now, if you call your page with the additional query ?barebones, like drupal/foo/bar?barebones, it will return the barebones version.

theres a tricky bit in getting your result back. var_dump($variables['page']) to see where drupal left your text. Its been tucked inside the render array surrounded by all kind of info used to render the text, which you are not using. Making me wonder if it wouldnt be more efficient to just print it and exit() inside myfunction :-)


Your module can define template files (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;
}
?>

By creating a new .tpl.php file for the page in question. E.g.

page-module.tpl.php

page-module.tpl.php would only need to be a simple page, e.g.

<?php
print $content;
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消