Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning untemplated output in drupal from menu callback function

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

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

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

like image 589
Tom Avatar asked Feb 18 '10 02:02

Tom


3 Answers

This will return plain text

function myfunction() {
  drupal_set_header('Content-Type: text/plain');
  print "some text";
  exit(0);
}
like image 145
Jeremy French Avatar answered Nov 16 '22 02:11

Jeremy French


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);
}
like image 27
chromix Avatar answered Nov 16 '22 02:11

chromix


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";
}
like image 42
bdombro Avatar answered Nov 16 '22 04:11

bdombro