Drupal 7 REST 服务 - 抑制 NULL 的 API 输出
Drupal 7 REST services - Suppress API output of NULL
我有一个 API 服务回调函数,它与自己的模块模板相关联以提供 HTML 输出。根据我的理解,API 输出的默认内容类型是 application/json
,因此我不得不手动将其覆盖为 text/html
。
但是,无论我尝试什么,我仍然总是得到 null
输出。我怎样才能抑制这种不需要的 null
输出?
custom_module.module
function api_callback_function($a) {
if (!headers_sent()) {
drupal_add_http_header('Content-Type', 'text/html');
}
print theme('custom_template_name_alias', array(
'b' => $a
));
return;
}
function custom_module_theme() {
$themes = array(
'custom_template_name_alias' => array(
'template' => 'something-only', // name of template file, sans file extension
'variables' => array(
'b' => NULL
),
)
);
return $themes;
}
something-only.tpl.php
<?php
$c = $variables['b'];
$path = drupal_get_path('module', 'custom_module');
global $base_url;
?>
<!doctype html><html class="no-js" lang="en">
<head>
<!-- HEAD related HTML code comes here -->
</head>
<body>
<div>Current value of variable 'b' = <?php echo $c; ?></div>
</body>
</html>
输出
尝试将 return
替换为 die()
in api_callback_function
我有一个 API 服务回调函数,它与自己的模块模板相关联以提供 HTML 输出。根据我的理解,API 输出的默认内容类型是 application/json
,因此我不得不手动将其覆盖为 text/html
。
但是,无论我尝试什么,我仍然总是得到 null
输出。我怎样才能抑制这种不需要的 null
输出?
custom_module.module
function api_callback_function($a) {
if (!headers_sent()) {
drupal_add_http_header('Content-Type', 'text/html');
}
print theme('custom_template_name_alias', array(
'b' => $a
));
return;
}
function custom_module_theme() {
$themes = array(
'custom_template_name_alias' => array(
'template' => 'something-only', // name of template file, sans file extension
'variables' => array(
'b' => NULL
),
)
);
return $themes;
}
something-only.tpl.php
<?php
$c = $variables['b'];
$path = drupal_get_path('module', 'custom_module');
global $base_url;
?>
<!doctype html><html class="no-js" lang="en">
<head>
<!-- HEAD related HTML code comes here -->
</head>
<body>
<div>Current value of variable 'b' = <?php echo $c; ?></div>
</body>
</html>
输出
尝试将 return
替换为 die()
in api_callback_function