通常,我会以另一种方式解决这个问题。我会通过hook_menu()
菜单路由器项而不是节点内容来定义内容,因为它很少打算直接供用户编辑。如果有很多处理,您可以将其与 .module 分开,并将其作为file
每个项目的 a 包含在内。
/**
* Implementation of hook_menu().
*/
function MODULE_menu() {
$items = array();
$items['example/json'] = array(
'title' => 'JSON example',
'page callback' => '_MODULE_json',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['example/xml'] = array(
'title' => 'XML example',
'page callback' => '_MODULE_xml',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* JSON example.
*/
function _MODULE_json($string = '') {
$data = array();
$data['something'] = 0;
$data['anotherthing'] = 1;
drupal_json($data);
}
/**
* XML example. No idea if this actually produces valid XML,
* but you get the idea.
*/
function _MODULE_xml($string = '') {
$data = array();
$data['different'] = 2;
$data['evenmore'] = 3;
// Build XML
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$output .= "<data>\n";
$output .= format_xml_elements($data);
$output .= "</data>\n";
// We are returning XML, so tell the browser.
drupal_set_header('Content-Type: application/xml');
echo $output;
}